-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.go
321 lines (290 loc) · 7.73 KB
/
world.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
package ecs
import (
"fmt"
"io"
"sort"
"github.com/BurntSushi/toml"
"github.com/gabstv/container"
"github.com/google/uuid"
)
type World struct {
data container.Dictionary[string, interface{}]
lastEntity Entity
entities []Entity
entityIDs map[Entity]uuid.UUID // this is used when serializing/deserializing data
entityUUIDs map[uuid.UUID]Entity
eventManager *eventManager
components map[string]IComponentStore
systems []ISystem
sysMap map[int]ISystem
sysid int
isloading bool
enabled bool
}
func (w *World) Data() *container.Dictionary[string, interface{}] {
return &w.data
}
func (w *World) IsLoading() bool {
return w.isloading
}
// EntityUUID returns the UUID of the entity
// If the entity exists, but no UUID is set, a new UUID is generated and set
func (w *World) EntityUUID(e Entity) uuid.UUID {
x := sort.Search(len(w.entities), func(i int) bool {
return w.entities[i] >= e
})
if x < len(w.entities) && w.entities[x] == e {
// x is present at data[i]
if uuid, ok := w.entityIDs[e]; ok {
return uuid
}
id, err := uuid.NewRandom()
if err != nil {
panic(err)
}
w.entityIDs[e] = id
w.entityUUIDs[id] = e
return id
}
// entity not found
return uuid.UUID{}
}
// getEntityByUUID is used by the deserializer to get the entity with the given UUID
//
// If the entity does not exist, a new entity is created and is assigned with the UUID
func (w *World) getEntityByUUID(id uuid.UUID) Entity {
if e, ok := w.entityUUIDs[id]; ok {
return e
}
// create a new entity and set the uuid to it
e := w.NewEntity()
w.entityUUIDs[id] = e
w.entityIDs[e] = id
return e
}
// EntityByUUID returns the entity with the given UUID
// If the entity does not exist, an empty entity (0) is returned
func (w *World) EntityByUUID(id uuid.UUID) (Entity, bool) {
if e, ok := w.entityUUIDs[id]; ok {
return e, true
}
return 0, false
}
func (w *World) NewEntity() Entity {
w.lastEntity++
w.entities = append(w.entities, w.lastEntity)
return w.lastEntity
}
// Remove removes an Entity. It tries to delete the entity from all the
// component registries of this world.
func (w *World) Remove(e Entity) bool {
x := sort.Search(len(w.entities), func(i int) bool {
return w.entities[i] >= e
})
if x >= len(w.entities) || w.entities[x] != e {
return false
}
// x is present at data[i]
for _, c := range w.components {
_ = c.Remove(e)
}
w.entities = append(w.entities[:x], w.entities[x+1:]...)
return true
}
func (w *World) RemoveSystem(id int) bool {
if sys, ok := w.sysMap[id]; ok {
delete(w.sysMap, id)
di := -1
for i, s := range w.systems {
if s == sys {
di = i
break
}
}
if di >= 0 {
w.systems = append(w.systems[:di], w.systems[di+1:]...)
}
return true
}
return false
}
func (w *World) Step() {
for _, sys := range w.systems {
sys.Execute()
}
}
func (w *World) StepF(flag int) {
for _, sys := range w.systems {
if sys.Flag()&flag != 0 {
sys.Execute()
}
}
}
func (w *World) Enabled() bool {
return w.enabled
}
func (w *World) SetEnabled(v bool) {
w.enabled = v
}
// AllEntities returns all entities in the world
func (w *World) AllEntities() []Entity {
ecopy := make([]Entity, len(w.entities))
copy(ecopy, w.entities)
return ecopy
}
// MarshalTo marshals the world data to a writer
func (w *World) MarshalTo(dw io.Writer) error {
return w.serializeData(toml.NewEncoder(dw))
}
func (w *World) UnmarshalFrom(dr io.Reader) error {
x := &DeserializedWorld{}
md, err := toml.NewDecoder(dr).Decode(x)
if err != nil {
return fmt.Errorf("failed to decode toml world data: %w", err)
}
return w.deserializeData(md, x)
}
func (w *World) UnmarshalFromMeta(md toml.MetaData, prim toml.Primitive) error {
x := &DeserializedWorld{}
err := md.PrimitiveDecode(prim, x)
if err != nil {
return fmt.Errorf("failed to decode toml world data: %w", err)
}
return w.deserializeData(md, x)
}
func (w *World) GetGenericComponent(registryName string) IComponentStore {
return w.components[registryName]
}
func (w *World) addSystem(sys ISystem) int {
w.sysid++
w.systems = append(w.systems, sys)
sort.SliceStable(w.systems, func(i, j int) bool {
return w.systems[i].Priority() < w.systems[j].Priority()
})
w.sysMap[w.sysid] = sys
return w.sysid
}
func (w *World) deserializeData(md toml.MetaData, dw *DeserializedWorld) error {
w.isloading = true
decoderMutex.Lock()
defer decoderMutex.Unlock()
setDecoderWorld(w)
defer setDecoderWorld(nil)
w.enabled = dw.Enabled
compoSmap := dw.ComponentIndex.ToMap()
compoImap := make(map[int]string)
for k, v := range compoSmap {
compoImap[v] = k
}
compos := make(map[int]IComponentStore)
// the components need to be registered beforehand
for i, v := range compoImap {
compos[i] = w.GetGenericComponent(v)
}
for _, ent := range dw.Entities {
e := w.getEntityByUUID(ent.UUID)
for _, c := range ent.Components {
if compos[c.CI] == nil {
if SerializerLogger != nil {
SerializerLogger.Printf("component [%d] %s not registered", c.CI, compoImap[c.CI])
}
} else {
compos[c.CI].dataImport(e, c.Data, md)
}
}
}
w.isloading = false
return nil
}
func (w *World) serializeData(me Encoder) error {
encoderMutex.Lock()
defer encoderMutex.Unlock()
setEncoderWorld(w)
defer setEncoderWorld(nil)
sw := SerializedWorld{
Entities: make([]SerializedEntity, 0, len(w.entities)),
Enabled: w.enabled,
}
compIndex := make(map[string]int)
entt := make(map[Entity]*SerializedEntity)
type ctuple struct {
Name string
Store IComponentStore
}
ctuples := make([]ctuple, 0, len(w.components))
for name, c := range w.components {
ctuples = append(ctuples, ctuple{Name: name, Store: c})
}
sort.SliceStable(ctuples, func(i, j int) bool {
return ctuples[i].Name < ctuples[j].Name
})
for indexm, ctuplev := range ctuples {
c := ctuplev.Store
ci := indexm + 1
compIndex[ctuplev.Name] = ci
c.dataExtract(func(e Entity, d interface{}) {
ent := entt[e]
if ent == nil {
ent = &SerializedEntity{
UUID: w.EntityUUID(e),
Components: make([]interface{}, 0),
}
}
ent.Components = append(ent.Components, SerializedComponentData{
CI: ci,
Data: d,
})
entt[e] = ent
})
}
stb := make([]Sortable[Entity, *SerializedEntity], 0, len(entt))
for eid, ent := range entt {
stb = append(stb, Sortable[Entity, *SerializedEntity]{
Index: eid,
Data: ent,
})
}
sort.Slice(stb, func(i, j int) bool {
return stb[i].Index < stb[j].Index
})
for _, s := range stb {
sw.Entities = append(sw.Entities, *s.Data)
}
sw.ComponentIndex = componentIndexFromMap(compIndex)
return me.Encode(sw)
}
// NewWorld creates a new world. A world is not thread safe .I t shouldn't be
// shared between threads. This function also adds the default systems to the
// world. To create a new world without any systems, use NewEmptyWorld()
func NewWorld() *World {
w := newWorld()
globalSystems.lock.Lock()
defer globalSystems.lock.Unlock()
for _, sf := range globalSystems.sysFactory {
sf(w)
}
return w
}
// NewEmptyWorld creates a new world. A world is not thread safe .I t shouldn't
// be shared between threads. NewEmptyWorld creates a new world with no systems.
func NewEmptyWorld() *World {
return newWorld()
}
// Remove removes an entity from the world. It will also remove all components
// attached to the entity. It returns false if the entity was not found.
func Remove(w *World, e Entity) bool {
return w.Remove(e)
}
func newWorld() *World {
return &World{
lastEntity: 0,
entities: make([]Entity, 0, 1024),
entityIDs: make(map[Entity]uuid.UUID),
entityUUIDs: make(map[uuid.UUID]Entity),
components: make(map[string]IComponentStore),
systems: make([]ISystem, 0, 32),
sysMap: make(map[int]ISystem),
enabled: true,
eventManager: newEventManager(),
}
}