【发布时间】:2016-05-04 21:15:52
【问题描述】:
我正在阅读ECS/Composition over inheritance 并决定修改我目前正在开发的一款文字冒险游戏,以更好地理解这个概念。
我决定在我的游戏中使用特定武器进行尝试。
现在,在我的游戏中,逻辑是每个武器都被视为一个 IGameObject。考虑到这一切,我想出了以下几点:
武器界面:
public interface IWeapon {
int getWeaponDamage();
}
武器类:
public class Weapon implements IWeapon {
private int damage;
public Weapon(int damage)
{
this.damage = damage;
}
@Override
public int getWeaponDamage() {
return this.damage;
}
IGameObject 接口:
public interface IGameObject {
String getGameObjectID();
String getGameObjectName();
String getGameObjectDescription();
}
游戏对象类:
public class GameObject implements IGameObject {
private String ID;
private String name;
private String Desc;
public GameObject(String ID, String name, String Desc)
{
this.ID = ID;
this.name = name;
this.Desc = Desc;
}
使用所有这些的抽象类:
public abstract class GameGun implements IGameObject, IWeapon {
IGameObject gameObj;
IWeapon weaponObj;
//Left out getters to keep it simple and easy to read.
public GameGun(IGameObject game, IWeapon weapon)
{
this.gameObj = game;
this.weaponObj = weapon;
}
public void fire()
{
System.out.println("Shot Fired");
}
public void reload()
{
//implementation for reload method
}
把它们放在一起:
主要方法:
GameObject sleep = new GameObject("SLP-1","Sleep Weapon","A Custom Made Gun");
Weapon sleepDamage = new Weapon(10);
GameGun sleepWeapon = new SleepWeapon(sleep,sleepDamage);
问题:
- 我只是想知道我的实现是否正确?
- 如果不是,我将如何更正它?
【问题讨论】:
标签: java oop game-engine composition