-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworld.go
292 lines (244 loc) · 8.11 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
package ecs
import (
"math"
"sync/atomic"
"reflect" // For resourceName
)
var (
DefaultAllocation = 0
)
const (
InvalidEntity Id = 0 // Represents the default entity Id, which is invalid
firstEntity Id = 1
MaxEntity Id = math.MaxUint32
)
// World is the main data-holder. You usually pass it to other functions to do things.
type World struct {
idCounter atomic.Uint64
nextId Id
minId, maxId Id // This is the range of Ids returned by NewId
arch *internalMap[Id, archetypeId]
engine *archEngine
resources map[reflect.Type]any
}
// Creates a new world
func NewWorld() *World {
return &World{
nextId: firstEntity + 1,
minId: firstEntity + 1,
maxId: MaxEntity,
arch: newMap[Id, archetypeId](DefaultAllocation),
engine: newArchEngine(),
resources: make(map[reflect.Type]any),
}
}
// Sets an range of Ids that the world will use when creating new Ids. Potentially helpful when you have multiple worlds and don't want their Id space to collide.
// Deprecated: This API is tentative. It may be better to just have the user create Ids as they see fit
func (w *World) SetIdRange(min, max Id) {
if min <= firstEntity {
panic("max must be greater than 1")
}
if max <= firstEntity {
panic("max must be greater than 1")
}
if min > max {
panic("min must be less than max!")
}
w.minId = min
w.maxId = max
}
// Creates a new Id which can then be used to create an entity. This is threadsafe
func (w *World) NewId() Id {
for {
val := w.idCounter.Load()
if w.idCounter.CompareAndSwap(val, val+1) {
return (Id(val) % (w.maxId - w.minId)) + w.minId
}
}
// if w.nextId < w.minId {
// w.nextId = w.minId
// }
// id := w.nextId
// if w.nextId == w.maxId {
// w.nextId = w.minId
// } else {
// w.nextId++
// }
// return id
}
// Writes components to the entity specified at id. This API can potentially break if you call it inside of a loop. Specifically, if you cause the archetype of the entity to change by writing a new component, then the loop may act in mysterious ways.
// Deprecated: This API is tentative, I might replace it with something similar to bevy commands to alleviate the above concern
func Write(world *World, id Id, comp ...Component) {
world.Write(id, comp...)
}
func (world *World) Write(id Id, comp ...Component) {
if len(comp) <= 0 {
return // Do nothing if there are no components
}
archId, ok := world.arch.Get(id)
if ok {
newarchetypeId := world.engine.rewriteArch(archId, id, comp...)
world.arch.Put(id, newarchetypeId)
} else {
// Id does not yet exist, we need to add it for the first time
mask := buildArchMask(comp...)
archId = world.engine.getArchetypeId(mask)
world.arch.Put(id, archId)
// Write all components to that archetype
world.engine.write(archId, id, comp...)
}
}
// func (world *World) GetArchetype(comp ...Component) archetypeId {
// mask := buildArchMask(comp...)
// return world.engine.getArchetypeId(mask)
// }
// // Note: This returns the index of the location allocated
// func (world *World) Allocate(id Id, archId archetypeId) int {
// return world.allocate(id, world.engine.dcr.revArchMask[archId])
// }
// Returns the index of the location allocated. May return -1 if invalid archMask supplied
func (world *World) allocate(id Id, addMask archetypeMask) int {
if addMask == blankArchMask {
return -1 // Nothing to allocate
}
archId, ok := world.arch.Get(id)
if ok {
// Calculate the new mask based on the bitwise or of the old and added masks
lookup := world.engine.lookup[archId]
oldMask := lookup.mask
newMask := oldMask.bitwiseOr(addMask)
// If the new mask matches the old mask, then we don't need to move anything
if oldMask == newMask {
index, ok := lookup.index.Get(id)
if !ok {
panic("bug: id missing from lookup list")
}
return index
}
newArchId, newIndex := world.engine.moveArchetype(archId, newMask, id)
world.arch.Put(id, newArchId)
return newIndex
} else {
// Id does not yet exist, we need to add it for the first time
archId = world.engine.getArchetypeId(addMask)
world.arch.Put(id, archId)
// Write all components to that archetype
return world.engine.allocate(archId, id)
}
}
// May return -1 if invalid archMask supplied, or if the entity doesn't exist
func (world *World) deleteMask(id Id, deleteMask archetypeMask) {
archId, ok := world.arch.Get(id)
if !ok {
return
}
// 1. calculate the destination mask
lookup := world.engine.lookup[archId]
oldMask := lookup.mask
newMask := oldMask.bitwiseClear(deleteMask)
// If the new mask requires the removal of all components, then just delete the current entity
if newMask == blankArchMask {
Delete(world, id)
return
}
// If the new mask matches the old mask, then we don't need to move anything
if oldMask == newMask {
return
}
// 2. Move all components from source arch to dest arch
newArchId := world.engine.moveArchetypeDown(archId, newMask, id)
world.arch.Put(id, newArchId)
}
// Reads a specific component of the entity specified at id.
// Returns true if the entity was found and had that component, else returns false.
// Deprecated: This API is tentative, I'm trying to improve the QueryN construct so that it can capture this usecase.
func Read[T any](world *World, id Id) (T, bool) {
var ret T
archId, ok := world.arch.Get(id)
if !ok {
return ret, false
}
return readArch[T](world.engine, archId, id)
}
// Reads a pointer to the component of the entity at the specified id.
// Returns true if the entity was found and had that component, else returns false.
// This pointer is short lived and can become invalid if any other entity changes in the world
// Deprecated: This API is tentative, I'm trying to improve the QueryN construct so that it can capture this usecase.
func ReadPtr[T any](world *World, id Id) *T {
archId, ok := world.arch.Get(id)
if !ok {
return nil
}
return readPtrArch[T](world.engine, archId, id)
}
// This is safe for maps and loops
// 1. This deletes the high level id -> archId lookup
// 2. This creates a "hole" in the archetype list
// Returns true if the entity was deleted, else returns false if the entity does not exist (or was already deleted)
// Deletes the entire entity specified by the id
// This can be called inside maps and loops, it will delete the entity immediately.
// Returns true if the entity exists and was actually deleted, else returns false
func Delete(world *World, id Id) bool {
archId, ok := world.arch.Get(id)
if !ok {
return false
}
world.arch.Delete(id)
world.engine.TagForDeletion(archId, id)
return true
}
// Deletes specific components from an entity in the world
// Skips all work if the entity doesn't exist
// Skips deleting components that the entity doesn't have
// If no components remain after the delete, the entity will be completely removed
func DeleteComponent(world *World, id Id, comp ...Component) {
if len(comp) <= 0 {
return
}
mask := buildArchMask(comp...)
world.deleteMask(id, mask)
}
// Returns true if the entity exists in the world else it returns false
func (world *World) Exists(id Id) bool {
return world.arch.Has(id)
}
func GetInjectable[T any](world *World) T {
var t T
name := resourceName(t)
// 1. If already created, just use this variable
anyVal, ok := world.resources[name]
if ok {
return anyVal.(T)
}
// 2. If supports initialization, then make a new one and return it
tAny := any(t)
initializer, ok := tAny.(Initializer)
if ok {
anyVal = initializer.initialize(world)
world.resources[name] = anyVal
return anyVal.(T)
}
// 3. Fallback: Just return the default value for whatever it is
world.resources[name] = t
return t
}
// --------------------------------------------------------------------------------
// - Resources
// --------------------------------------------------------------------------------
func resourceName(t any) reflect.Type {
return reflect.TypeOf(t)
}
// TODO: Should I force people to do pointers?
func PutResource[T any](world *World, resource *T) {
name := resourceName(resource)
world.resources[name] = resource
}
func GetResource[T any](world *World) *T {
var t T
name := resourceName(&t)
anyVal, ok := world.resources[name]
if !ok {
return nil
}
return anyVal.(*T)
}