-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.dart
296 lines (242 loc) · 8.28 KB
/
Entity.dart
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
import 'dart:collection';
/// Base class for registering callbacks.
class Listenable {
List<void Function()> _listeners = [];
void add(void Function() f) => _listeners.add(f);
void remove(void Function() f) => _listeners.remove(f);
bool get hasListeners => _listeners.isNotEmpty;
void dispose() => _listeners.clear();
void notify() {
for (void Function() f in _listeners) {
f();
}
}
}
class ListenableValue<T> extends Listenable {
T _value;
ListenableValue(T value) : _value = value;
String toString() => value.toString();
T get value => _value;
set value(T newValue) {
if (newValue != _value) {
_value = newValue;
notify();
}
}
}
/// Call [update] and supply the time spent between runs and return the time until next cycle.
Future<void> aloop(int Function(int) update) async {
Stopwatch sw = Stopwatch()..start();
int d = 0;
while (true) {
d = update(sw.elapsedMilliseconds);
if (d == -1) break;
sw.reset();
await Future.delayed(Duration(milliseconds: d));
}
}
/// Simple component to add an arbitrary string to an entity.
class Tag {
String tag;
Tag([this.tag = '']);
Tag.fromJson(Map<String, dynamic> json) : tag = json['tag'];
Map<String, dynamic> toJson() => {'tag': tag};
String toString() => 'Tag(tag: $tag)';
}
/// Simple Entity Component System.
class ECS {
int entityIndex = -1;
List<int> freeEntities = [];
HashMap<int, Set<Type>> entitySignature = HashMap();
int get entitiesAlive => entitySignature.length;
HashMap<Type, ComponentArray> components = HashMap();
List<System> systems = [];
/// Creates and adds [components] to related ComponentArrays and systems.
int createEntity([List<dynamic> components = const <dynamic>[]]) {
int e = freeEntities.isEmpty ? ++entityIndex : freeEntities.removeAt(0);
if (components.isNotEmpty) {
Set<Type> signature = {for (var c in components) c.runtimeType};
entitySignature[e] = signature;
for (var c in components) {
assert(this.components.keys.contains(c.runtimeType),
'Component ${c.runtimeType} not registered');
(this.components[c.runtimeType])!.add(e, c);
}
for (System s in systems) {
if (signature.containsAll(s.signature)) s.entities.add(e);
}
}
return e;
}
/// Remove entity from arrays and systems.
void disposeEntity(int e) {
for (Type T in entitySignature[e]!) {
this.components[T]!.remove(e);
}
for (System s in systems) {
s.entities.remove(e);
}
entitySignature.remove(e);
freeEntities.add(e);
}
// ---
/// Add [ComponentArray].
void addComponent<T>() => components[T] = ComponentArray<T>();
/// Add a single [T] component to [entity].
void addEntityComponent<T>(int entity, T component) {
Set<Type> signature = entitySignature[entity]!;
signature.add(T);
updateSignature(entity, signature);
components[T]!.add(entity, component);
}
/// Remove component from array.
T removeEntityComponent<T>(int entity) {
Set<Type> signature = entitySignature[entity]!;
signature.remove(T);
updateSignature(entity, signature);
return components[T]!.remove(entity);
}
/// Get single component for entity.
T getEntityComponent<T>(int entity) => components[T]!.get(entity);
/// Same as [getEntityComponent].
T call<T>(int entity) => components[T]!.get(entity);
/// Reverse lookup an [entity] by a [component].
int getEntityByComponent<T>(T component) {
return components[T]!
.indexToEntity[components[T]!.items.indexOf(component)] ??
-1;
}
/// Get all components of entity by [component].
List<dynamic> getEntityComponentsByComponent<T>(T component) =>
getEntityComponents(getEntityByComponent(component));
/// Get list of components for [entity].
List<dynamic> getEntityComponents(int entity) => <dynamic>[
for (Type T in entitySignature[entity]!) components[T]!.get(entity)
];
// ---
/// Add system for [signature] typed entities.
///
/// If no function is supplied, then the system is disabled and just acts as a list.
/// Returning the list directly can allow for a faster loop to be written outside of the [ECS.update].
List<int> addSystem(Set<Type> signature, [void Function(List<int>)? f]) {
System s = System(signature, f);
systems.add(s);
return s.entities;
}
/// Call all system updates to entities.
void update() {
for (System s in systems) {
if (s.f != null) {
s.f!(s.entities);
}
}
}
/// Update [signature] for [entity].
///
/// This will also add/remove entities from the relevant systems too.
void updateSignature(int entity, Set<Type> signature) {
for (System s in systems) {
if (signature.containsAll(s.signature)) {
if (!s.entities.contains(entity)) {
s.entities.add(entity);
}
} else {
s.entities.remove(entity);
}
}
}
void load<T>(Map<String, dynamic> json, Function f) {
components[T]!.fromJson(json, f);
for (var i in components[T]!.entityToIndex.keys) {
(entitySignature[i] ??= {}).add(T);
}
}
//Map<String, dynamic> toJson<T>() => components[T]!.toJson();
//ECS entityIndex freeEntities components
void fromJson(Map<String, dynamic> json) {
entityIndex = json['entityIndex'];
freeEntities = json['freeEntities'].cast<int>();
}
Map<String, dynamic> toJson() => {
'entityIndex': entityIndex,
'freeEntities': freeEntities,
'components': {
for (var i in components.entries) i.key.toString(): i.value.toJson()
}
};
void resyncSystems() {
for (var i in systems) {
i.entities.clear();
}
for (var e in entitySignature.entries) {
updateSignature(e.key, e.value);
}
}
String toString() =>
'ECS(\n entitiesAlive: $entitiesAlive, \n components: $components, \n systems: $systems\n)';
}
/// Struct for systems.
class System {
Set<Type> signature;
void Function(List<int>)? f;
List<int> entities = [];
System(this.signature, this.f);
String toString() =>
'System(\n f: $f, \n signature: $signature, \n size: ${entities.length}\n )';
}
class ComponentArray<T> {
int get size => items.length;
/// What is in the array.
List<T> items = [];
/// Bidirectional mapping to look up specific instances of components.
HashMap<int, int> entityToIndex = HashMap();
HashMap<int, int> indexToEntity = HashMap();
/// Add a component and associate it with an Entity ID.
void add(int entity, T component) {
int newIndex = size;
entityToIndex[entity] = newIndex;
indexToEntity[newIndex] = entity;
//items[newIndex] = component;
items.add(component);
}
/// Remove and re-pack component array.
///
/// This actually takes the last item in the array, moves it to the deleted item slot,
/// then updates the index and entity maps.
T remove(int entity) {
// Copy element at end into deleted element's place to maintain density
int indexOfRemovedEntity = entityToIndex[entity]!;
int indexOfLastElement = size - 1;
T temp = items[indexOfRemovedEntity];
items[indexOfRemovedEntity] = items[indexOfLastElement];
// Update map to point to moved spot
int entityOfLastElement = indexToEntity[indexOfLastElement]!;
entityToIndex[entityOfLastElement] = indexOfRemovedEntity;
indexToEntity[indexOfRemovedEntity] = entityOfLastElement;
entityToIndex.remove(entity);
indexToEntity.remove(indexOfLastElement);
items.removeAt(indexOfLastElement);
return temp;
}
/// Get data by Entity ID.
T get(int entity) => items[entityToIndex[entity]!];
String toString() => '\n ComponentArray<$T>(size: $size)\n ';
void fromJson(Map<String, dynamic> json, Function f) {
items = <T>[for (var i in json['items']) f(i)];
indexToEntity = HashMap.from({
for (var e in json['indexToEntity'].entries) int.parse(e.key): e.value
});
entityToIndex = HashMap.from({
for (var e in json['entityToIndex'].entries) int.parse(e.key): e.value
});
}
Map<String, dynamic> toJson() => {
'items': items,
'entityToIndex': {
for (var e in entityToIndex.entries) e.key.toString(): e.value
},
'indexToEntity': {
for (var e in indexToEntity.entries) e.key.toString(): e.value
}
};
}