-
Notifications
You must be signed in to change notification settings - Fork 9
/
entity.go
197 lines (168 loc) · 3.9 KB
/
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
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
package entitas
import (
"errors"
"fmt"
"sort"
)
var (
ErrComponentExists = errors.New("component exists")
ErrComponentDoesNotExist = errors.New("component does not exist")
)
type EntityID uint
type Entity interface {
AddComponent(cs ...Component) error
ReplaceComponent(cs ...Component)
WillRemoveComponent(ts ...ComponentType) error
RemoveComponent(ts ...ComponentType) error
RemoveAllComponents()
RemoveAllCallbacks()
AddCallback(ev ComponentEvent, cb ComponentCallback)
HasCallbacks() bool
ID() EntityID
HasComponent(ts ...ComponentType) bool
HasAnyComponent(ts ...ComponentType) bool
Component(t ComponentType) (Component, error)
Components() []Component
ComponentIndices() []ComponentType
}
type ComponentEvent uint
const (
ComponentAdded ComponentEvent = iota
ComponentReplaced
ComponentWillBeRemoved
ComponentRemoved
)
type ComponentCallback func(Entity, Component)
type entity struct {
id EntityID
components map[ComponentType]Component
callbacks map[ComponentEvent][]ComponentCallback
}
func NewEntity(id int) Entity {
return &entity{
id: EntityID(id),
components: make(map[ComponentType]Component),
callbacks: make(map[ComponentEvent][]ComponentCallback),
}
}
func (e *entity) AddComponent(cs ...Component) error {
for _, c := range cs {
if e.HasComponent(c.Type()) {
return ErrComponentExists
}
e.components[c.Type()] = c
e.callback(ComponentAdded, c)
}
return nil
}
func (e *entity) ReplaceComponent(cs ...Component) {
for _, c := range cs {
has := e.HasComponent(c.Type())
e.components[c.Type()] = c
if has {
e.callback(ComponentReplaced, c)
} else {
e.callback(ComponentAdded, c)
}
}
}
func (e *entity) WillRemoveComponent(ts ...ComponentType) error {
for _, t := range ts {
c, err := e.Component(t)
if err != nil {
return err
}
e.callback(ComponentWillBeRemoved, c)
}
return nil
}
func (e *entity) RemoveComponent(ts ...ComponentType) error {
for _, t := range ts {
c, err := e.Component(t)
if err != nil {
return err
}
e.callback(ComponentWillBeRemoved, c)
delete(e.components, t)
e.callback(ComponentRemoved, c)
}
return nil
}
func (e *entity) RemoveAllComponents() {
components := e.components
for _, c := range components {
e.callback(ComponentWillBeRemoved, c)
}
e.components = make(map[ComponentType]Component)
for _, c := range components {
e.callback(ComponentRemoved, c)
}
}
func (e *entity) ID() EntityID {
return e.id
}
func (e *entity) AddCallback(ev ComponentEvent, cb ComponentCallback) {
cbs, ok := e.callbacks[ev]
if !ok {
cbs = make([]ComponentCallback, 0)
}
e.callbacks[ev] = append(cbs, cb)
}
func (e *entity) HasCallbacks() bool {
return len(e.callbacks) > 0
}
func (e *entity) RemoveAllCallbacks() {
e.callbacks = make(map[ComponentEvent][]ComponentCallback)
}
func (e *entity) HasComponent(ts ...ComponentType) bool {
for _, t := range ts {
if _, ok := e.components[t]; !ok {
return false
}
}
return true
}
func (e *entity) HasAnyComponent(ts ...ComponentType) bool {
for _, t := range ts {
if _, ok := e.components[t]; ok {
return true
}
}
return false
}
func (e *entity) Component(t ComponentType) (Component, error) {
c, ok := e.components[t]
if !ok {
return nil, ErrComponentDoesNotExist
}
return c, nil
}
func (e *entity) Components() []Component {
components := make([]Component, len(e.components))
i := 0
for _, c := range e.components {
components[i] = c
i++
}
sort.Sort(ComponentsByType(components))
return components
}
func (e *entity) ComponentIndices() []ComponentType {
types := make([]ComponentType, len(e.components))
i := 0
for t := range e.components {
types[i] = t
i++
}
return types
}
func (e *entity) String() string {
return fmt.Sprintf("Entity_%d(%v)", e.id, e.Components())
}
func (e *entity) callback(ev ComponentEvent, c Component) {
if cbs, ok := e.callbacks[ev]; ok {
for _, cb := range cbs {
cb(e, c)
}
}
}