/
registry.go
221 lines (188 loc) · 5.02 KB
/
registry.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
package ecsgo
import (
"context"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
)
type Registry struct {
eg *executionGroup
deferredActions *deferredActions
archeTypeList []*ArcheType
systems []*System
observers []*Observer
entityArcheTypeMap map[EntityId]*ArcheType
duringTick int32
// for issue new id
mx sync.Mutex
lastId uint32
tombstones []EntityId
}
func NewRegistry() *Registry {
r := &Registry{
eg: newExecutionGroup(),
entityArcheTypeMap: make(map[EntityId]*ArcheType),
}
r.deferredActions = newDeferredActions(r)
return r
}
func (r *Registry) CreateEntity() EntityId {
r.mx.Lock()
var entityId EntityId
lastIdx := len(r.tombstones) - 1
if lastIdx >= 0 {
lastId := r.tombstones[lastIdx]
r.tombstones[lastIdx] = EntityId{}
r.tombstones = r.tombstones[:lastIdx]
lastId.version++
entityId = lastId
} else {
r.lastId++
entityId = EntityId{
id: r.lastId,
version: 1,
}
}
// setting archetype, currently it is just for checking issued or not
r.entityArcheTypeMap[entityId] = nil
r.mx.Unlock()
r.deferredActions.createEntity(entityId)
return entityId
}
func (r *Registry) RemoveEntity(entityId EntityId) {
r.deferredActions.removeEntity(entityId)
}
func AddComponent[T any](r *Registry, entityId EntityId, val T) {
addComponentDeferredAction[T](r.deferredActions, entityId, val)
}
func RemoveComponent[T any](r *Registry, entityId EntityId) {
removeComponentDeferredAction[T](r.deferredActions, entityId)
}
func (r *Registry) AddSystem(name string, priority int, fn SystemFn) *System {
s := newSystem(r, name, priority, fn)
r.deferredActions.addSystem(s)
return s
}
func (r *Registry) AddObserver(name string, fn ObserverFunc) *Observer {
o := newObserver(r, name, fn)
r.deferredActions.addObserver(o)
return o
}
func (r *Registry) IsActiveEntity(entityId EntityId) bool {
_, found := r.entityArcheTypeMap[entityId]
return found
}
func (r *Registry) Tick(deltaTime time.Duration, ctx context.Context) error {
atomic.StoreInt32(&r.duringTick, 1)
defer func() {
atomic.StoreInt32(&r.duringTick, 0)
}()
err := r.processDeferredActions()
if err != nil {
return err
}
err = r.eg.execute(deltaTime, ctx)
if err != nil {
return err
}
// processDeferred again that process deferred actions while processing Systems
return r.processDeferredActions()
}
func (r *Registry) processDeferredActions() error {
return r.deferredActions.process()
}
func (r *Registry) addObserverSync(o *Observer) {
r.observers = append(r.observers, o)
}
func (r *Registry) addSystemSync(s *System) {
r.systems = append(r.systems, s)
for _, a := range r.archeTypeList {
s.addArcheTypeIfInterest(a)
}
r.eg.addSystem(s)
}
func (r *Registry) removeEntitySync(entityId EntityId) error {
a := r.entityArcheTypeMap[entityId]
if a != nil {
a.removeEntity(entityId)
}
delete(r.entityArcheTypeMap, entityId)
// it is threadsafe so don't need to lock because it is only called on deferredActions
r.tombstones = append(r.tombstones, entityId)
// call observers
removed := a.getComponentTypeList()
for _, o := range r.observers {
err := o.executeIfInterest(entityId, a, nil, removed)
if err != nil {
return nil
}
}
return nil
}
func (r *Registry) processEntityActionSync(entityId EntityId, actions []entityAction) error {
if len(actions) == 0 {
return nil
}
var types []reflect.Type
var added []reflect.Type
var removed []reflect.Type
archeType := r.entityArcheTypeMap[entityId]
if archeType != nil {
types = archeType.getComponentTypeList()
}
for _, action := range actions {
action.modifyTypes(&types, &added, &removed)
}
targetArcheType := r.getOrMakeArcheTypeSync(types)
if targetArcheType != nil {
targetArcheType.addEntity(entityId)
if archeType != nil && archeType != targetArcheType {
// move component data from old to new archeType
origtypes := archeType.getComponentTypeList()
for _, t := range origtypes {
if targetArcheType.hasComponent(t) {
err := archeType.copyDataToOtherArcheType(entityId, t, targetArcheType)
if err != nil {
return errors.Errorf("failed to move data from old to new archetype %v", err)
}
}
}
}
for _, action := range actions {
action.apply(entityId, targetArcheType)
}
}
if archeType != nil {
archeType.removeEntity(entityId)
}
r.entityArcheTypeMap[entityId] = targetArcheType
// call observers
for _, o := range r.observers {
err := o.executeIfInterest(entityId, targetArcheType, added, removed)
if err != nil {
return err
}
}
return nil
}
func (r *Registry) getOrMakeArcheTypeSync(types []reflect.Type) *ArcheType {
if len(types) == 0 {
return nil
}
for _, a := range r.archeTypeList {
if a.equalComponents(types) {
return a
}
}
newArcheType := newArcheType(types...)
r.archeTypeList = append(r.archeTypeList, newArcheType)
r.onAddArcheType(newArcheType)
return newArcheType
}
func (r *Registry) onAddArcheType(archeType *ArcheType) {
for _, s := range r.systems {
s.addArcheTypeIfInterest(archeType)
}
}