This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
World.hx
295 lines (250 loc) · 7.24 KB
/
World.hx
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
package ecx;
import ecx.macro.MacroUtil;
import ecx.types.EntityVector;
import ecx.types.ComponentTable;
import ecx.ds.CArrayIterator;
import ecx.ds.CInt32RingBuffer;
import ecx.managers.WorldConstructor;
import ecx.types.FamilyData;
import ecx.types.EntityData;
import ecx.ds.CArray;
import ecx.ds.Cast;
import ecx.ds.CBitArray;
import ecx.macro.ClassMacroTools;
import haxe.macro.Expr.ExprOf;
#if ecx_debug
using ecx.managers.WorldDebug;
#end
@:final @:dce @:unreflective
@:access(ecx.System, ecx.Family, ecx.Entity)
class World {
// Mapping: (type, entity) => component
public var components(default, null):ComponentTable;
// Identifier of this world
public var id(default, null):Int;
/**
Maximum amount of entities including invalid/reserved.
Capacity will be rounded to power-of-two value plus one.
For requested capacity 3 will be allocated:
capacity = nearestPOT(3 - 1) + 1 = 3.
For capacity = 5 we have set of valid entities {1, 2, 3, 4} and one invalid is 0
**/
public var capacity(default, null):Int;
public var used(default, null):Int = 0;
public var available(get, never):Int;
// lookup for all systems
var _services:CArray<Service>;
// all systems sorted by priority
var _orderedServices:CArray<Service>;
// only active systems (not idle)
var _systems:CArray<System>;
// systems with families
var _processors:CArray<System>;
var _families:CArray<FamilyData>;
var _changeList:Array<Entity> = [];
var _removeList:Array<Entity> = [];
var _pool:CInt32RingBuffer;
// entity => entity wrapper
var _mapToData:CArray<EntityData>;
// Flags
var _aliveMask:CBitArray;
var _activeFlags:CBitArray;
var _changedFlags:CBitArray;
var _removedFlags:CBitArray;
function new(id:Int, config:WorldConfig, capacity:Int) {
this.id = id;
WorldConstructor.construct(this, capacity, config);
}
macro public function resolve<T:Service>(self:ExprOf<World>, serviceClass:ExprOf<Class<T>>):ExprOf<T> {
var serviceType = ClassMacroTools.serviceType(serviceClass);
return macro {
var tmp = @:privateAccess $self._services[$serviceType.id];
ecx.ds.Cast.unsafe(tmp, $serviceClass);
};
}
public function create():Entity {
var entity = allocNextEntity();
_aliveMask.enable(entity.id);
_activeFlags.enable(entity.id);
return entity;
}
/** Useful for prefabs **/
public function createPassive():Entity {
var entity = allocNextEntity();
_aliveMask.enable(entity.id);
return entity;
}
public function clone(source:Entity):Entity {
var entity = create();
var componentsByType = components;
for(typeId in 0...componentsByType.length) {
componentsByType[typeId].copy(source, entity);
}
return entity;
}
public function delete(entity:Entity) {
#if ecx_debug
guardEntity(entity);
#end
if(_removedFlags.enableIfNot(entity.id)) {
_removeList.push(entity);
}
}
public function invalidate() {
#if ecx_debug
lockFamilies();
#end
if(_removeList.length > 0 || _changeList.length > 0) {
deleteEntities(_removeList);
changeEntities(_changeList);
for(family in _families) {
var entities:EntityVector = family.entities;
if(entities.changed) {
entities.invalidate();
}
}
}
#if ecx_debug
guardFamilies();
unlockFamilies();
#end
}
public function activate(entity:Entity) {
#if ecx_debug
guardEntity(entity);
if(_activeFlags.get(entity.id)) throw 'This entity is already active';
#end
_activeFlags.enable(entity.id);
_internal_entityChanged(entity);
}
public function deactivate(entity:Entity) {
#if ecx_debug
guardEntity(entity);
if(!_activeFlags.get(entity.id)) throw "This entity is already inactive";
#end
_activeFlags.disable(entity.id);
_internal_entityChanged(entity);
}
// macro public function componentArray<T:(Component<Dynamic>, Service)>(self:ExprOf<World>, componentClass:ExprOf<Class<T>>):ExprOf<T> {
// return macro cast @:privateAccess $self._services[componentClass.__TYPE.id];
// }
inline public function getEntity(id:Int):Entity {
return @:privateAccess new Entity(id);
}
inline public function isActive(entity:Entity):Bool {
return _activeFlags.get(entity.id);
}
inline public function checkAlive(entity:Entity):Bool {
return _aliveMask.get(entity.id);
}
public function toString():String {
return 'World #$id';
}
// inline public function hasComponent(entity:Entity, type:ComponentType):Bool {
// return components[type.id].has(entity);
// }
//
// public function removeComponent(entity:Entity, type:ComponentType) {
// var entityToComponent:Component<Dynamic> = components[type.id];
// entityToComponent.remove(entity);
// if(isActive(entity)) {
// _internal_entityChanged(entity);
// }
// }
public function clearComponents(entity:Entity) {
var componentsData = components;
for(typeId in 0...componentsData.length) {
componentsData[typeId].remove(entity);
}
if(isActive(entity)) {
_internal_entityChanged(entity);
}
}
inline public function commit(entity:Entity) {
if(isActive(entity)) {
_internal_entityChanged(entity);
}
}
/** Iterator for *active* systems ordered by priority **/
inline public function systems():CArrayIterator<System> {
return new CArrayIterator<System>(_systems);
}
function allocNextEntity():Entity {
#if ecx_debug
if(used >= capacity) throw 'Out of entities, max allowed $capacity';
#end
++used;
return new Entity(_pool.pop());
}
inline function get_available():Int {
return capacity - used;
}
function deleteEntities(entities:Array<Entity>) {
var locPool:CInt32RingBuffer = _pool;
var locRemovedFlags = _removedFlags;
var locActiveFlags = _activeFlags;
var locAliveMask = _aliveMask;
var families = _families;
var i = 0;
while(i < entities.length) {
var tail = entities.length;
while(i < tail) {
var entity = entities[i];
// Need to remove entities from families before deletion and notify systems
for(j in 0...families.length) {
@:privateAccess families.get(j)._internal_entityChanged(entity, false);
}
clearComponents(entity);
locActiveFlags.disable(entity.id);
locAliveMask.disable(entity.id);
locRemovedFlags.disable(entity.id);
locPool.push(entity.id);
++i;
}
}
var count = entities.length;
if(count > 0) {
used -= count;
#if ecx_debug
if(used < 0) throw "No way!";
#end
//if(startLength != removeList.length) throw "removing while removing";
entities.splice(0, count);
}
}
function changeEntities(entities:Array<Entity>) {
var changedFlags = _changedFlags;
var activeFlags = _activeFlags;
var aliveMask = _aliveMask;
var families = _families;
var startLength = entities.length;
if(entities.length > 0) {
var i = 0;
var end = entities.length;
while (i < end) {
var entity = entities[i];
var alive = aliveMask.get(entity.id);
if(alive) {
var active = activeFlags.get(entity.id);
for(j in 0...families.length) {
@:privateAccess families.get(j)._internal_entityChanged(entity, active);
}
}
changedFlags.disable(entity.id);
++i;
}
#if ecx_debug
if(startLength != entities.length) throw "update while updating";
#end
entities.splice(0, end);
}
}
function _internal_entityChanged(entity:Entity) {
#if ecx_debug
guardEntity(entity);
#end
if(_changedFlags.enableIfNot(entity.id)) {
_changeList.push(entity);
}
}
}