-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcomponent.go
267 lines (214 loc) · 5.42 KB
/
component.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
package ecs
import (
"fmt"
"reflect"
"unsafe"
)
const (
h4 = uint8(240)
l4 = uint8(15)
zero = uint8(0)
)
type ComponentState uint8
const (
ComponentStateInvalid ComponentState = iota
ComponentStateActive
ComponentStateDisable
)
type ComponentType uint8
const (
ComponentTypeFreeMask ComponentType = 1 << 7
ComponentTypeDisposableMask ComponentType = 1 << 6
)
const (
ComponentTypeNormal ComponentType = 0
ComponentTypeDisposable = 1 | ComponentTypeDisposableMask
ComponentTypeFree = 2 | ComponentTypeFreeMask
ComponentTypeFreeDisposable = 3 | ComponentTypeFreeMask | ComponentTypeDisposableMask
)
type EmptyComponent struct {
Component[EmptyComponent]
}
type IComponent interface {
Owner() Entity
Type() reflect.Type
setOwner(owner Entity)
setState(state ComponentState)
setIntType(typ uint16)
setSeq(seq uint32)
getState() ComponentState
getIntType() uint16
getComponentType() ComponentType
getPermission() ComponentPermission
check(initializer SystemInitConstraint)
getSeq() uint32
newCollection(meta *ComponentMetaInfo) IComponentSet
addToCollection(ct ComponentType, p unsafe.Pointer)
deleteFromCollection(collection interface{})
isValidComponentType() bool
debugAddress() unsafe.Pointer
}
type ComponentObject interface {
__ComponentIdentification()
}
type FreeComponentObject interface {
__ComponentIdentification()
}
type DisposableComponentObject interface {
__ComponentIdentification()
}
type FreeDisposableComponentObject interface {
__ComponentIdentification()
}
type ComponentPointer[T ComponentObject] interface {
IComponent
*T
}
type FreeComponentPointer[T FreeComponentObject] interface {
ComponentPointer[T]
*T
}
type DisposableComponentPointer[T FreeComponentObject] interface {
ComponentPointer[T]
*T
}
type FreeDisposableComponentPointer[T FreeComponentObject] interface {
ComponentPointer[T]
*T
}
type FreeComponent[T ComponentObject] struct {
Component[T]
}
func (f *FreeComponent[T]) getComponentType() ComponentType {
return ComponentTypeFree
}
type DisposableComponent[T ComponentObject] struct {
Component[T]
}
func (f *DisposableComponent[T]) getComponentType() ComponentType {
return ComponentTypeDisposable
}
type FreeDisposableComponent[T ComponentObject] struct {
Component[T]
}
func (f *FreeDisposableComponent[T]) getComponentType() ComponentType {
return ComponentTypeFreeDisposable
}
type componentIdentification struct{}
func (c componentIdentification) __ComponentIdentification() {}
type Component[T ComponentObject] struct {
componentIdentification
st uint8
o1 uint8
it uint16
seq uint32
owner Entity
}
func (c *Component[T]) getComponentType() ComponentType {
return ComponentTypeNormal
}
func (c *Component[T]) addToCollection(ct ComponentType, p unsafe.Pointer) {
cc := (*ComponentSet[T])(p)
var ins *T
if ct&ComponentTypeFreeMask > 0 {
ins, _ = cc.UnorderedCollection.Add(c.rawInstance())
} else {
ins = cc.Add(c.rawInstance(), c.owner)
}
if ins != nil {
(*Component[T])(unsafe.Pointer(ins)).setState(ComponentStateActive)
}
}
func (c *Component[T]) deleteFromCollection(collection interface{}) {
cc, ok := collection.(*ComponentSet[T])
if !ok {
Log.Info("add to collection, collecion is nil")
return
}
c.setState(ComponentStateDisable)
cc.Remove(c.owner)
return
}
func (c *Component[T]) newCollection(meta *ComponentMetaInfo) IComponentSet {
return NewComponentSet[T](meta)
}
func (c *Component[T]) setOwner(entity Entity) {
c.owner = entity
}
func (c *Component[T]) rawInstance() *T {
return (*T)(unsafe.Pointer(c))
}
func (c *Component[T]) instance() IComponent {
return any((*T)(unsafe.Pointer(c))).(IComponent)
}
func (c *Component[T]) setState(state ComponentState) {
c.st = (c.st & l4) | (uint8(state) << 4)
}
func (c *Component[T]) getState() ComponentState {
return ComponentState(c.st & h4 >> 4)
}
func (c *Component[T]) setType(typ ComponentType) {
c.st = (c.st & h4) | uint8(typ)
}
func (c *Component[T]) getType() ComponentType {
return ComponentType(c.st & l4)
}
func (c *Component[T]) setIntType(typ uint16) {
c.it = typ
}
func (c *Component[T]) getIntType() uint16 {
return c.it
}
func (c *Component[T]) setSeq(seq uint32) {
c.seq = seq
}
func (c *Component[T]) getSeq() uint32 {
return c.seq
}
func (c *Component[T]) invalidate() {
c.setState(ComponentStateInvalid)
}
func (c *Component[T]) active() {
c.setState(ComponentStateActive)
}
func (c *Component[T]) Owner() Entity {
return c.owner
}
func (c *Component[T]) Type() reflect.Type {
return TypeOf[T]()
}
func (c *Component[T]) getPermission() ComponentPermission {
return ComponentReadWrite
}
func (c *Component[T]) check(initializer SystemInitConstraint) {
if initializer.isValid() {
panic("out of initialization stage")
}
ins := c.instance()
if !ins.isValidComponentType() {
panic("invalid component type")
}
sys := initializer.getSystem()
sys.World().getOrCreateComponentMetaInfo(ins)
sys.World().getComponentCollection().checkSet(ins)
}
func (c *Component[T]) isValidComponentType() bool {
typ := c.Type()
fieldNum := typ.NumField()
if fieldNum < 1 {
return false
}
for i := 1; i < fieldNum; i++ {
subType := typ.Field(i).Type
if !IsPureValueType(subType) {
return false
}
}
return true
}
func (c *Component[T]) debugAddress() unsafe.Pointer {
return unsafe.Pointer(c)
}
func (c *Component[T]) ToString() string {
return fmt.Sprintf("%+v", c.rawInstance())
}