问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/15049510/how-could-i-implement-body-parts-in-a-java-roguelike-game