-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathComponentPatternExample.cs
186 lines (148 loc) · 4.17 KB
/
ComponentPatternExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//-------------------------------------------------------------------------------------
// ComponentPatternExample.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace ComponentPatternExample
{
public class ComponentPatternExample : MonoBehaviour
{
RPGGame rpgGame = new RPGGame();
void Start()
{
rpgGame.Start();
}
void Update()
{
rpgGame.Update();
}
}
/// <summary>
/// 游戏类
/// </summary>
class RPGGame
{
public int velocity;
public int x = 0, y = 0;
//辅助类对象
public WorldX worldX = new WorldX();
public GraphicsX graphicsX = new GraphicsX();
//组件
private InputComponent inputComponent;
private PhysicsComponent physicsComponent;
private GraphicsComponent graphicsComponent;
//组件List
public List<BaseComponent> ComponentList = new List<BaseComponent>();
//组件List容量
int componentAmount = -1;
public void Start()
{
inputComponent = new PlayerInputComponent();
physicsComponent = new PlayerPhysicsComponent();
graphicsComponent = new PlayerGraphicsComponent();
ComponentList.Add(inputComponent);
ComponentList.Add(physicsComponent);
ComponentList.Add(graphicsComponent);
Debug.Log("Game Components Initialization Finish...");
Debug.Log("Please enter LeftArrow or RightArrow button to play...");
}
public void Update()
{
if (ComponentList == null)
{
return;
}
componentAmount = ComponentList.Count;
for (int i = 0; i < componentAmount; i++)
{
ComponentList[i].Update(this);
}
}
};
/// <summary>
/// 输入组件
/// </summary>
class PlayerInputComponent : InputComponent
{
public void Update(RPGGame game)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
game.velocity -= WALK_ACCELERATION;
Debug.Log(" game velocity= " + game.velocity.ToString());
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
game.velocity += WALK_ACCELERATION;
Debug.Log(" game velocity= " + game.velocity.ToString());
}
}
private int WALK_ACCELERATION = 1;
}
/// <summary>
/// 物理组件
/// </summary>
class PlayerPhysicsComponent : PhysicsComponent
{
public void Update(RPGGame game)
{
game.x += game.velocity;
//Handle Physics...
}
}
/// <summary>
/// 图形组件
/// </summary>
class PlayerGraphicsComponent : GraphicsComponent
{
public void Update(RPGGame game)
{
//Handle Graphics...
if (game == null || game.graphicsX == null)
{
return;
}
Sprite sprite = spriteStand;
if (game.velocity < 0)
{
sprite = spriteWalkLeft;
}
else if (game.velocity > 0)
{
sprite = spriteWalkRight;
}
game.graphicsX.Draw(sprite, game.x, game.y);
}
private Sprite spriteStand;
private Sprite spriteWalkLeft;
private Sprite spriteWalkRight;
}
#region interfaces
interface BaseComponent
{
void Update(RPGGame game);
}
interface GraphicsComponent : BaseComponent
{
new void Update(RPGGame game);
}
interface PhysicsComponent : BaseComponent
{
new void Update(RPGGame game);
}
interface InputComponent : BaseComponent
{
new void Update(RPGGame game);
}
#endregion
#region 辅助类
class WorldX
{
}
class GraphicsX
{
public void Draw(Sprite sprite, float x, float y) { }
}
}
# endregion