-
Notifications
You must be signed in to change notification settings - Fork 459
/
entity_map.go
76 lines (63 loc) · 1.56 KB
/
entity_map.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
package entity
import (
"bytes"
"github.com/xiaonanln/goworld/engine/common"
)
// EntityMap is the data structure for maintaining entity IDs to entities
type EntityMap map[common.EntityID]*Entity
// Add adds a new entity to EntityMap
func (em EntityMap) Add(entity *Entity) {
em[entity.ID] = entity
}
// Del deletes an entity from EntityMap
func (em EntityMap) Del(id common.EntityID) {
delete(em, id)
}
// Get returns the Entity of specified entity ID in EntityMap
func (em EntityMap) Get(id common.EntityID) *Entity {
return em[id]
}
// Keys return keys of the EntityMap in a slice
func (em EntityMap) Keys() (keys []common.EntityID) {
for eid := range em {
keys = append(keys, eid)
}
return
}
// Values return values of the EntityMap in a slice
func (em EntityMap) Values() (vals []*Entity) {
for _, e := range em {
vals = append(vals, e)
}
return
}
// EntitySet is the data structure for a set of entities
type EntitySet map[*Entity]struct{}
// Add adds an entity to the EntitySet
func (es EntitySet) Add(entity *Entity) {
es[entity] = struct{}{}
}
// Del deletes an entity from the EntitySet
func (es EntitySet) Del(entity *Entity) {
delete(es, entity)
}
// Contains returns if the entity is in the EntitySet
func (es EntitySet) Contains(entity *Entity) bool {
_, ok := es[entity]
return ok
}
func (es EntitySet) String() string {
b := bytes.Buffer{}
b.WriteString("{")
first := true
for entity := range es {
if !first {
b.WriteString(", ")
} else {
first = false
}
b.WriteString(entity.String())
}
b.WriteString("}")
return b.String()
}