-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.go
315 lines (252 loc) · 9.45 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
// Package volt is an ECS for game development, based on the Archetype paradigm.
package volt
import (
"hash/fnv"
"slices"
)
// uint16 identifier, for small scoped data.
type smallId uint16
// uint64 identifier, for big scoped data.
type id uint64
// Entity identifier in the world.
type EntityId id
// Component identifier in the register.
type ComponentId smallId
// archetype identifier in the world.
type archetypeId id
// List of ComponentId.
type componentsIds []ComponentId
// Implementation of an archetype with its identifier, componentsIds, and entitiesIds
type archetype struct {
Id archetypeId
Type componentsIds
entities []EntityId
}
// Container of archetype and key position in storage, for a given EntityId
type entityRecord struct {
Id EntityId
archetypeId archetypeId
key int
name entityName
}
// entityName is a string transformed to byte array.
//
// It avoids the garbage collector to analyze this data constantly,
// at the price of a fixed data size.
type entityName = string
type entities map[EntityId]entityRecord
// World representation, container of all the data related to entities and their Components.
type World struct {
componentsRegistry ComponentsRegister
entities entities
archetypes []archetype
storage []storage
entityAddedFn func(entityId EntityId)
entityRemovedFn func(entityId EntityId)
componentAddedFn func(entityId EntityId, componentId ComponentId)
componentRemovedFn func(entityId EntityId, componentId ComponentId)
}
// CreateWorld returns a pointer to a new World.
//
// It preallocates initialCapacity in memory.
func CreateWorld(initialCapacity int) *World {
world := &World{
entities: make(entities, initialCapacity),
archetypes: make([]archetype, 0, 1024),
storage: make([]storage, TAGS_INDICES),
entityAddedFn: func(entityId EntityId) {},
entityRemovedFn: func(entityId EntityId) {},
componentAddedFn: func(entityId EntityId, componentId ComponentId) {},
componentRemovedFn: func(entityId EntityId, componentId ComponentId) {},
}
world.createArchetype()
return world
}
// SetEntityAddedFn sets a callback for when a new entity is added.
func (world *World) SetEntityAddedFn(entityAddedFn func(entityId EntityId)) {
world.entityAddedFn = entityAddedFn
}
// SetEntityRemovedFn sets a callback for when an entity is removed.
func (world *World) SetEntityRemovedFn(entityRemovedFn func(entityId EntityId)) {
world.entityRemovedFn = entityRemovedFn
}
// SetComponentAddedFn sets a callback for when a component is added to an entity.
func (world *World) SetComponentAddedFn(componentAddedFn func(entityId EntityId, componentId ComponentId)) {
world.componentAddedFn = componentAddedFn
}
// SetComponentRemovedFn sets a callback for when a component is removed.
func (world *World) SetComponentRemovedFn(componentRemovedFn func(entityId EntityId, componentId ComponentId)) {
world.componentRemovedFn = componentRemovedFn
}
// CreateEntity creates a new Entity in World;
// It is linked to no Component.
func (world *World) CreateEntity(name string) EntityId {
if existingId := world.SearchEntity(name); existingId != 0 {
return existingId
}
entityId := hashEntityName(name)
archetype := world.getArchetypeForComponentsIds()
entityRecord := entityRecord{
Id: entityId,
name: name,
}
world.entities[entityId] = entityRecord
world.setArchetype(entityRecord, archetype)
return entityId
}
// CreateEntityWithComponents2 creates an entity in World;
// It sets the components A, B to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents2[A, B ComponentInterface](world *World, name string, a A, b B) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents2(world, entityRecord, a, b)
if err != nil {
return 0, err
}
return entityId, nil
}
// CreateEntityWithComponents3 creates an entity in World;
//
// It sets the components A, B, C to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents3[A, B, C ComponentInterface](world *World, name string, a A, b B, c C) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents3(world, entityRecord, a, b, c)
if err != nil {
return 0, err
}
return entityId, nil
}
// CreateEntityWithComponents4 creates an entity in World;
//
// It sets the components A, B, C, D to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents4[A, B, C, D ComponentInterface](world *World, name string, a A, b B, c C, d D) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents4(world, entityRecord, a, b, c, d)
if err != nil {
return 0, err
}
return entityId, nil
}
// CreateEntityWithComponents5 creates an entity in World;
//
// It sets the components A, B, C, D, E to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents5[A, B, C, D, E ComponentInterface](world *World, name string, a A, b B, c C, d D, e E) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents5(world, entityRecord, a, b, c, d, e)
if err != nil {
return 0, err
}
return entityId, nil
}
// CreateEntityWithComponents6 creates an entity in World;
//
// It sets the components A, B, C, D, E, F to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents6[A, B, C, D, E, F ComponentInterface](world *World, name string, a A, b B, c C, d D, e E, f F) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents6(world, entityRecord, a, b, c, d, e, f)
if err != nil {
return 0, err
}
return entityId, nil
}
// CreateEntityWithComponents7 creates an entity in World;
//
// It sets the components A, B, C, D, E, F, G to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents7[A, B, C, D, E, F, G ComponentInterface](world *World, name string, a A, b B, c C, d D, e E, f F, g G) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents7(world, entityRecord, a, b, c, d, e, f, g)
if err != nil {
return 0, err
}
return entityId, nil
}
// CreateEntityWithComponents8 creates an entity in World;
//
// It sets the components A, B, C, D, E, F, G, H to the entity, for faster performances than the atomic version.
func CreateEntityWithComponents8[A, B, C, D, E, F, G, H ComponentInterface](world *World, name string, a A, b B, c C, d D, e E, f F, g G, h H) (EntityId, error) {
entityId := hashEntityName(name)
entityRecord := entityRecord{Id: entityId, name: name}
world.entities[entityId] = entityRecord
err := addComponents8(world, entityRecord, a, b, c, d, e, f, g, h)
if err != nil {
return 0, err
}
return entityId, nil
}
// PublishEntity calls the callback setted in SetEntityAddedFn.
func (world *World) PublishEntity(entityId EntityId) {
world.entityAddedFn(entityId)
}
// RemoveEntity removes all the data related to an Entity.
//
// It calls the callback setted in SetEntityRemovedFn beforehand, so that the callback still has access to the data.
func (world *World) RemoveEntity(entityId EntityId) {
world.entityRemovedFn(entityId)
entityRecord := world.entities[entityId]
archetype := world.archetypes[entityRecord.archetypeId]
lastEntityKey := len(archetype.entities) - 1
for _, componentId := range archetype.Type {
s := world.storage[componentId]
if s != nil && slices.Contains(archetype.Type, s.getType()) {
s.moveLastToKey(archetype.Id, entityRecord.key)
}
}
if lastEntityKey >= 0 {
lastEntityId := world.archetypes[archetype.Id].entities[lastEntityKey]
lastEntity := world.entities[lastEntityId]
if lastEntity.key > entityRecord.key {
lastEntity.key = entityRecord.key
world.entities[lastEntityId] = lastEntity
archetype.entities[entityRecord.key] = lastEntityId
}
archetype.entities = archetype.entities[:lastEntityKey]
world.archetypes[archetype.Id] = archetype
}
delete(world.entities, entityId)
}
// SearchEntity returns the EntityId named by name.
// If not found, returns 0.
func (world *World) SearchEntity(name string) EntityId {
entityId := hashEntityName(name)
if _, ok := world.entities[entityId]; ok {
return entityId
}
return 0
}
// GetEntityName returns the name of an EntityId.
// If not found, returns an empty string.
func (world *World) GetEntityName(entityId EntityId) string {
if entity, ok := world.entities[entityId]; ok {
return entity.name
}
return ""
}
// SetEntityName sets the name for an EntityId.
func (world *World) SetEntityName(entityId EntityId, name string) {
entityRecord := world.entities[entityId]
entityRecord.name = name
world.entities[entityId] = entityRecord
}
// Count returns the number of entities in World.
func (world *World) Count() int {
return len(world.entities)
}
func hashEntityName(name entityName) EntityId {
h := fnv.New64()
_, err := h.Write([]byte(name))
if err != nil {
return EntityId(0)
}
return EntityId(h.Sum64())
}