-
Notifications
You must be signed in to change notification settings - Fork 9
/
system.go
292 lines (242 loc) · 5.66 KB
/
system.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package ecs
import (
"reflect"
"sync"
"unsafe"
)
type SystemState uint8
const (
SystemStateInvalid SystemState = iota
SystemStateInit
SystemStateStart
SystemStatePause
SystemStateUpdate
SystemStateDestroy
SystemStateDestroyed
)
type SystemInitConstraint struct {
sys *ISystem
}
func (s *SystemInitConstraint) getSystem() ISystem {
if s.sys == nil {
panic("out of initialization stage")
}
return *s.sys
}
func (s *SystemInitConstraint) SetBroken(reason string) {
(*s.sys).setBroken()
panic(reason)
}
func (s *SystemInitConstraint) isValid() bool {
return *s.sys == nil
}
type ISystem interface {
Type() reflect.Type
Order() Order
World() IWorld
GetRequirements() map[reflect.Type]IRequirement
IsRequire(component IComponent) bool
ID() int64
GetUtility() IUtility
pause()
resume()
stop()
getPointer() unsafe.Pointer
isRequire(componentType reflect.Type) bool
setOrder(order Order)
setRequirements(initializer SystemInitConstraint, rqs ...IRequirement)
getState() SystemState
setState(state SystemState)
setSecurity(isSafe bool)
isThreadSafe() bool
setExecuting(isExecuting bool)
isExecuting() bool
baseInit(world *ecsWorld, ins ISystem)
getOptimizer() *OptimizerReporter
getGetterCache() *GetterCache
setBroken()
isValid() bool
setUtility(u IUtility)
}
type SystemObject interface {
__SystemIdentification()
}
type SystemPointer[T SystemObject] interface {
ISystem
*T
}
type systemIdentification struct{}
func (s systemIdentification) __SystemIdentification() {}
type System[T SystemObject] struct {
systemIdentification
lock sync.Mutex
requirements map[reflect.Type]IRequirement
getterCache *GetterCache
order Order
optimizerReporter *OptimizerReporter
world *ecsWorld
utility IUtility
realType reflect.Type
state SystemState
valid bool
isSafe bool
executing bool
id int64
}
func (s *System[T]) instance() (sys ISystem) {
(*iface)(unsafe.Pointer(&sys)).data = unsafe.Pointer(s)
return
}
func (s *System[T]) rawInstance() *T {
return (*T)(unsafe.Pointer(s))
}
func (s *System[T]) ID() int64 {
if s.id == 0 {
s.id = LocalUniqueID()
}
return s.id
}
func (s *System[T]) SetRequirements(initializer SystemInitConstraint, rqs ...IRequirement) {
if initializer.isValid() {
panic("out of initialization stage")
}
s.setRequirements(initializer, rqs...)
}
func (s *System[T]) setRequirementsInternal(rqs ...IRequirement) {
if s.requirements == nil {
s.requirements = map[reflect.Type]IRequirement{}
}
var typ reflect.Type
for _, value := range rqs {
typ = value.Type()
s.requirements[typ] = value
}
}
func (s *System[T]) isInitialized() bool {
return s.state >= SystemStateInit
}
func (s *System[T]) setRequirements(initializer SystemInitConstraint, rqs ...IRequirement) {
if s.requirements == nil {
s.requirements = map[reflect.Type]IRequirement{}
}
var typ reflect.Type
for _, value := range rqs {
typ = value.Type()
value.check(initializer)
s.requirements[typ] = value
s.World().getComponentMetaInfoByType(typ)
}
}
func (s *System[T]) setUtility(u IUtility) {
s.utility = u
}
func (s *System[T]) setSecurity(isSafe bool) {
s.isSafe = isSafe
}
func (s *System[T]) isThreadSafe() bool {
return s.isSafe
}
func (s *System[T]) GetUtility() IUtility {
return s.utility
}
func (s *System[T]) pause() {
if s.getState() == SystemStateUpdate {
s.setState(SystemStatePause)
}
}
func (s *System[T]) resume() {
if s.getState() == SystemStatePause {
s.setState(SystemStateUpdate)
}
}
func (s *System[T]) stop() {
if s.getState() < SystemStateDestroy {
s.setState(SystemStateDestroy)
}
}
func (s *System[T]) getState() SystemState {
return s.state
}
func (s *System[T]) setState(state SystemState) {
s.state = state
}
func (s *System[T]) setBroken() {
s.valid = false
}
func (s *System[T]) isValid() bool {
return s.valid
}
func (s *System[T]) setExecuting(isExecuting bool) {
s.executing = isExecuting
}
func (s *System[T]) isExecuting() bool {
return s.executing
}
func (s *System[T]) GetRequirements() map[reflect.Type]IRequirement {
return s.requirements
}
func (s *System[T]) IsRequire(com IComponent) bool {
return s.isRequire(com.Type())
}
func (s *System[T]) isRequire(typ reflect.Type) bool {
_, ok := s.requirements[typ]
return ok
}
func (s *System[T]) baseInit(world *ecsWorld, ins ISystem) {
s.requirements = map[reflect.Type]IRequirement{}
s.getterCache = NewGetterCache(len(s.requirements))
if ins.Order() == OrderInvalid {
s.setOrder(OrderDefault)
}
s.world = world
s.valid = true
initializer := SystemInitConstraint{}
is := ISystem(s)
initializer.sys = &is
if i, ok := ins.(InitReceiver); ok {
err := TryAndReport(func() error {
return i.Init(initializer)
})
if err != nil {
Log.Error(err)
}
}
*initializer.sys = nil
initializer.sys = nil
s.state = SystemStateStart
}
func (s *System[T]) getPointer() unsafe.Pointer {
return unsafe.Pointer(s)
}
func (s *System[T]) Type() reflect.Type {
if s.realType == nil {
s.realType = TypeOf[T]()
}
return s.realType
}
func (s *System[T]) setOrder(order Order) {
if s.isInitialized() {
return
}
s.order = order
}
func (s *System[T]) Order() Order {
return s.order
}
func (s *System[T]) World() IWorld {
return s.world
}
func (s *System[T]) GetEntityInfo(entity Entity) (*EntityInfo, bool) {
return s.world.getEntityInfo(entity)
}
// get optimizer
func (s *System[T]) getOptimizer() *OptimizerReporter {
if s.optimizerReporter == nil {
s.optimizerReporter = &OptimizerReporter{}
s.optimizerReporter.init()
}
return s.optimizerReporter
}
func (s *System[T]) getGetterCache() *GetterCache {
return s.getterCache
}