-
Notifications
You must be signed in to change notification settings - Fork 9
/
world_test.go
167 lines (131 loc) · 3.35 KB
/
world_test.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
package ecs
import (
"errors"
"sync"
"testing"
"time"
)
const (
__worldTest_Entity_Count int = 3
)
type __world_Test_C_1 struct {
Component[__world_Test_C_1]
Field1 int
Field2 int
}
type __world_Test_C_2 struct {
Component[__world_Test_C_2]
Field1 int
Field2 int
}
type __world_Test_C_3 struct {
Component[__world_Test_C_3]
Name FixedString[Fixed16]
}
type __world_Test_S_1 struct {
System[__world_Test_S_1]
}
func (w *__world_Test_S_1) Init(si SystemInitConstraint) error {
w.SetRequirements(si, &__world_Test_C_1{}, &__world_Test_C_2{}, &__world_Test_C_3{})
BindUtility[__world_Test_U_Input](si)
return nil
}
func (w *__world_Test_S_1) Update(event Event) {
Log.Infof("Update: %d", event.Frame)
iter := GetComponentAll[__world_Test_C_1](w)
for c := iter.Begin(); !iter.End(); c = iter.Next() {
c2 := GetRelated[__world_Test_C_2](w, c.owner)
if c2 == nil {
Log.Infof("Component(Owner:%d) has no related component __world_Test_C_2", c.Owner())
continue
}
for i := 0; i < testOptimizerDummyMaxFor; i++ {
c.Field1 += i
}
for i := 0; i < testOptimizerDummyMaxFor; i++ {
c2.Field2 += i
}
c3 := GetRelated[__world_Test_C_3](w, c.owner)
Log.Infof("Component(Owner:%d) Changed: __world_Test_C_1: %d, __world_Test_C_2: %d, __world_Test_C_3: %s", c.Owner(), c.Field1, c2.Field2, c3.Name.String())
}
}
type __world_Test_U_Input struct {
Utility[__world_Test_U_Input]
}
func (u *__world_Test_U_Input) ChangeName(entity Entity, name string) {
c := GetComponent[__world_Test_C_3](u.GetSystem(), entity)
if c == nil {
return
}
old := c.Name.String()
c.Name.Set(name)
Log.Infof("Name changed, old: %s, new:%s", old, name)
}
func Test_ecsWorld_World(t *testing.T) {
config := NewDefaultWorldConfig()
world := NewSyncWorld(config)
RegisterSystem[__world_Test_S_1](world)
world.Startup()
entities := make([]Entity, __worldTest_Entity_Count)
for i := 0; i < __worldTest_Entity_Count; i++ {
e1 := world.NewEntity()
world.Add(e1, &__world_Test_C_1{}, &__world_Test_C_2{}, &__world_Test_C_3{})
entities[i] = e1
}
world.Update()
u, ok := GetUtility[__world_Test_U_Input](world)
if ok {
u.ChangeName(entities[0], "name0")
}
world.Update()
world.Stop()
for false {
world.Update()
time.Sleep(time.Second)
}
}
func Test_ecsWorld_World_launcher(t *testing.T) {
config := NewDefaultWorldConfig()
config.FrameInterval = time.Second
world := NewAsyncWorld(config)
RegisterSystem[__world_Test_S_1](world)
world.Startup()
wg := &sync.WaitGroup{}
wg.Add(1)
entities := make([]Entity, __worldTest_Entity_Count)
world.Sync(func(gaw SyncWrapper) error {
for i := 0; i < __worldTest_Entity_Count; i++ {
e1 := gaw.NewEntity()
gaw.Add(e1, &__world_Test_C_1{}, &__world_Test_C_2{}, &__world_Test_C_3{})
entities[i] = e1
}
wg.Done()
return nil
})
wg.Wait()
wg.Add(1)
world.Sync(func(gaw SyncWrapper) error {
u, ok := GetUtility[__world_Test_U_Input](gaw)
if !ok {
return errors.New("utility not found")
}
u.ChangeName(entities[0], "name1")
gaw.DestroyEntity(entities[1])
gaw.Remove(entities[2], &__world_Test_C_2{})
wg.Done()
return nil
})
wg.Wait()
wg.Add(1)
world.Sync(func(gaw SyncWrapper) error {
u, ok := GetUtility[__world_Test_U_Input](gaw)
if !ok {
return errors.New("Utility not found")
}
u.ChangeName(entities[0], "name2")
wg.Done()
return nil
})
wg.Wait()
world.Stop()
}