-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathecs.go
429 lines (335 loc) · 8.43 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
423
424
425
426
427
428
429
package ecs
import (
"strconv"
"sync"
"sync/atomic"
)
type EntityID uint32
func (id EntityID) String() string {
return strconv.Itoa(int(id))
}
type ComponentID uint32
type Tag struct {
flags uint64 // limited to 64 components
inverse bool
}
func (tag Tag) matches(smallertag Tag) bool {
res := tag.flags&smallertag.flags == smallertag.flags
if smallertag.inverse {
return !res
}
return res
}
func (tag *Tag) binaryORInPlace(othertag Tag) *Tag {
tag.flags |= othertag.flags
return tag
}
func (tag *Tag) binaryNOTInPlace(othertag Tag) *Tag {
tag.flags ^= othertag.flags
return tag
}
func (tag Tag) clone() Tag {
return tag
}
func (tag Tag) Inverse(values ...bool) Tag {
clone := tag.clone()
inverse := true
if len(values) > 0 {
inverse = values[0]
}
clone.inverse = inverse
return clone
}
type View struct {
tag Tag
entities QueryResultCollection
lock *sync.RWMutex
}
type QueryResultCollection []*QueryResult
func (coll QueryResultCollection) Entities() []*Entity {
res := make([]*Entity, len(coll))
for i, qr := range coll {
res[i] = qr.Entity
}
return res
}
func (v *View) Get() QueryResultCollection {
v.lock.RLock()
defer v.lock.RUnlock()
return v.entities
}
func (v *View) add(entity *Entity) {
v.lock.Lock()
v.entities = append(v.entities, entity.manager.GetEntityByID(
entity.ID,
v.tag,
))
v.lock.Unlock()
}
func (v *View) remove(entity *Entity) {
v.lock.RLock()
for i, qr := range v.entities {
if qr.Entity.ID == entity.ID {
maxbound := len(v.entities) - 1
v.entities[maxbound], v.entities[i] = v.entities[i], v.entities[maxbound]
v.entities = v.entities[:maxbound]
break
}
}
v.lock.RUnlock()
}
type Manager struct {
lock *sync.RWMutex
entityIdInc uint32
componentNumInc uint32 // limited to 64
entities []*Entity
entitiesByID map[EntityID]*Entity
components []*Component
views []*View
}
type Component struct {
id ComponentID
tag Tag
datalock *sync.RWMutex
data map[EntityID]interface{}
destructor func(entity *Entity, data interface{})
}
func (component *Component) SetDestructor(destructor func(entity *Entity, data interface{})) {
component.destructor = destructor
}
func (component *Component) GetID() ComponentID {
return component.id
}
func (manager *Manager) CreateView(tagelements ...interface{}) *View {
tag := BuildTag(tagelements...)
view := &View{
tag: tag,
lock: &sync.RWMutex{},
}
entities := manager.Query(tag)
view.entities = make(QueryResultCollection, len(entities))
manager.lock.Lock()
for i, entityresult := range entities {
view.entities[i] = entityresult
}
manager.views = append(manager.views, view)
manager.lock.Unlock()
return view
}
type Entity struct {
ID EntityID
tag Tag
manager *Manager
}
func (entity *Entity) GetID() EntityID {
return entity.ID
}
func NewManager() *Manager {
return &Manager{
entityIdInc: 0,
componentNumInc: 0,
entitiesByID: make(map[EntityID]*Entity),
lock: &sync.RWMutex{},
views: make([]*View, 0),
}
}
func BuildTag(elements ...interface{}) Tag {
tag := Tag{}
for _, element := range elements {
switch typedelement := element.(type) {
case *Component:
{
tag.binaryORInPlace(typedelement.tag)
}
case Tag:
{
tag.binaryORInPlace(typedelement)
}
default:
{
panic("Invalid type passed to BuildTag; accepts only <*Component> and <Tag> types.")
}
}
}
return tag
}
func (manager *Manager) NewEntity() *Entity {
nextid := ComponentID(atomic.AddUint32(&manager.componentNumInc, 1))
id := nextid - 1 // to start at 0
entity := &Entity{
ID: EntityID(id),
manager: manager,
}
manager.lock.Lock()
manager.entities = append(manager.entities, entity)
manager.entitiesByID[entity.ID] = entity
manager.lock.Unlock()
return entity
}
func (manager *Manager) NewComponent() *Component {
if manager.componentNumInc >= 63 {
panic("Component overflow (limited to 64)")
}
nextid := ComponentID(atomic.AddUint32(&manager.componentNumInc, 1))
id := nextid - 1 // to start at 0
tag := Tag{
flags: (1 << id), // set bit on position corresponding to component number
inverse: false,
}
component := &Component{
id: id,
tag: tag,
data: make(map[EntityID]interface{}),
datalock: &sync.RWMutex{},
}
manager.lock.Lock()
manager.components = append(manager.components, component)
manager.lock.Unlock()
return component
}
func (manager *Manager) GetEntityByID(id EntityID, tagelements ...interface{}) *QueryResult {
manager.lock.RLock()
res, ok := manager.entitiesByID[id]
if !ok {
manager.lock.RUnlock()
return nil
}
tag := BuildTag(tagelements...)
components := manager.fetchComponentsForEntity(res, tag)
manager.lock.RUnlock()
if components == nil {
return nil
}
return &QueryResult{
Entity: res,
Components: components,
}
}
func (entity Entity) Matches(tag Tag) bool {
return entity.tag.matches(tag)
}
func (entity *Entity) AddComponent(component *Component, componentdata interface{}) *Entity {
component.datalock.Lock()
component.data[entity.ID] = componentdata
component.datalock.Unlock()
component.datalock.RLock()
tagbefore := entity.tag
entity.tag.binaryORInPlace(component.tag)
for _, view := range entity.manager.views {
if !tagbefore.matches(view.tag) && entity.tag.matches(view.tag) {
view.add(entity)
}
}
component.datalock.RUnlock()
return entity
}
func (entity *Entity) RemoveComponent(component *Component) *Entity {
if component.destructor != nil {
if data, ok := component.data[entity.ID]; ok {
component.destructor(entity, data)
}
}
component.datalock.Lock()
delete(component.data, entity.ID)
tagbefore := entity.tag
entity.tag.binaryNOTInPlace(component.tag)
for _, view := range entity.manager.views {
if tagbefore.matches(view.tag) && !entity.tag.matches(view.tag) {
view.remove(entity)
}
}
component.datalock.Unlock()
return entity
}
func (entity Entity) HasComponent(component *Component) bool {
return entity.tag.matches(component.tag)
}
func (entity Entity) GetComponentData(component *Component) (interface{}, bool) {
component.datalock.RLock()
data, ok := component.data[entity.ID]
component.datalock.RUnlock()
return data, ok
}
func (manager *Manager) DisposeEntities(entities ...*Entity) {
for _, entity := range entities {
manager.DisposeEntity(entity)
}
}
func (manager *Manager) DisposeEntity(entity interface{}) {
var typedentity *Entity
switch typeditem := entity.(type) {
case *QueryResult:
{
typedentity = typeditem.Entity
}
case QueryResult:
{
typedentity = typeditem.Entity
}
case *Entity:
{
typedentity = typeditem
}
default:
{
panic("Invalid type passed to DisposeEntity; accepts only <*QueryResult>, <QueryResult> and <*Entity> types.")
}
}
if typedentity == nil {
return
}
manager.lock.Lock()
for _, component := range manager.components {
if typedentity.HasComponent(component) {
typedentity.RemoveComponent(component)
}
}
delete(manager.entitiesByID, typedentity.ID)
manager.lock.Unlock()
}
type QueryResult struct {
Entity *Entity
Components map[*Component]interface{}
}
func (manager *Manager) fetchComponentsForEntity(entity *Entity, tag Tag) map[*Component]interface{} {
if !entity.tag.matches(tag) {
return nil
}
componentMap := make(map[*Component]interface{})
for _, component := range manager.components {
if tag.matches(component.tag) {
data, ok := entity.GetComponentData(component)
if !ok {
return nil // if one of the required components is not set, return nothing !
}
componentMap[component] = data
}
// fmt.Printf("-------------\n")
// fmt.Printf("%16b : %s\n", int64(tag), "tag")
// fmt.Printf("%16b : %s\n", int64(component.tag), "component.tag")
// fmt.Printf("%16b : %s\n", int64(entity.tag), "entity.tag")
// fmt.Printf("//////////////////\n")
}
return componentMap
}
func (manager *Manager) Query(tag Tag) QueryResultCollection {
matches := make(QueryResultCollection, 0)
manager.lock.RLock()
for _, entity := range manager.entities {
if entity.tag.matches(tag) {
componentMap := make(map[*Component]interface{})
for _, component := range manager.components {
if tag.matches(component.tag) {
data, _ := entity.GetComponentData(component)
componentMap[component] = data
}
}
matches = append(matches, &QueryResult{
Entity: entity,
Components: componentMap,
})
}
}
manager.lock.RUnlock()
return matches
}