所以我只是在用Java編寫遊戲的開始,我在寫我的遊戲對象。現在我在Evolve Your Hierarchy這裏讀到,你應該把你的遊戲當成作品而不是作爲一個大的階級層次結構。從以前的鏈接此圖顯示:如何在Java中編寫可靠的純聚合(組合)遊戲對象?
然而,在實際開始認真執行我對哪裏應用接口中的一個小問題。
比方說你有一個叫播放器和可移動的接口和渲染類。你實現這個使用公共接口變量:
class Player {
public Moveable moveable;
public Renderable renderable;
}
class GenericMoveable implements Moveable {
// Function implementations
}
class PlayerRenderable implements Renderable {
// Function implementations
}
還是你試圖通過直接應用接口對象做到這一點:
class Player implements Moveable, Renderable {
private GenericMoveable genericMoveable;
// Non-direct Implementation of Moveable
void someMoveFunc(double x, double y) {
genericMoveable.someMoveFunc(x, y);
}
// Direct implementation of Renderable
void someRenderableFunction() {
// Player class specific code
}
}
class GenericMoveable implements Moveable {
// Function implementations
}
現在目前我覺得第二種方法比較好。主要原因是因爲我可以創建以下列表:
List<Renderable> renderObjects; // use this to draw all of the objects to the screen
List<Moveable> moveObjects; // use this to move all objects at once
我真的只是想確認我是在正確的軌道上。我非常肯定,當通過Java編寫遊戲創建遊戲對象時,您希望將接口直接應用到遊戲對象中,然後在需要時使用專用接口,併爲其餘部分直接實現這些功能。它是否正確?我在做什麼的積極和消極?
雖然,而在我們這裏,還有什麼我需要看出來的成分中執行遊戲的Java對象?我應該做什麼不同?感謝您提前做出任何回覆,我非常感謝任何幫助。
第二個例子似乎要好很多,因爲你可以從列表中做出Moveable和Renderable接口。但是,我有許多實現Moveable的類,我發現我複製了genericMoveable.someMoveFunc(x,y) ;在所有這些問題中 - 你遇到過這個問題嗎? – sdasdadas 2013-01-14 22:03:14
@sdasdadas其實我建議採取看看這個:http://gamadu.com/artemis/ – 2013-01-15 02:24:12