How could I implement body parts in a Java roguelike game [closed]

浪子不回头ぞ 提交于 2019-12-12 01:29:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!