-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertion.go
238 lines (206 loc) · 4.69 KB
/
assertion.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
package pt
import (
"fmt"
"git.golaxy.org/core/ec"
"git.golaxy.org/core/internal/exception"
"git.golaxy.org/core/util/types"
"reflect"
"strings"
)
// As 从实体提取一些需要的组件接口,复合在一起直接使用。
/*
示例:
type A interface {
MethodA()
}
...
type B interface {
MethodB()
}
...
type CompositeAB struct {
A
B
}
...
v, ok := As[CompositeAB](entity)
if ok {
v.MethodA()
v.MethodB()
}
注意:
1.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。
2.实体更新组件后,需要重新提取。
3.如果组件使用自定义别名加入实体,那么无法使用此功能提取组件接口。
*/
func As[T comparable](entity ec.Entity) (T, bool) {
iface := types.ZeroT[T]()
vfIface := reflect.ValueOf(&iface).Elem()
if !as(entity, vfIface) {
return types.ZeroT[T](), false
}
return iface, true
}
// Cast 从实体提取一些需要的组件接口,复合在一起直接使用,提取失败会panic。
/*
示例:
type A interface {
MethodA()
}
...
type B interface {
MethodB()
}
...
type CompositeAB struct {
A
B
}
...
Cast[CompositeAB](entity).MethodA()
Cast[CompositeAB](entity).MethodB()
注意:
1.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。
2.实体更新组件后,需要重新提取。
3.如果组件使用自定义别名加入实体,那么无法使用此功能提取组件接口。
*/
func Cast[T comparable](entity ec.Entity) T {
iface, ok := As[T](entity)
if !ok {
panic(fmt.Errorf("%w: incorrect cast", ErrPt))
}
return iface
}
// Compose 创建组件复合器
func Compose[T comparable](entity ec.Entity) *Composite[T] {
if entity == nil {
panic(fmt.Errorf("%w: %w: entity is nil", ErrPt, exception.ErrArgs))
}
return &Composite[T]{
entity: entity,
}
}
// Composite 组件复合器,直接使用As()或Cast()时,无法检测提取后实体是否又更新组件,使用复合器可以解决此问题。
/*
示例:
type A interface {
MethodA()
}
...
type B interface {
MethodB()
}
...
type CompositeAB struct {
A
B
}
...
cx := Compose[CompositeAB](entity)
...
if v, ok := cx.As(); ok {
v.MethodA()
v.MethodB()
...
entity.AddComponent(comp)
...
if v, ok := cx.As(); ok {
v.MethodA()
v.MethodB()
}
}
...
cx.Cast().MethodA()
cx.Cast().MethodB()
...
entity.AddComponent(comp)
...
cx.Cast().MethodA()
cx.Cast().MethodB()
注意:
1.如果组件使用自定义别名加入实体,那么无法使用此功能提取组件接口。
*/
type Composite[T comparable] struct {
entity ec.Entity
version int64
iface T
}
// Entity 实体
func (c *Composite[T]) Entity() ec.Entity {
if c.entity == nil {
panic(fmt.Errorf("%w: setting entity is nil", ErrPt))
}
return c.entity
}
// Changed 实体是否已更新组件
func (c *Composite[T]) Changed() bool {
if c.entity == nil {
panic(fmt.Errorf("%w: setting entity is nil", ErrPt))
}
return c.version != ec.UnsafeEntity(c.entity).GetVersion()
}
// As 从实体提取一些需要的组件接口,复合在一起直接使用(实体更新组件后,会自动重新提取)
func (c *Composite[T]) As() (T, bool) {
if c.entity == nil {
panic(fmt.Errorf("%w: setting entity is nil", ErrPt))
}
if c.iface != types.ZeroT[T]() && !c.Changed() {
return c.iface, true
}
if !as(c.entity, reflect.ValueOf(c.iface)) {
return types.ZeroT[T](), false
}
c.version = ec.UnsafeEntity(c.entity).GetVersion()
return c.iface, true
}
// Cast 从实体提取一些需要的组件接口,复合在一起直接使用,提取失败会panic(实体更新组件后,会自动重新提取)
func (c *Composite[T]) Cast() T {
iface, ok := c.As()
if !ok {
panic(fmt.Errorf("%w: incorrect cast", ErrPt))
}
return iface
}
func as(entity ec.Entity, vfIface reflect.Value) bool {
if entity == nil {
return false
}
sb := strings.Builder{}
sb.Grow(128)
switch vfIface.Kind() {
case reflect.Struct:
for i := 0; i < vfIface.NumField(); i++ {
vfField := vfIface.Field(i)
tfField := vfField.Type()
switch vfField.Kind() {
case reflect.Pointer:
tfField = tfField.Elem()
break
case reflect.Interface:
break
default:
return false
}
sb.Reset()
types.WriteFullNameRT(&sb, tfField)
comp := entity.GetComponent(sb.String())
if comp == nil {
return false
}
vfField.Set(comp.GetReflected())
}
return true
case reflect.Interface:
tfIface := vfIface.Type()
sb.Reset()
types.WriteFullNameRT(&sb, tfIface)
comp := entity.GetComponent(sb.String())
if comp == nil {
return false
}
vfIface.Set(comp.GetReflected())
return true
default:
return false
}
}