-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.go
More file actions
160 lines (118 loc) · 2.97 KB
/
Copy pathEntityManager.go
File metadata and controls
160 lines (118 loc) · 2.97 KB
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
package limbov1
import (
errnov1 "github.com/rejchev/errno"
)
type EntityManager struct {
ents []Entity
free []uint32
gens []uint16
mask []ComponentMask
entmap map[Entity]int
}
var entityManager = EntityManager{}
func Entities() *EntityManager {
return &entityManager
}
func (x *EntityManager) Init() errnov1.Code {
x.ents = make([]Entity, 0, 8)
x.gens = make([]uint16, 0, 8)
x.free = make([]uint32, 0, 8)
x.mask = make([]ComponentMask, 0, 8)
x.entmap = map[Entity]int{}
return errnov1.OK
}
func (x *EntityManager) Cleanup() {
x.entmap = map[Entity]int{}
x.ents = x.ents[:]
x.free = x.free[:]
x.gens = x.gens[:]
x.mask = x.mask[:]
}
func (x *EntityManager) Create() Entity {
id := uint32(0)
if len(x.free) > 0 {
id = x.free[len(x.free)-1]
x.free = x.free[:len(x.free)-1]
} else {
id = uint32(len(x.gens))
x.gens = append(x.gens, 0)
}
ent := CreateEntity(id, x.gens[id])
innerIdx := len(x.ents)
x.ents = append(x.ents, ent)
if len(x.mask) <= int(innerIdx) {
x.mask = append(x.mask, make([]ComponentMask, innerIdx+1-len(x.mask))...)
}
x.entmap[ent] = innerIdx
Events().Publish("entity.created", ent)
return ent
}
// DestroyEntitySystem -> ent.Destroy -> sync Publish("entity.destroy")
func (x *EntityManager) Destroy(v Entity) {
innerIdx := x.index(v)
if innerIdx == -1 {
return
}
Events().Publish("entity.destroy", v)
idx := v.Id()
x.gens[idx]++
x.mask[innerIdx].Reset()
x.free = append(x.free, idx)
entsLen := len(x.ents)
if entsLen > (innerIdx + 1) {
x.ents[innerIdx] = x.ents[entsLen-1]
x.mask[innerIdx] = x.mask[entsLen-1]
x.entmap[x.ents[entsLen-1]] = innerIdx
}
delete(x.entmap, v)
x.ents = x.ents[:entsLen-1]
x.mask = x.mask[:entsLen-1]
Events().Publish("entity.destroyed", v)
}
func (x *EntityManager) Mask(v Entity) ComponentMask {
return x.mask[x.index(v)]
}
func (x *EntityManager) Iterator() *Iterator[Entity] {
buff := make([]Entity, len(x.ents))
copy(buff, x.ents)
return NewIterator(buff)
}
func (x *EntityManager) WithComponent(e Entity, v Compotype) {
x.Mask(e).With(v)
}
func (x *EntityManager) WithComponents(e Entity, v ...Compotype) {
x.Mask(e).WithB(v...)
}
func (x *EntityManager) WithoutComponent(e Entity, v Compotype) {
x.Mask(e).Without(v)
}
func (x *EntityManager) SetMask(e Entity, v ComponentMask) {
x.mask[x.index(e)] = v
}
func (x *EntityManager) Masks() []ComponentMask {
return x.mask
}
func (x *EntityManager) Count() int {
return len(x.entmap)
}
func (x *EntityManager) index(v Entity) int {
if v, ok := x.entmap[v]; ok {
return v
}
return -1
}
func (x *EntityManager) HasComponentB(e Entity, t Compotype) bool {
return x.HasComponent(e, t)
}
func (x *EntityManager) HasComponent(e Entity, t Compotype) bool {
return x.Mask(e).Has(t)
}
func (x *EntityManager) Has(e Entity, t Compotype) bool {
return x.HasComponentB(e, t)
}
func (x *EntityManager) IsAlive(v Entity) bool {
if uint32(len(x.gens)) <= v.Id() {
return false
}
return x.gens[v.Id()] == v.Gen() && x.index(v) != -1
}