Component-based Development & XNA & Design Patterns

Component-based Development & XNA & Design Patterns


他强调把程序的功能性分开的软件开发过程。

Component based Programming在游戏引擎里被广泛得运用,如Unity3D:

ComponentUnity

Component Based Programming由三个主要构成:

System: 处理Render、Physics、Particle。

Entries:又称为GameObject,出现在游戏引擎画面上的对象(游标、地板…等)。

Component:一个Entries可能会包含数个Componets,为这对象添加功能如Box Collider(对象碰撞网格)、Mesh Renderer(Render画面)。

以XNA来说:
Componet最常用的莫过于GameComponent、DrawableGameComponent

在使用XNA 当产生一个DrawableGameCompoent 就必须 override三个Method

   1: public override void LoadContent(){}
   2: public override void Draw(){}
   3: public override void Update(){}


在XNA中每新增一个Compoent时,都要使用Components.Add(XXComponent),使用过XNA的人想必都不陌生吧。

当没有Xna 的Framework,那是必须自己写一个Framework(如PlayStationMobile),这时在Design Patterns有个模式 Composite Pattern:

Composite:将对象组合成树形结构已表示[部分-整体]的层次结构。使的用户对单个对象和组合对象的使用具有一致性。

DesignPatterns-Component

   1: abstract class Component
   2: {
   3:     protected String name;
   4:     public Component(String name)
   5:     {
   6:         this.name = name;
   7:     }
   8:     public abstract void Add(Component c);
   9:     public abstract void Remove(Component c);
  10:     public abstract void Display(int depth);
  11: }


   1: class Leaf:Component
   2: {
   3:     public Leaf(string name)
   4:         :base(name){}
   5:     public override void Add(Component c)
   6:     {
   7:         Console.WriteLine("Can't add to a leaf");
   8:     }
   9:     public override void Remove(Component c)
  10:     {
  11:         Console.WriteLine("Can't remove frome a leaf");
  12:     }
  13:     publoc override void Display()
  14:     {
  15:         Console.WriteLine(new String('-',depth) + name);
  16:     }
  17: }


   1: class Composite:Component
   2: {
   3:     private Listchildren = new List();
   4:     public Composite(string name)
   5:         :base(name){}
   6:     public override void Add(Component C)
   7:     {
   8:         children.Add(C);
   9:     }
  10:     public override void Remove(Component C)
  11:     {
  12:         children.Add(C);
  13:     }
  14:     public override void Display(int depth)
  15:     {
  16:         Console.WriteLine(new String('-',depth) + name);
  17:         foreach(Component conponent in children)
  18:         {
  19:             component.Display(depth + 2);
  20:         }
  21:     }
  22: }


在这里假设我要管理敌人的Manager的类、管理子弹的类、管理游戏状态的类,其在主程序:

   1: static void Main(String[] args)
   2: {
   3:     Composite root = new Composite("root");
   4:     
   5:     Composite enemyManager = new Composite("EnemyManager");
   6:     enemyManager.Add(new Leaf("Enemy A"));
   7:     enemyManager.Add(new Leaf("Enemy B"));
   8:     root.Add(enemyManager);
   9:     
  10:     Composite bulletManager = new Composite("BulletManager");
  11:     root.Add(bulletManager);
  12:     
  13:     Leaf gameManager = new Leaf("GameManager");
  14:     root.Add(gameManager);
  15: }


这游戏内对象关系就变成:

Tem

这技术不管用在2D Engine 或 3D Engine都广泛使用,所以当使用Engine做游戏开发这是很重要的知识。