-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
212 lines (180 loc) · 6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ec
import (
"fmt"
"git.golaxy.org/core/event"
"git.golaxy.org/core/internal/concurrent"
"git.golaxy.org/core/util/container"
"git.golaxy.org/core/util/iface"
"git.golaxy.org/core/util/option"
"git.golaxy.org/core/util/reinterpret"
"git.golaxy.org/core/util/uid"
"reflect"
)
// NewEntity 创建实体
func NewEntity(settings ...option.Setting[EntityOptions]) Entity {
return UnsafeNewEntity(option.Make(With.Default(), settings...))
}
// Deprecated: UnsafeNewEntity 内部创建实体
func UnsafeNewEntity(options EntityOptions) Entity {
if !options.CompositeFace.IsNil() {
options.CompositeFace.Iface.init(options)
return options.CompositeFace.Iface
}
e := &EntityBehavior{}
e.init(options)
return e.opts.CompositeFace.Iface
}
// Entity 实体接口
type Entity interface {
iEntity
iComponentMgr
iTreeNode
concurrent.CurrentContextProvider
reinterpret.CompositeProvider
fmt.Stringer
// GetId 获取实体Id
GetId() uid.Id
// GetPrototype 获取实体原型
GetPrototype() string
// GetScope 获取可访问作用域
GetScope() Scope
// GetState 获取实体状态
GetState() EntityState
// GetReflected 获取反射值
GetReflected() reflect.Value
// GetMeta 获取Meta信息
GetMeta() Meta
// DestroySelf 销毁自身
DestroySelf()
}
type iEntity interface {
init(opts EntityOptions)
getOptions() *EntityOptions
setId(id uid.Id)
setContext(ctx iface.Cache)
getVersion() int64
setState(state EntityState)
setReflected(v reflect.Value)
eventEntityDestroySelf() event.IEvent
cleanManagedHooks()
}
// EntityBehavior 实体行为,在需要扩展实体能力时,匿名嵌入至实体结构体中
type EntityBehavior struct {
opts EntityOptions
context iface.Cache
componentList container.List[iface.FaceAny]
state EntityState
reflected reflect.Value
treeNodeState TreeNodeState
treeNodeParent Entity
_eventEntityDestroySelf event.Event
eventComponentMgrAddComponents event.Event
eventComponentMgrRemoveComponent event.Event
eventComponentMgrFirstAccessComponent event.Event
eventTreeNodeAddChild event.Event
eventTreeNodeRemoveChild event.Event
eventTreeNodeEnterParent event.Event
eventTreeNodeLeaveParent event.Event
managedHooks []event.Hook
}
// GetId 获取实体Id
func (entity *EntityBehavior) GetId() uid.Id {
return entity.opts.PersistId
}
// GetPrototype 获取实体原型
func (entity *EntityBehavior) GetPrototype() string {
return entity.opts.Prototype
}
// GetScope 获取可访问作用域
func (entity *EntityBehavior) GetScope() Scope {
return entity.opts.Scope
}
// GetState 获取实体状态
func (entity *EntityBehavior) GetState() EntityState {
return entity.state
}
// GetReflected 获取反射值
func (entity *EntityBehavior) GetReflected() reflect.Value {
if entity.reflected.IsValid() {
return entity.reflected
}
entity.reflected = reflect.ValueOf(entity.opts.CompositeFace.Iface)
return entity.reflected
}
// GetMeta 获取Meta信息
func (entity *EntityBehavior) GetMeta() Meta {
return entity.opts.Meta
}
// DestroySelf 销毁自身
func (entity *EntityBehavior) DestroySelf() {
switch entity.GetState() {
case EntityState_Awake, EntityState_Start, EntityState_Living:
_EmitEventEntityDestroySelf(UnsafeEntity(entity), entity.opts.CompositeFace.Iface)
}
}
// GetCurrentContext 获取当前上下文
func (entity *EntityBehavior) GetCurrentContext() iface.Cache {
return entity.context
}
// GetConcurrentContext 解析线程安全的上下文
func (entity *EntityBehavior) GetConcurrentContext() iface.Cache {
return entity.context
}
// GetCompositeFaceCache 支持重新解释类型
func (entity *EntityBehavior) GetCompositeFaceCache() iface.Cache {
return entity.opts.CompositeFace.Cache
}
// String implements fmt.Stringer
func (entity *EntityBehavior) String() string {
return fmt.Sprintf(`{"id":%q, "prototype":%q}`, entity.GetId(), entity.GetPrototype())
}
func (entity *EntityBehavior) init(opts EntityOptions) {
entity.opts = opts
if entity.opts.CompositeFace.IsNil() {
entity.opts.CompositeFace = iface.MakeFace[Entity](entity)
}
entity._eventEntityDestroySelf.Init(false, nil, event.EventRecursion_Discard)
entity.eventComponentMgrAddComponents.Init(false, nil, event.EventRecursion_Allow)
entity.eventComponentMgrRemoveComponent.Init(false, nil, event.EventRecursion_Allow)
entity.eventComponentMgrFirstAccessComponent.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeAddChild.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeRemoveChild.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeEnterParent.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeLeaveParent.Init(false, nil, event.EventRecursion_Allow)
}
func (entity *EntityBehavior) getOptions() *EntityOptions {
return &entity.opts
}
func (entity *EntityBehavior) setId(id uid.Id) {
entity.opts.PersistId = id
}
func (entity *EntityBehavior) setContext(ctx iface.Cache) {
entity.context = ctx
}
func (entity *EntityBehavior) getVersion() int64 {
return entity.componentList.Version()
}
func (entity *EntityBehavior) setState(state EntityState) {
if state <= entity.state {
return
}
entity.state = state
switch entity.state {
case EntityState_Leave:
entity._eventEntityDestroySelf.Close()
entity.eventComponentMgrAddComponents.Close()
entity.eventComponentMgrRemoveComponent.Close()
entity.eventComponentMgrFirstAccessComponent.Close()
case EntityState_Shut:
entity.eventTreeNodeAddChild.Close()
entity.eventTreeNodeRemoveChild.Close()
entity.eventTreeNodeEnterParent.Close()
entity.eventTreeNodeLeaveParent.Close()
}
}
func (entity *EntityBehavior) setReflected(v reflect.Value) {
entity.reflected = v
}
func (entity *EntityBehavior) eventEntityDestroySelf() event.IEvent {
return &entity._eventEntityDestroySelf
}