-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcomponent_collection.go
277 lines (240 loc) · 6.45 KB
/
component_collection.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
package ecs
import (
"reflect"
"sync"
"unsafe"
)
type CollectionOperate uint8
const (
CollectionOperateNone CollectionOperate = iota
CollectionOperateAdd //add component operation
CollectionOperateDelete //delete component operation
CollectionOperateDeleteAll //delete component by type operation
)
type IComponentCollection interface {
operate(op CollectionOperate, entity Entity, component IComponent)
deleteOperate(op CollectionOperate, entity Entity, it uint16)
getTempTasks() []func()
clearDisposable()
getComponentSet(typ reflect.Type) IComponentSet
getComponentSetByIntType(typ uint16) IComponentSet
getCollections() *SparseArray[uint16, IComponentSet]
checkSet(com IComponent)
}
type ComponentCollection struct {
collections *SparseArray[uint16, IComponentSet]
world *ecsWorld
bucket int64
locks []sync.RWMutex
opLog []map[reflect.Type]*opTaskList
}
func NewComponentCollection(world *ecsWorld, k int) *ComponentCollection {
cc := &ComponentCollection{
world: world,
collections: NewSparseArray[uint16, IComponentSet](),
}
for i := 1; ; i++ {
if c := int64(1 << i); int64(k) < c {
cc.bucket = c - 1
break
}
}
cc.bucket = 0
cc.locks = make([]sync.RWMutex, cc.bucket+1)
for i := int64(0); i < cc.bucket+1; i++ {
cc.locks[i] = sync.RWMutex{}
}
cc.opLog = make([]map[reflect.Type]*opTaskList, cc.bucket+1)
cc.initOptTemp()
return cc
}
func (c *ComponentCollection) initOptTemp() {
for index := range c.opLog {
c.locks[index].Lock()
c.opLog[index] = make(map[reflect.Type]*opTaskList)
c.locks[index].Unlock()
}
}
func (c *ComponentCollection) operate(op CollectionOperate, entity Entity, component IComponent) {
var hash int64
switch component.getComponentType() {
case ComponentTypeFree, ComponentTypeFreeDisposable:
hash = int64((uintptr)(unsafe.Pointer(&hash))) & c.bucket
case ComponentTypeNormal, ComponentTypeDisposable:
hash = int64(entity) & c.bucket
}
typ := component.Type()
newOpt := opTaskPool.Get()
newOpt.target = entity
newOpt.com = component
newOpt.op = op
b := c.opLog[hash]
c.locks[hash].Lock()
defer c.locks[hash].Unlock()
tl, ok := b[typ]
if !ok {
tl = &opTaskList{}
b[typ] = tl
}
tl.Append(newOpt)
}
func (c *ComponentCollection) deleteOperate(op CollectionOperate, entity Entity, it uint16) {
var hash int64
meta := c.world.componentMeta.GetComponentMetaInfoByIntType(it)
if meta.componentType&ComponentTypeFreeMask > 0 {
hash = int64((uintptr)(unsafe.Pointer(&hash))) & c.bucket
} else {
hash = int64(entity) & c.bucket
}
typ := meta.typ
newOpt := opTaskPool.Get()
newOpt.target = entity
newOpt.com = nil
newOpt.op = op
b := c.opLog[hash]
c.locks[hash].Lock()
defer c.locks[hash].Unlock()
tl, ok := b[typ]
if !ok {
tl = &opTaskList{}
b[typ] = tl
}
tl.Append(newOpt)
}
func (c *ComponentCollection) clearDisposable() {
disposable := c.world.componentMeta.GetDisposableTypes()
for r, _ := range disposable {
meta := c.world.getComponentMetaInfoByType(r)
if meta.componentType&ComponentTypeFreeMask > 0 {
continue
}
set := c.collections.Get(meta.it)
if set == nil {
return
}
(*set).Range(func(com IComponent) bool {
info, ok := c.world.entities.GetEntityInfo(com.Owner())
if ok {
info.removeFromCompound(meta.it)
}
return true
})
(*set).Clear()
}
}
func (c *ComponentCollection) clearFree() {
free := c.world.componentMeta.GetFreeTypes()
for r, _ := range free {
meta := c.world.getComponentMetaInfoByType(r)
set := c.collections.Get(meta.it)
if set == nil {
return
}
(*set).Clear()
}
}
func (c *ComponentCollection) getTempTasks() []func() {
combination := make(map[reflect.Type]*opTaskList)
for i := 0; i < len(c.opLog); i++ {
c.locks[i].RLock()
for typ, list := range c.opLog[i] {
if list.Len() == 0 {
continue
}
if _, ok := combination[typ]; ok {
combination[typ].Combine(list)
} else {
combination[typ] = list.Clone()
}
list.Reset()
}
c.locks[i].RUnlock()
}
var tasks []func()
for typ, list := range combination {
taskList := list
if taskList.Len() == 0 {
continue
}
meta := c.world.getComponentMetaInfoByType(typ)
setp := c.collections.Get(meta.it)
if setp == nil {
c.checkSet(taskList.head.com)
setp = c.collections.Get(meta.it)
}
fn := func() {
c.opExecute(taskList, *setp)
}
tasks = append(tasks, fn)
}
fn := func() {
for typ, list := range combination {
meta := c.world.getComponentMetaInfoByType(typ)
for task := list.head; task != nil; task = task.next {
if task.op == CollectionOperateDelete {
continue
}
info, ok := c.world.getEntityInfo(task.target)
if ok {
switch task.op {
case CollectionOperateAdd:
switch task.com.getComponentType() {
case ComponentTypeNormal, ComponentTypeDisposable:
info.addToCompound(meta.it)
}
case CollectionOperateDelete:
switch task.com.getComponentType() {
case ComponentTypeNormal, ComponentTypeDisposable:
info.removeFromCompound(meta.it)
}
}
}
}
}
}
tasks = append(tasks, fn)
return tasks
}
func (c *ComponentCollection) opExecute(taskList *opTaskList, collection IComponentSet) {
meta := collection.GetElementMeta()
for task := taskList.head; task != nil; task = task.next {
switch task.op {
case CollectionOperateAdd:
task.com.setIntType(meta.it)
task.com.setOwner(task.target)
task.com.addToCollection(task.com.getComponentType(), collection.pointer())
case CollectionOperateDelete:
if meta.componentType&ComponentTypeFreeMask == 0 {
collection.Remove(task.target)
}
case CollectionOperateDeleteAll:
collection.Clear()
}
}
next := taskList.head
for next != nil {
task := next
next = next.next
opTaskPool.Put(task)
}
taskList.Reset()
}
func (c *ComponentCollection) getComponentSet(typ reflect.Type) IComponentSet {
meta := c.world.getComponentMetaInfoByType(typ)
return *(c.collections.Get(meta.it))
}
func (c *ComponentCollection) getComponentSetByIntType(it uint16) IComponentSet {
return *(c.collections.Get(it))
}
func (c *ComponentCollection) getCollections() *SparseArray[uint16, IComponentSet] {
return c.collections
}
func (c *ComponentCollection) checkSet(com IComponent) {
typ := com.Type()
meta := c.world.getComponentMetaInfoByType(typ)
isExist := c.collections.Exist(meta.it)
if !isExist {
set := com.newCollection(meta)
c.collections.Add(set.GetElementMeta().it, &set)
}
}