I am making a Roguelike game in java, and I want every creature to have bodyparts (as in Dwarf fortress). I was just wondering what the best way to implement this might be.
-
http://en.wikipedia.org/wiki/Scene_graph – Mike Samuel Feb 24 '13 at 07:09
-
There is a stackexchange for game development related questions: http://gamedev.stackexchange.com – Spoike Feb 24 '13 at 07:21
-
1Rouge is all about the wandering .. but really, no suitable question detected. – Feb 24 '13 at 07:23
-
It's a perfectly reasonable question and has valid programming content (what data structure is appropriate? how to implement separation of body parts from creatures? etc.) – mikera Feb 24 '13 at 08:15
-
1"Best way" depends entirely on rest of the architecture. You should start with a game which does not have body parts, probably. There are dozens of possible and perfectly valid patterns you could use. – hyde Feb 24 '13 at 08:21
2 Answers
Like most things in Java you could start modeling it all in objects. Take all the appropriate nouns from your requirements (creature and bodypart) and figure out their relationships (a creature has several bodyparts).
public class Creature {
private ArrayList<BodyPart> bodyParts;
// could be array instead
}
public class BodyPart {
public int health;
}
As to how to use it in your rougelike game it depends on how you want to write your actual game.
Edit:
Here is a gist to help you get started: https://gist.github.com/spoike/5023039

- 119,724
- 44
- 140
- 158
-
How could i go about adding 15-20 bodyparts to a creature? And thanks very much for that answer – user2103959 Feb 24 '13 at 07:15
-
@user2103959 depends on how you want to access those bodyparts. I'd probably use a HashMap instead to allow for you access to a bodypart. I updated the question with a gist for you to hack away on. – Spoike Feb 24 '13 at 07:44
Firstly, I'd suggest adopting a prototype-based object model. This is generally more flexible than a fixed OOP-style inheritance heirarchy. In my roguelike Tyrant all game objects have a HashMap of properties, for example.
Then, I would define the list of body parts for each creature in the prototype. This way you can define different body part configurations for different creatures (e.g. some may have wings....)
Finally, I would implement the body parts using composition, i.e. a creature has a list (ArrayList
perhaps?) of body parts that correspond to the list of body parts defined in the prototype. These body parts should themselves be valid game objects (i.e. they have their own prototype, they can be separated from the creature and scattered over the map etc....). When a creature is first created, you create the necessary body parts as part of the creature initialisation.

- 105,238
- 25
- 256
- 415