-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameObjectSystem.pde
331 lines (269 loc) · 8.45 KB
/
GameObjectSystem.pde
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//=========================================================================================================
// Author: David Hanna
//
// An object which is an entity in the game world.
//=========================================================================================================
// BIG TO DO NOTE: Need to implement a destroy function to have Game Object components
// remove themselves from the Physics World and the Event Manager.
//---------------------------------------------------------------
// INTERFACE
//---------------------------------------------------------------
interface IGameObject
{
// Must be called before going out of scope.
public void destroy();
// This should be called immediately after creating a Game Object to load its data from a GameObject XML file.
public void fromXML(String fileName);
// Every instantiated Game Object has a unique ID.
public int getUID();
// A Game Object can have a tag which may be used for various purposes. e.g. if (getTag() == "player") { ... }
public String getTag();
public void setTag(String _tag);
// Transform values.
public PVector getTranslation();
public PVector getScale();
// WARNING: Avoid these methods if the GameObject has a RigidBody component.
// Use the RigidBody instead. JBox2D can not scale RigidBodies or Fixtures unfortunately.
public void translate(PVector vector);
public void scale(PVector vector);
public void setTranslation(PVector vector);
public void setScale(PVector vector);
// Find a component attached to this GameObject. Returns null if not found.
// Note: GameObjects are limited to having only one component of each type.
public IComponent getComponent(ComponentType componentType);
public ArrayList<IComponent> getComponents(ComponentType componentType);
// Updates and renders the Game Object over the given time in milliseconds.
public void update(int deltaTime);
}
// This is basically a convenience container class for GameObjects that can load levels,
// provide convenience functions for loading new GameObjects and clearing a level, etc.
interface IGameObjectManager
{
// Creates a level full of GameObjects based on a Level XML file.
public void fromXML(String fileName);
public void update(int deltaTime);
public void addGameObject(IGameObject gameObject);
public IGameObject addGameObject(String fileName, PVector translation, PVector scale);
public IGameObject getGameObject(int UID);
public ArrayList<IGameObject> getGameObjectsByTag(String tag);
public void removeGameObject(int UID);
public void clearGameObjects();
}
//---------------------------------------------------------------
// IMPLEMENTATION
//---------------------------------------------------------------
// Increments such that every GameObject has a unique ID.
int gameObjectNextUID = 0;
class GameObject implements IGameObject
{
private int UID;
private String tag;
private PVector translation;
private PVector scale;
private ArrayList<IComponent> components;
public GameObject(PVector _translation, PVector _scale)
{
UID = gameObjectNextUID;
gameObjectNextUID++;
tag = "";
translation = _translation;
scale = _scale;
components = new ArrayList<IComponent>();
}
@Override public void destroy()
{
for (IComponent component : components)
{
component.destroy();
}
}
@Override public void fromXML(String fileName)
{
XML xmlGameObject = loadXML(fileName);
assert(xmlGameObject.getName().equals("GameObject"));
for (XML xmlComponent : xmlGameObject.getChildren())
{
IComponent component = componentFactory(this, xmlComponent);
if (component != null)
{
components.add(component);
}
}
}
@Override public int getUID()
{
return UID;
}
@Override public String getTag()
{
return tag;
}
@Override public void setTag(String _tag)
{
tag = _tag;
}
@Override public PVector getTranslation()
{
return translation;
}
@Override public PVector getScale()
{
return scale;
}
@Override public void translate(PVector vector)
{
translation.add(vector);
}
@Override public void scale(PVector vector)
{
scale.add(vector);
}
@Override public void setTranslation(PVector vector)
{
translation = vector;
}
@Override public void setScale(PVector vector)
{
scale = vector;
}
@Override public IComponent getComponent(ComponentType componentType)
{
for (IComponent component : components)
{
if (component.getComponentType() == componentType)
{
return component;
}
}
return null;
}
@Override public ArrayList<IComponent> getComponents(ComponentType componentType)
{
ArrayList<IComponent> list = new ArrayList<IComponent>();
for (IComponent component : components)
{
if (component.getComponentType() == componentType)
{
list.add(component);
}
}
return list;
}
@Override public void update(int deltaTime)
{
for (IComponent component : components)
{
component.update(deltaTime);
}
}
}
class GameObjectManager implements IGameObjectManager
{
private HashMap<Integer, IGameObject> gameObjects;
private ArrayList<IGameObject> addList;
private ArrayList<Integer> removeList;
public GameObjectManager()
{
gameObjects = new HashMap<Integer, IGameObject>();
addList = new ArrayList<IGameObject>();
removeList = new ArrayList<Integer>();
}
@Override public void fromXML(String fileName)
{
XML xmlLevel = loadXML(fileName);
assert(xmlLevel.getName().equals("Level"));
for (XML xmlGameObject : xmlLevel.getChildren("GameObject"))
{
PVector translation = new PVector(0.0f, 0.0f);
PVector scale = new PVector(1.0f, 1.0f);
for (XML xmlTransform : xmlGameObject.getChildren("Transform"))
{
for (XML xmlTranslation : xmlTransform.getChildren("Translation"))
{
translation.x = xmlTranslation.getFloat("x");
translation.y = xmlTranslation.getFloat("y");
}
for (XML xmlScale : xmlTransform.getChildren("Scale"))
{
scale.x = xmlScale.getFloat("x");
scale.y = xmlScale.getFloat("y");
}
}
IGameObject gameObject = new GameObject(translation, scale);
String tag = xmlGameObject.getString("tag");
if (tag != null)
{
gameObject.setTag(tag);
}
gameObject.fromXML(xmlGameObject.getString("file"));
addGameObject(gameObject);
}
}
@Override public void update(int deltaTime)
{
for (Map.Entry entry : gameObjects.entrySet())
{
IGameObject gameObject = (IGameObject)entry.getValue();
gameObject.update(deltaTime);
}
for (IGameObject gameObject : addList)
{
gameObjects.put(gameObject.getUID(), gameObject);
}
for (Integer UID : removeList)
{
IGameObject gameObject = gameObjects.remove(UID);
if (gameObject != null)
{
gameObject.destroy();
}
}
addList.clear();
removeList.clear();
}
@Override public void addGameObject(IGameObject gameObject)
{
addList.add(gameObject);
}
@Override public IGameObject addGameObject(String fileName, PVector translation, PVector scale)
{
IGameObject gameObject = new GameObject(translation, scale);
gameObject.fromXML(fileName);
addList.add(gameObject);
return gameObject;
}
@Override public IGameObject getGameObject(int UID)
{
if (gameObjects.containsKey(UID))
{
return gameObjects.get(UID);
}
return null;
}
@Override public ArrayList<IGameObject> getGameObjectsByTag(String tag)
{
ArrayList<IGameObject> gameObjectsByTag = new ArrayList<IGameObject>();
for (Map.Entry entry : gameObjects.entrySet())
{
IGameObject gameObject = (IGameObject)entry.getValue();
if (gameObject.getTag().equals(tag))
{
gameObjectsByTag.add(gameObject);
}
}
return gameObjectsByTag;
}
@Override public void removeGameObject(int UID)
{
removeList.add(UID);
}
@Override public void clearGameObjects()
{
for (Map.Entry entry : gameObjects.entrySet())
{
IGameObject gameObject = (IGameObject)entry.getValue();
gameObject.destroy();
}
gameObjects.clear();
}
}