-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_componentmgr.go
309 lines (252 loc) · 9.13 KB
/
entity_componentmgr.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
package ec
import (
"fmt"
"git.golaxy.org/core/event"
"git.golaxy.org/core/internal/exception"
"git.golaxy.org/core/util/container"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/core/util/iface"
"git.golaxy.org/core/util/uid"
"slices"
)
// iComponentMgr 组件管理器接口
type iComponentMgr interface {
// GetComponent 使用名称查询组件,同个名称指向多个组件时,返回首个组件
GetComponent(name string) Component
// GetComponentById 使用组件Id查询组件
GetComponentById(id uid.Id) Component
// ContainsComponent 组件是否存在
ContainsComponent(name string) bool
// ContainsComponentById 使用组件Id检测组件是否存在
ContainsComponentById(id uid.Id) bool
// RangeComponents 遍历所有组件
RangeComponents(fun generic.Func1[Component, bool])
// ReversedRangeComponents 反向遍历所有组件
ReversedRangeComponents(fun generic.Func1[Component, bool])
// FilterComponents 过滤并获取组件
FilterComponents(fun generic.Func1[Component, bool]) []Component
// GetComponents 获取所有组件
GetComponents() []Component
// CountComponents 统计所有组件数量
CountComponents() int
// AddComponent 添加组件,因为同个名称可以指向多个组件,所以名称指向的组件已存在时,不会返回错误
AddComponent(name string, components ...Component) error
// RemoveComponent 使用名称删除组件,将会删除同个名称指向的多个组件
RemoveComponent(name string)
// RemoveComponentById 使用组件Id删除组件
RemoveComponentById(id uid.Id)
iAutoEventComponentMgrAddComponents // 事件:实体的组件管理器添加组件
iAutoEventComponentMgrRemoveComponent // 事件:实体的组件管理器删除组件
iAutoEventComponentMgrFirstAccessComponent // 事件:实体的组件管理器首次访问组件
}
// GetComponent 使用名称查询组件,同个名称指向多个组件时,返回首个组件
func (entity *EntityBehavior) GetComponent(name string) Component {
if e, ok := entity.getComponentElement(name); ok {
return entity.accessComponent(iface.Cache2Iface[Component](e.Value.Cache))
}
return nil
}
// GetComponentById 使用组件Id查询组件
func (entity *EntityBehavior) GetComponentById(id uid.Id) Component {
if e, ok := entity.getComponentElementById(id); ok {
return entity.accessComponent(iface.Cache2Iface[Component](e.Value.Cache))
}
return nil
}
// ContainsComponent 组件是否存在
func (entity *EntityBehavior) ContainsComponent(name string) bool {
_, ok := entity.getComponentElement(name)
return ok
}
// ContainsComponentById 使用组件Id检测组件是否存在
func (entity *EntityBehavior) ContainsComponentById(id uid.Id) bool {
_, ok := entity.getComponentElementById(id)
return ok
}
// RangeComponents 遍历所有组件
func (entity *EntityBehavior) RangeComponents(fun generic.Func1[Component, bool]) {
entity.componentList.Traversal(func(e *container.Element[iface.FaceAny]) bool {
comp := entity.accessComponent(iface.Cache2Iface[Component](e.Value.Cache))
if comp == nil {
return true
}
return fun.Exec(comp)
})
}
// ReversedRangeComponents 反向遍历所有组件
func (entity *EntityBehavior) ReversedRangeComponents(fun generic.Func1[Component, bool]) {
entity.componentList.ReversedTraversal(func(e *container.Element[iface.FaceAny]) bool {
comp := entity.accessComponent(iface.Cache2Iface[Component](e.Value.Cache))
if comp == nil {
return true
}
return fun.Exec(comp)
})
}
// FilterComponents 过滤并获取组件
func (entity *EntityBehavior) FilterComponents(fun generic.Func1[Component, bool]) []Component {
var components []Component
entity.componentList.Traversal(func(e *container.Element[iface.FaceAny]) bool {
comp := iface.Cache2Iface[Component](e.Value.Cache)
if fun.Exec(comp) {
components = append(components, comp)
}
return true
})
for i := range components {
if entity.accessComponent(components[i]) == nil {
components[i] = nil
}
}
components = slices.DeleteFunc(components, func(comp Component) bool {
return comp == nil
})
return components
}
// GetComponents 获取所有组件
func (entity *EntityBehavior) GetComponents() []Component {
components := make([]Component, 0, entity.componentList.Len())
entity.componentList.Traversal(func(e *container.Element[iface.FaceAny]) bool {
components = append(components, iface.Cache2Iface[Component](e.Value.Cache))
return true
})
for i := range components {
if entity.accessComponent(components[i]) == nil {
components[i] = nil
}
}
components = slices.DeleteFunc(components, func(comp Component) bool {
return comp == nil
})
return components
}
// CountComponents 统计所有组件数量
func (entity *EntityBehavior) CountComponents() int {
return entity.componentList.Len()
}
// AddComponent 添加组件,因为同个名称可以指向多个组件,所以名称指向的组件已存在时,不会返回错误
func (entity *EntityBehavior) AddComponent(name string, components ...Component) error {
if len(components) <= 0 {
return fmt.Errorf("%w: %w: components is empty", ErrEC, exception.ErrArgs)
}
for i := range components {
comp := components[i]
if comp == nil {
return fmt.Errorf("%w: %w: component is nil", ErrEC, exception.ErrArgs)
}
if comp.GetState() != ComponentState_Birth {
return fmt.Errorf("%w: invalid component state %q", ErrEC, comp.GetState())
}
}
for i := range components {
if err := entity.addComponent(name, components[i]); err != nil {
return err
}
}
_EmitEventComponentMgrAddComponents(entity, entity.opts.CompositeFace.Iface, components)
return nil
}
// RemoveComponent 使用名称删除组件,将会删除同个名称指向的多个组件
func (entity *EntityBehavior) RemoveComponent(name string) {
e, ok := entity.getComponentElement(name)
if !ok {
return
}
entity.componentList.TraversalAt(func(other *container.Element[iface.FaceAny]) bool {
comp := iface.Cache2Iface[Component](other.Value.Cache)
if comp.GetName() != name {
return false
}
if comp.getFixed() {
return true
}
if comp.GetState() > ComponentState_Living {
return true
}
comp.setState(ComponentState_Detach)
_EmitEventComponentMgrRemoveComponent(entity, entity.opts.CompositeFace.Iface, comp)
other.Escape()
return true
}, e)
}
// RemoveComponentById 使用组件Id删除组件
func (entity *EntityBehavior) RemoveComponentById(id uid.Id) {
e, ok := entity.getComponentElementById(id)
if !ok {
return
}
comp := iface.Cache2Iface[Component](e.Value.Cache)
if comp.getFixed() {
return
}
if comp.GetState() > ComponentState_Living {
return
}
comp.setState(ComponentState_Detach)
_EmitEventComponentMgrRemoveComponent(entity, entity.opts.CompositeFace.Iface, comp)
e.Escape()
}
// EventComponentMgrAddComponents 事件:实体的组件管理器添加组件
func (entity *EntityBehavior) EventComponentMgrAddComponents() event.IEvent {
return &entity.eventComponentMgrAddComponents
}
// EventComponentMgrRemoveComponent 事件:实体的组件管理器删除组件
func (entity *EntityBehavior) EventComponentMgrRemoveComponent() event.IEvent {
return &entity.eventComponentMgrRemoveComponent
}
// EventComponentMgrFirstAccessComponent 事件:实体的组件管理器首次访问组件
func (entity *EntityBehavior) EventComponentMgrFirstAccessComponent() event.IEvent {
return &entity.eventComponentMgrFirstAccessComponent
}
func (entity *EntityBehavior) addComponent(name string, component Component) error {
component.init(name, entity.opts.CompositeFace.Iface, component)
face := iface.MakeFaceAny(component)
if e, ok := entity.getComponentElement(name); ok {
entity.componentList.TraversalAt(func(other *container.Element[iface.FaceAny]) bool {
if iface.Cache2Iface[Component](other.Value.Cache).GetName() == name {
e = other
return true
}
return false
}, e)
e = entity.componentList.InsertAfter(face, e)
} else {
e = entity.componentList.PushBack(face)
}
component.setState(ComponentState_Attach)
return nil
}
func (entity *EntityBehavior) getComponentElement(name string) (*container.Element[iface.FaceAny], bool) {
var e *container.Element[iface.FaceAny]
entity.componentList.Traversal(func(other *container.Element[iface.FaceAny]) bool {
if iface.Cache2Iface[Component](other.Value.Cache).GetName() == name {
e = other
return false
}
return true
})
return e, e != nil
}
func (entity *EntityBehavior) getComponentElementById(id uid.Id) (*container.Element[iface.FaceAny], bool) {
var e *container.Element[iface.FaceAny]
entity.componentList.Traversal(func(other *container.Element[iface.FaceAny]) bool {
if iface.Cache2Iface[Component](other.Value.Cache).GetId() == id {
e = other
return false
}
return true
})
return e, e != nil
}
func (entity *EntityBehavior) accessComponent(comp Component) Component {
if entity.opts.AwakeOnFirstAccess && comp.GetState() == ComponentState_Attach {
switch entity.GetState() {
case EntityState_Awake, EntityState_Start, EntityState_Living:
_EmitEventComponentMgrFirstAccessComponent(entity, entity.opts.CompositeFace.Iface, comp)
}
}
if comp.GetState() >= ComponentState_Death {
return nil
}
return comp
}