对于实体/组件系统中的每个组件,使用通用的无所不包的Component
类与特定的类(PositionComponent
、InputComponent
等)会有什么不利之处?
我所说的“通用”是指:
class Component {
String name; // component name, like "position"
Map<Key, Object> attribs; // Attributes like "x" and "y" stored here.
}
其中“特定”组件可能是:
class PositionComponent {
int x, y;
}
我想为每个组件创建一个单独的类,这意味着我的代码库中有大量的3-5行类。
发布于 2013-12-17 06:40:13
在您提议的“泛型”组件方法中,有几件事情需要注意:
attribs
成员或类似的成员中。对于ECS来说,这是一种有效的可能方法,但并非所有ECS都选择“组件是纯数据”模型,而且它肯定不是唯一的方法。就我个人而言,我从未见过像你在野外提出的任何真实的“通用组件”。这似乎是一个非常糟糕的选择,如果没有其他原因,仅仅是当您对组件进行了如此广泛的概括时,您最好通过使实体成为存储所有这些属性并命名空间它们的东西来减少开销:
class Entity {
Dictionary<string, object> components;
}
// ...
var entity = new Entity();
entity.components.Add("Position.X", 1.0f);
entity.components.Add("Position.Y", 2.0f);
entity.components.Add("Position.Z", 3.0f);
entity.components.Add("RigidBody.Mass", 100.0f);
entity.components.Add("Killable.HP", 15.0f);
(但我还是不推荐。)
https://gamedev.stackexchange.com/questions/67485
复制相似问题