-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathentity.go
39 lines (32 loc) · 819 Bytes
/
entity.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
package main
type EntityID int32
type Entity struct {
EntityID EntityID
}
type EntityManager struct {
nextEntityID EntityID
entities map[EntityID]*Entity
}
// Allocate and assign a new entity ID
func (mgr *EntityManager) AddEntity(entity *Entity) {
// EntityManager starts initialized to zero
if mgr.entities == nil {
mgr.entities = make(map[EntityID]*Entity)
}
// Search for next free ID
entityID := mgr.nextEntityID
_, exists := mgr.entities[entityID]
for exists {
entityID++
if entityID == mgr.nextEntityID {
panic("EntityID space exhausted")
}
_, exists = mgr.entities[entityID]
}
entity.EntityID = entityID
mgr.entities[entityID] = entity
mgr.nextEntityID = entityID + 1
}
func (mgr *EntityManager) RemoveEntity(entity *Entity) {
mgr.entities[entity.EntityID] = nil, false
}