传统的继承会带来很多修改上的不便(为什么?看书吧^_^),那么基于组件的开发有什么优点呢?(我也不说了^_^,查资料吧。。。)
基本上就是比如:我们有一个组件的接口
class IComponent
{
public:
virtual const CObjectId& GetComponentId() const = 0; //自身的ID
virtual const CObjectId& GetFamilyId() const = 0; //父组件的ID
}
然后我们定义了很多实现IComponent的组件(对!就是我们要用到的组件),比如:
class IRenderable :public IComponent
{
public:
virtual void Render() const = 0;
}
class IMoveable : public IComponent
{
public:
virtual void Move() = 0;
}
然后有一个由多个组件集成的类(对!就是我们以后要用的类^_^)
class GameObject
{
public:
const IComponent* GetComponent(const CObjectId& id) const;
void AddComponent(IComponent* component);
private:
map<const CObjectId&, IComponent*> components;
}
然后我们就可以通过组件的方式来定义一个既可以Render又可以Move的精灵
class Sprite : public GameObject
{
GameObject()
{
AddComponent(IRenderable);
AddComponent(IMoveable);
}
}
void main()
{
Sprite sprite;
IMoveable* moveable = static_cast<IMoveable*>(sprite.GetComponent("moveable"));
if(moveable != NULL)
moveable->Move();
IRenderable* renderable = static_cast<IRenderable*>(sprite.GetComponent("renderable"));
if(renderable != NULL)
renderable->Render();
}
更加强大的是我们还可以通过XML来配置类,然后通过一个管理器来管理,
那以后我们就可以不用再写类了,直接通过XML就可以动态配置了,哈哈哈。。。
class GameObjectManager
{
public:
GameObject* CreateObject(const CObjectId& id);
}
<Objects>
<Object id="sprite">
<Component id="renderable"/>
<Component id="moveable" x="10" y="100"/>
</Object>
</Objects>
void main()
{
Sprite* sprite = static_cast<Sprite*>(GameObjectManager::GetSingleton().CreateObject("sprite"));
if(sprite == null)
return;
IMoveable* moveable = static_cast<IMoveable*>(sprite->GetComponent("moveable"));
if(moveable != NULL)
moveable->Move();
IRenderable* renderable = static_cast<IRenderable*>(sprite->GetComponent("renderable"));
if(renderable != NULL)
renderable->Render();
}
可能你会问,组件之间怎么通信呢?只要在组件间保存一个GameObject的指针就可以了。
说点题外话,游戏系统间用什么通信比较合理呢?比如AI跟渲染器,最基本的就是保存各个引用,如果这样的话整个
世界就乱套了,耦合太高了。有人提到用消息机制可以解开各系统之间的耦合,我也觉得确实是一个不错的机制;
但是我觉得更好的是用两套系统,然后共享一个数据源,这样系统跟系统之间就可以不用打交道了。但是关于这部分
的接口可能比较难抽象,我也是个新手没什么研究,有机会一定得研究看看。如果大家有好的资料也麻烦共享一下。谢谢!
不过我觉得有点不好的就是:访问所有的组件都要查表来完成,会影响一点速度,
可能这就是我们要在速度跟扩展性之间权衡吧!谁有更好的解决办法就不要吝啬,大声说出来讨论吧!