基于组件的游戏编程

    
 传统的继承会带来很多修改上的不便(为什么?看书吧^_^),那么基于组件的开发有什么优点呢?(我也不说了^_^,查资料吧。。。)

    基本上就是比如:我们有一个组件的接口
  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跟渲染器,最基本的就是保存各个引用,如果这样的话整个
 世界就乱套了,耦合太高了。有人提到用消息机制可以解开各系统之间的耦合,我也觉得确实是一个不错的机制;
 但是我觉得更好的是用两套系统,然后共享一个数据源,这样系统跟系统之间就可以不用打交道了。但是关于这部分
 的接口可能比较难抽象,我也是个新手没什么研究,有机会一定得研究看看。如果大家有好的资料也麻烦共享一下。谢谢!
 
 不过我觉得有点不好的就是:访问所有的组件都要查表来完成,会影响一点速度,
 可能这就是我们要在速度跟扩展性之间权衡吧!谁有更好的解决办法就不要吝啬,大声说出来讨论吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值