设为首页 加入收藏

TOP

《游戏编程模式》(6)(一)
2017-10-13 10:39:19 】 浏览:9009
Tags:《游戏编程模式》

Chapter 14 组件模式

允许一个单一的实体跨越多个不同域而不会导致耦合。

为实现两个类之间的代码共享,应该让他们拥有同一个类的实例,而不是继承同一个类。

使用情境:

  1. 有一个涉及多个域的类。但希望这些域保持解耦;
  2. 这个类很庞大;
  3. 希望定义许多共享不同能力的对象。

分割不同的域:

 1 class InputComponent
 2 {
 3 
 4 public:
 5   void update(Bjorn& bjorn)
 6   {
 7     switch (Controller::getJoystickDirection())
 8     {
 9       case DIR_LEFT:
10         bjorn.velocity -= WALK_ACCELERATION;
11         break;
12  
13       case DIR_RIGHT:
14         bjorn.velocity += WALK_ACCELERATION;
15         break;
16     }
17   } 
18 
19 private:
20   static const int WALK_ACCELERATION = 1;
21 
22 };
23 
24 class PhysicsComponent
25 {
26 
27 public:
28   void update(Bjorn& bjorn, World& world)
29   {
30     bjorn.x += bjorn.velocity;
31     world.resolveCollision(volume_,
32         bjorn.x, bjorn.y, bjorn.velocity);
33   }
34  
35 private:
36   Volume volume_;
37 
38 }; 
39 
40 class GraphicsComponent
41 {
42 
43 public:
44   void update(Bjorn& bjorn, Graphics& graphics)
45   {
46     Sprite* sprite = &spriteStand_;
47     if (bjorn.velocity < 0)
48     {
49       sprite = &spriteWalkLeft_;
50     }
51     else if (bjorn.velocity > 0)
52     {
53       sprite = &spriteWalkRight_;
54     } 
55 
56     graphics.draw(*sprite, bjorn.x, bjorn.y);
57   }
58 
59 private:
60   Sprite spriteStand_;
61   Sprite spriteWalkLeft_;
62   Sprite spriteWalkRight_;
63 
64 };

最后的GameObject类:

 1 class GameObject
 2 {
 3 
 4 public:
 5   int velocity;
 6   int x, y; 
 7 
 8   void update(World& world, Graphics& graphics)
 9   {
10     input_.update(*this);
11     physics_.update(*this, world);
12     graphics_.update(*this, graphics);
13   } 
14 
15 private:
16   InputComponent input_;
17   PhysicsComponent physics_;
18   GraphicsComponent graphics_;
19 
20 };

 

更进一步,抽象出InputComponent为基类,使得GameObject可以连接不同的Input组件:

 1 class InputComponent
 2 {
 3 public:
 4   virtual ~InputComponent() {}
 5   virtual void update(Bjorn& bjorn) = 0;
 6 };
 7 
 8 class PlayerInputComponent : public InputComponent
 9 {
10 
11 public:
12   virtual void update(Bjorn& bjorn)
13   {
14     switch (Controller::getJoystickDirection())
15     {
16       case DIR_LEFT:
17         bjorn.velocity -= WALK_ACCELERATION;
18         break;
19 
20       case DIR_RIGHT:
21         bjorn.velocity += WALK_ACCELERATION;
22         break;
23     }
24   } 
25 
26 private:
27   static const int WALK_ACCELERATION = 1;
28 
29 };
30 
31 class DemoInputComponent : public InputComponent
32 {
33 
34 public:
35   virtual void update(Bjorn& bjorn)
36   {
37     // AI to automatically control Bjorn...
38   }
39 };

其他组件也是如此:

 1 class PhysicsComponent
 2 {
 3 public:
 4   virtual ~PhysicsComponent() {}
 5   virtual void update(GameObject& obj, World& world) = 0;
 6 };
 7 
 8 class GraphicsComponent
 9 {
10 public:
11   virtual ~GraphicsComponent() {}
12   virtual void update(GameObject& obj, Graphics& graphics) = 0;
13 };
14 
15 class BjornPhysicsComponent : public PhysicsComponent
16 {
17 public:
18   virtual void update(GameObject& obj, World& world)
19   {
20     // Physics code...
21   }
22 };
23 
24 class BjornGraphicsComponent : public GraphicsComponent
25 {
26 public:
27   virtual void update(GameObject& obj, Graphics& graphics)
28   {
29     // Graphics code...
30   }
31 };

然后是更灵活的GameObject类:

 1 class GameObject
 2 {
 3 
 4 public:
 5   int velocity;
 6   int x, y;
 7  
 8   GameObject(InputComponent* input,
 9              PhysicsComponent* physics,
10              GraphicsComponent* graphics)
11   : input_(input),
12     physics_(physics),
13     graphics_(graphics)
14   {}
15 
16   void update(World& world, Graphics& graphic
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MediatorPattern(中介者模式) 下一篇《游戏编程模式》(7)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目