/
entity.go
220 lines (185 loc) · 5.86 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
213
214
215
216
217
218
219
220
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 {
_Entity
_ComponentMgr
concurrent.CurrentContextProvider
reinterpret.CompositeProvider
fmt.Stringer
// GetId 获取实体Id
GetId() uid.Id
// GetPrototype 获取实体原型
GetPrototype() string
// GetScope 获取可访问作用域
GetScope() Scope
// GetECNodeState 获取EC节点状态
GetECNodeState() ECNodeState
// GetECParent 获取在EC树中的父实体
GetECParent() (Entity, bool)
// GetState 获取实体状态
GetState() EntityState
// GetMeta 获取Meta信息
GetMeta() Meta
// DestroySelf 销毁自身
DestroySelf()
}
type _Entity interface {
init(opts EntityOptions)
getOptions() *EntityOptions
setId(id uid.Id)
setContext(ctx iface.Cache)
getVersion() int64
setECNodeState(state ECNodeState)
setECParent(parent Entity)
setState(state EntityState)
getReflected() reflect.Value
eventEntityDestroySelf() event.IEvent
cleanHooks()
}
// EntityBehavior 实体行为,在需要扩展实体能力时,匿名嵌入至实体结构体中
type EntityBehavior struct {
opts EntityOptions
context iface.Cache
ecNodeState ECNodeState
parent Entity
componentList container.List[iface.FaceAny]
state EntityState
reflected reflect.Value
_eventEntityDestroySelf event.Event
eventCompMgrAddComponents event.Event
eventCompMgrRemoveComponent event.Event
eventCompMgrFirstAccessComponent event.Event
hooks []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
}
// GetECNodeState 获取EC节点状态
func (entity *EntityBehavior) GetECNodeState() ECNodeState {
return entity.ecNodeState
}
// GetECParent 获取在EC树中的父实体
func (entity *EntityBehavior) GetECParent() (Entity, bool) {
return entity.parent, entity.parent != nil
}
// GetState 获取实体状态
func (entity *EntityBehavior) GetState() EntityState {
return entity.state
}
// 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 {
var parentId uid.Id
if parent, ok := entity.GetECParent(); ok {
parentId = parent.GetId()
}
return fmt.Sprintf(`{"id":%q, "prototype":%q, "parent_id":%q, "state":%q}`, entity.GetId(), entity.GetPrototype(), parentId, entity.GetState())
}
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.eventCompMgrAddComponents.Init(false, nil, event.EventRecursion_Allow)
entity.eventCompMgrRemoveComponent.Init(false, nil, event.EventRecursion_Allow)
entity.eventCompMgrFirstAccessComponent.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) setECNodeState(state ECNodeState) {
entity.ecNodeState = state
}
func (entity *EntityBehavior) setECParent(parent Entity) {
entity.parent = parent
}
func (entity *EntityBehavior) setState(state EntityState) {
if state <= entity.state {
return
}
entity.state = state
switch entity.state {
case EntityState_Leave:
entity._eventEntityDestroySelf.Close()
case EntityState_Shut:
entity.eventCompMgrAddComponents.Close()
entity.eventCompMgrRemoveComponent.Close()
entity.eventCompMgrFirstAccessComponent.Close()
}
}
func (entity *EntityBehavior) getReflected() reflect.Value {
if entity.reflected.IsValid() {
return entity.reflected
}
entity.reflected = reflect.ValueOf(entity.opts.CompositeFace.Iface)
return entity.reflected
}
func (entity *EntityBehavior) eventEntityDestroySelf() event.IEvent {
return &entity._eventEntityDestroySelf
}