-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWorld.cs
More file actions
365 lines (324 loc) · 8.22 KB
/
World.cs
File metadata and controls
365 lines (324 loc) · 8.22 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using Microsoft.Xna.Framework;
using System.Collections;
using System.Collections.Generic;
namespace Dabrorius.MonoPunk
{
public class World
{
public Vector2 Camera = new Vector2(0,0);
public Boolean Active;
public World ()
{
Active = true;
}
public virtual void Begin()
{
// to override
}
public virtual void End()
{
// to override
}
/**
* Performed by the game loop, renders all contained Entities.
* If you override this to give your World render code, remember
* to call super.render() or your Entities will not be rendered.
*/
public virtual void Render()
{
// render the entities in order of depth
Entity e;
int i = layerList.Count;
while (i > 0)
{
i--;
e = renderLast[layerList[i]] as Entity;
while (e != null)
{
if (e.Visible) e.Render();
e = e.renderPrev;
}
}
}
/**
* Performed by the game loop, updates all contained Entities.
* If you override this to give your World update code, remember
* to call super.update() or your Entities will not be updated.
*/
virtual public void Update()
{
// update the entities
Entity e = updateFirst;
while (e != null)
{
if (e.Active)
{
//if (e._tween) e.updateTweens();
e.Update();
}
if (e.graphic != null /*&& e.graphic.active*/) e.graphic.Update();
e = e.updateNext;
}
}
/**
* Adds the Entity to the World at the end of the frame.
* @param e Entity object you want to add.
* @return The added Entity object.
*/
public Entity Add(Entity e)
{
toAdd.Add(e);
return e;
}
/**
* Adds an Entity to the World with the Graphic object.
* @param graphic Graphic to assign the Entity.
* @param x X position of the Entity.
* @param y Y position of the Entity.
* @param layer Layer of the Entity.
* @return The Entity that was added.
*/
public Entity AddGraphic(Graphic graphic, int layer = 0, int x = 0, int y = 0)
{
Entity e = new Entity(x, y, graphic);
if (layer != 0) e.layer = layer;
//e.active = false;
return Add(e);
}
/**
* Removes the Entity from the World at the end of the frame.
* @param e Entity object you want to remove.
* @return The removed Entity object.
*/
public Entity Remove(Entity e)
{
toRemove.Add (e);
return e;
}
/**
* Removes all Entities from the World at the end of the frame.
*/
public void RemoveAll()
{
Entity e = updateFirst;
while (e != null)
{
toRemove.Add(e);
e = e.updateNext;
}
}
/**
* Updates the add/remove lists at the end of the frame.
*/
public void UpdateLists()
{
// remove entities
if (toRemove.Count > 0)
{
foreach (Entity e in toRemove)
{
if (e.world == null)
{
if( toAdd.Contains(e) )
{
toAdd.Remove(e);
}
continue;
}
if (e.world != this)
continue;
e.Removed();
e.world = null;
removeUpdate(e);
removeRender(e);
if (e.type != null) removeType(e);
//if (e._name) unregisterName(e);
//if (e.autoClear && e._tween) e.clearTweens();
}
toRemove.Clear();
}
// add entities
if (toAdd.Count > 0)
{
foreach (Entity e in toAdd)
{
if (e.world != null) continue;
addUpdate(e);
addRender(e);
if (e.type != null) addType(e);
//if (e._name) registerName(e);
e.world = this;
e.Added();
}
toAdd.Clear();
}
// recycle entities
/*
if (_recycle.length)
{
for each (e in _recycle)
{
if (e._world || e._recycleNext)
continue;
e._recycleNext = _recycled[e._class];
_recycled[e._class] = e;
}
_recycle.length = 0;
}
*/
// sort the depth list
if (layerSort)
{
if (layerList.Count > 1) layerList.Sort();
layerSort = false;
}
}
/** @private Adds Entity to the render list. */
internal void addRender(Entity e)
{
if (renderFirst.ContainsKey(e.layer) && renderFirst[e.layer] != null)
{
Entity f = renderFirst[e.layer] as Entity;
// Append entity to existing layer.
e.renderNext = f;
f.renderPrev = e;
layerCount[e.layer]++;
}
else
{
// Create new layer with entity.
renderLast[e.layer] = e;
layerList.Add(e.layer);
layerSort = true;
e.renderNext = null;
layerCount[e.layer] = 1;
}
renderFirst[e.layer] = e;
e.renderPrev = null;
}
/** @private Removes Entity from the render list. */
internal void removeRender(Entity e)
{
if (e.renderNext != null) e.renderNext.renderPrev = e.renderPrev;
else renderLast[e.layer] = e.renderPrev;
if (e.renderPrev != null) e.renderPrev.renderNext = e.renderNext;
else
{
// Remove this entity from the layer.
renderFirst[e.layer] = e.renderNext;
if (e.renderNext == null)
{
// Remove the layer from the layer list if this was the last entity.
if (layerList.Count > 1)
{
layerList.Remove(e.Layer);
layerSort = true;
}
}
}
layerCount[e.layer] --;
e.renderNext = e.renderPrev = null;
}
/** @private Adds Entity to the update list. */
private void addUpdate(Entity e)
{
// add to update list
if (updateFirst != null)
{
updateFirst.updatePrev = e;
e.updateNext = updateFirst;
}
else e.updateNext = null;
e.updatePrev = null;
updateFirst = e;
count ++;
/* TO-DO class count*/
}
/** @private Removes Entity from the update list. */
private void removeUpdate(Entity e)
{
// remove from the update list
if (updateFirst == e) updateFirst = e.updateNext;
if (e.updateNext != null) e.updateNext.updatePrev = e.updatePrev;
if (e.updatePrev != null) e.updatePrev.updateNext = e.updateNext;
e.updateNext = e.updatePrev = null;
count --;
//classCount[e.class] --;
}
/** @private Adds Entity to the type list. */
internal void addType(Entity e)
{
// add to type list
if( typeFirst.ContainsKey(e.type) && typeFirst[e.type] != null )
{
typeFirst[e.type].typePrev = e;
e.typeNext = typeFirst[e.type];
typeCount[e.type]++;
}
else
{
e.typeNext = null;
typeCount[e.type] = 1;
}
e.typePrev = null;
typeFirst[e.type] = e;
}
/** @private Removes Entity from the type list. */
internal void removeType(Entity e)
{
// remove from the type list
if (typeFirst[e.type] == e) typeFirst[e.type] = e.typeNext;
if (e.typeNext != null) e.typeNext.typePrev = e.typePrev;
if (e.typePrev != null) e.typePrev.typeNext = e.typeNext;
e.typeNext = e.typePrev = null;
typeCount[e.type] --;
}
/**
* Returns the amount of Entities of the type are in the World.
* @param type The type (or Class type) to count.
* @return How many Entities of type exist in the World.
*/
public int TypeCount(String type)
{
if( ! typeCount.ContainsKey(type) ) return 0;
return typeCount[type];
}
/**
* Returns the amount of Entities of the Class are in the World.
* @param c The Class type to count.
* @return How many Entities of Class exist in the World.
*/
/*
public function classCount(c:Class):uint
{
return _classCount[c] as uint;
}
*/
/**
* Returns the amount of Entities are on the layer in the World.
* @param layer The layer to count Entities on.
* @return How many Entities are on the layer.
*/
public int LayerCount(int layer)
{
if( ! layerCount.ContainsKey(layer) ) return 0;
return layerCount[layer];
}
// Adding and removal.
private List<Entity> toAdd = new List<Entity>();
private List<Entity> toRemove = new List<Entity>();
private List<Entity> toRecycle = new List<Entity>();
// Update information.
private Entity updateFirst;
private int count = 0;
// Render information
private Dictionary<int,Entity> renderFirst = new Dictionary<int,Entity>();
private Dictionary<int,Entity> renderLast = new Dictionary<int,Entity>();
private Dictionary<int,int> layerCount = new Dictionary<int,int>();
private List<int> layerList = new List<int>();
internal Dictionary<String,Entity> typeFirst = new Dictionary<String, Entity>();
private Dictionary<String,int> typeCount = new Dictionary<String, int>();
private Boolean layerSort;
}
}