-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs.go
422 lines (358 loc) · 10.9 KB
/
ecs.go
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package ecs
import (
"reflect"
"unsafe"
"github.com/jabolopes/go-sparseset"
)
var (
typeIdGen = 0
typeIds = map[reflect.Type]int{}
)
func getTypeId[T any]() int {
var t T
typ := reflect.TypeOf(t)
typeId, ok := typeIds[typ]
if !ok {
typeId = typeIdGen
typeIds[typ] = typeId
typeIdGen++
}
return typeId
}
type remover interface {
Remove(int)
}
// ECS is the Entity Component System.
//
// Several functions / methods return pointers to components. These pointers are
// only valid as long as the ECS is not modified by adding or removing
// entities. If adding or removing entities, the ECS's internal memory may
// reallocated, thus invaliding those pointers. For this reason, pointers to
// components obtained prior to adding or removing entities should not be
// accessed if the ECS is modified in this way. Also, it's recommended not to
// store pointers to components inside data types for later use because if they
// become invalidated it's easy to forget and access them later.
type ECS struct {
defaultPageSize int
nullKey int
pools map[int]unsafe.Pointer
removers []remover
idGenerator int
}
func getPool[T any](e *ECS) (*sparseset.Set[T], bool) {
set, ok := e.pools[getTypeId[T]()]
if !ok {
return nil, false
}
return (*sparseset.Set[T])(set), true
}
func initPool[T any](e *ECS) *sparseset.Set[T] {
if p, ok := getPool[T](e); ok {
return p
}
var t *T
elemType := reflect.TypeOf(t)
method, ok := elemType.MethodByName("Destroy")
var pool *sparseset.Set[T]
if ok {
options := sparseset.Options[T]{
func(value *T) {
method.Func.Call([]reflect.Value{reflect.ValueOf(value)})
},
}
pool = sparseset.NewWithOptions[T](e.defaultPageSize, e.nullKey, options)
} else {
pool = sparseset.New[T](e.defaultPageSize, e.nullKey)
}
e.pools[getTypeId[T]()] = unsafe.Pointer(pool)
e.removers = append(e.removers, pool)
return pool
}
// Creates a new entity and returns the entity ID.
func (e *ECS) Add() int {
id := e.idGenerator
e.idGenerator++
return id
}
// Removes an entity given its ID and removes all of its components.
func (e *ECS) Remove(entityId int) {
for _, remover := range e.removers {
remover.Remove(entityId)
}
}
// Returns a new instance of the ECS with default options.
func New() *ECS {
return &ECS{
4096, /* defaultPageSize */
1 << 20, /* nullKey */
map[int]unsafe.Pointer{}, /* pools */
nil, /* removers */
0, /* idGenerator */
}
}
// Initializes an entity and its component. If the entity already exists, it is
// first removed and then re-added. If the intention is not to initialize the
// entity, then use 'Set' instead.
func Init[A any](e *ECS, entityId int, a A) {
e.Remove(entityId)
*initPool[A](e).Add(entityId) = a
}
func Init2[A, B any](e *ECS, entityId int, a A, b B) {
e.Remove(entityId)
Set2(e, entityId, a, b)
}
func Init3[A, B, C any](e *ECS, entityId int, a A, b B, c C) {
e.Remove(entityId)
Set3(e, entityId, a, b, c)
}
func Init4[A, B, C, D any](e *ECS, entityId int, a A, b B, c C, d D) {
e.Remove(entityId)
Set4(e, entityId, a, b, c, d)
}
func Init5[A, B, C, D, E any](ecs *ECS, entityId int, a A, b B, c C, d D, e E) {
ecs.Remove(entityId)
Set5(ecs, entityId, a, b, c, d, e)
}
func Init6[A, B, C, D, E, F any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F) {
ecs.Remove(entityId)
Set6(ecs, entityId, a, b, c, d, e, f)
}
func Init7[A, B, C, D, E, F, G any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G) {
ecs.Remove(entityId)
Set7(ecs, entityId, a, b, c, d, e, f, g)
}
func Init8[A, B, C, D, E, F, G, H any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G, h H) {
ecs.Remove(entityId)
Set8(ecs, entityId, a, b, c, d, e, f, g, h)
}
// Returns a component of the given type for an entity given its ID. Returns a
// pointer to the component and true if said entity exists, otherwise it returns
// false.
//
// The pointer is valid as long as the ECS is not modified (see ECS type)
func Get[T any](e *ECS, entityId int) (*T, bool) {
set, ok := getPool[T](e)
if !ok {
return nil, false
}
return set.Get(entityId)
}
// Same as 'Get' for 2 component types. Returns true only if the entity has all
// types.
//
// The pointers are valid as long as the ECS is not modified (see ECS type)
func Get2[A, B any](e *ECS, entityId int) (*A, *B, bool) {
set1, ok := getPool[A](e)
if !ok {
return nil, nil, false
}
set2, ok := getPool[B](e)
if !ok {
return nil, nil, false
}
return sparseset.Lookup(entityId, set1, set2)
}
// Same as 'Get' for 3 component types. Returns true only if the entity has all
// types.
//
// The pointers are valid as long as the ECS is not modified (see ECS type)
func Get3[A, B, C any](e *ECS, entityId int) (*A, *B, *C, bool) {
set1, ok := getPool[A](e)
if !ok {
return nil, nil, nil, false
}
set2, ok := getPool[B](e)
if !ok {
return nil, nil, nil, false
}
set3, ok := getPool[C](e)
if !ok {
return nil, nil, nil, false
}
return sparseset.Lookup3(entityId, set1, set2, set3)
}
// Sets a component for an entity given its ID.
func Set[A any](e *ECS, entityId int, a A) {
*initPool[A](e).Add(entityId) = a
}
// Same as 'Set' for 2 component types.
func Set2[A, B any](ecs *ECS, entityId int, a A, b B) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
}
// Same as 'Set' for 3 component types.
func Set3[A, B, C any](ecs *ECS, entityId int, a A, b B, c C) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
*initPool[C](ecs).Add(entityId) = c
}
// Same as 'Set' for 4 component types.
func Set4[A, B, C, D any](ecs *ECS, entityId int, a A, b B, c C, d D) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
*initPool[C](ecs).Add(entityId) = c
*initPool[D](ecs).Add(entityId) = d
}
// Same as 'Set' for 5 component types.
func Set5[A, B, C, D, E any](ecs *ECS, entityId int, a A, b B, c C, d D, e E) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
*initPool[C](ecs).Add(entityId) = c
*initPool[D](ecs).Add(entityId) = d
*initPool[E](ecs).Add(entityId) = e
}
// Same as 'Set' for 6 component types.
func Set6[A, B, C, D, E, F any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
*initPool[C](ecs).Add(entityId) = c
*initPool[D](ecs).Add(entityId) = d
*initPool[E](ecs).Add(entityId) = e
*initPool[F](ecs).Add(entityId) = f
}
// Same as 'Set' for 7 component types.
func Set7[A, B, C, D, E, F, G any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
*initPool[C](ecs).Add(entityId) = c
*initPool[D](ecs).Add(entityId) = d
*initPool[E](ecs).Add(entityId) = e
*initPool[F](ecs).Add(entityId) = f
*initPool[G](ecs).Add(entityId) = g
}
// Same as 'Set' for 8 component types.
func Set8[A, B, C, D, E, F, G, H any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G, h H) {
*initPool[A](ecs).Add(entityId) = a
*initPool[B](ecs).Add(entityId) = b
*initPool[C](ecs).Add(entityId) = c
*initPool[D](ecs).Add(entityId) = d
*initPool[E](ecs).Add(entityId) = e
*initPool[F](ecs).Add(entityId) = f
*initPool[G](ecs).Add(entityId) = g
*initPool[H](ecs).Add(entityId) = h
}
// Removes a component from an entity given its ID. If the entity already does
// not have said component, then it's a no-op.
func Unset[T any](e *ECS, entityId int) {
set, ok := getPool[T](e)
if !ok {
return
}
set.Remove(entityId)
}
// Returns an iterator that iterates all entities that have the given component
// type.
//
// for iterator := ecs.Iterate[MyComponent](e); ; {
// c, ok := e.Next()
// if !ok {
// break
// }
//
// // Do something with 'c'.
// }
//
// The pointer returned by the iterator is valid as long as the ECS is not
// modified (see ECS type)
func Iterate[A any](e *ECS) *sparseset.Iterator[A] {
set, ok := getPool[A](e)
if !ok {
return sparseset.EmptyIterator[A]()
}
return sparseset.Iterate(set)
}
// Returns an iterator that iterates all entities that have all component types.
//
// for iterator := ecs.Join[MyComponent, OtherComponent](e); ; {
// c1, c2, ok := e.Next()
// if !ok {
// break
// }
//
// // Do something with 'c1' and 'c2'.
// }
//
// The pointers returned by the iterator are valid as long as the ECS is not
// modified (see ECS type)
func Join[A, B any](e *ECS) *sparseset.JoinIterator[A, B] {
set1, ok := getPool[A](e)
if !ok {
return sparseset.EmptyJoinIterator[A, B]()
}
set2, ok := getPool[B](e)
if !ok {
return sparseset.EmptyJoinIterator[A, B]()
}
return sparseset.Join(set1, set2)
}
// Same as 'Join' for 3 component types.
func Join3[A, B, C any](e *ECS) *sparseset.Join3Iterator[A, B, C] {
set1, ok := getPool[A](e)
if !ok {
return sparseset.EmptyJoin3Iterator[A, B, C]()
}
set2, ok := getPool[B](e)
if !ok {
return sparseset.EmptyJoin3Iterator[A, B, C]()
}
set3, ok := getPool[C](e)
if !ok {
return sparseset.EmptyJoin3Iterator[A, B, C]()
}
return sparseset.Join3(set1, set2, set3)
}
// Same as 'Join' for 4 component types.
func Join4[A, B, C, D any](e *ECS) *sparseset.Join4Iterator[A, B, C, D] {
set1, ok := getPool[A](e)
if !ok {
return sparseset.EmptyJoin4Iterator[A, B, C, D]()
}
set2, ok := getPool[B](e)
if !ok {
return sparseset.EmptyJoin4Iterator[A, B, C, D]()
}
set3, ok := getPool[C](e)
if !ok {
return sparseset.EmptyJoin4Iterator[A, B, C, D]()
}
set4, ok := getPool[D](e)
if !ok {
return sparseset.EmptyJoin4Iterator[A, B, C, D]()
}
return sparseset.Join4(set1, set2, set3, set4)
}
// Returns any entity that has the given component. Returns the entity ID, the
// pointer to the component and true if said entity exists, otherwise it returns
// false.
//
// The pointer is valid as long as the ECS is not modified (see ECS type)
func IterateAny[T any](e *ECS) (int, *T, bool) {
iterator := Iterate[T](e)
entityId, t, ok := iterator.Next()
return entityId, t, ok
}
// Returns any entity that has all the given components. Returns the entity ID,
// the pointers to the components and true if said entitiy exists, otherwise it
// returns false. The pointers are valid as long as the ECS is not modified.
//
// The pointers are valid as long as the ECS is not modified (see ECS type)
func JoinAny[A, B any](e *ECS) (int, *A, *B, bool) {
iterator := Join[A, B](e)
entityId, a, b, ok := iterator.Next()
return entityId, a, b, ok
}
func Join3Any[A, B, C any](e *ECS) (int, *A, *B, *C, bool) {
iterator := Join3[A, B, C](e)
entityId, a, b, c, ok := iterator.Next()
return entityId, a, b, c, ok
}
// Sorts the components using a stable sort function according to the given
// comparator function. The comparator function uses the same semantics are
// 'cmp.Compare' from the http://pkg.go.dev/cmp package.
func SortStableFunc[T any](e *ECS, compare func(int, *T, int, *T) int) {
set, ok := getPool[T](e)
if !ok {
return
}
sparseset.SortStableFunc(set, compare)
}