-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsystem_test.go
122 lines (103 loc) · 2.73 KB
/
system_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
package ecs
import "testing"
type PriorityComponent struct {
throughFirstSystem bool
}
type PriorityEntity struct {
BasicEntity
*PriorityComponent
}
type SystemPriorityFirst struct {
e PriorityEntity
}
func (s *SystemPriorityFirst) Priority() int { return 500 }
func (s *SystemPriorityFirst) Add(b BasicEntity, p *PriorityComponent) {
s.e = PriorityEntity{b, p}
}
func (s *SystemPriorityFirst) Remove(basic BasicEntity) {}
func (s *SystemPriorityFirst) Update(dt float32) {
s.e.throughFirstSystem = true
}
type SystemPrioritySecond struct {
e PriorityEntity
pass bool
}
func (s *SystemPrioritySecond) Priority() int { return 1 }
func (s *SystemPrioritySecond) Add(b BasicEntity, p *PriorityComponent) {
s.e = PriorityEntity{b, p}
}
func (s *SystemPrioritySecond) Remove(basic BasicEntity) {}
func (s *SystemPrioritySecond) Update(dt float32) {
if s.e.throughFirstSystem {
s.pass = true
}
}
// TestSystemPriority tests if systems get added based on priority
func TestSystemPriority(t *testing.T) {
w := &World{}
sys2 := SystemPrioritySecond{}
w.AddSystem(&sys2)
w.AddSystem(&SystemPriorityFirst{})
ent := struct {
BasicEntity
PriorityComponent
}{
BasicEntity: NewBasic(),
}
for _, system := range w.Systems() {
switch sys := system.(type) {
case *SystemPriorityFirst:
sys.Add(ent.BasicEntity, &ent.PriorityComponent)
case *SystemPrioritySecond:
sys.Add(ent.BasicEntity, &ent.PriorityComponent)
}
}
w.Update(1)
if !sys2.pass {
t.Error("Systems were not run in order after updated by the world.")
}
}
type SystemAddRemove struct {
entities []PriorityEntity
}
func (s *SystemAddRemove) Add(b BasicEntity, p *PriorityComponent) {
s.entities = append(s.entities, PriorityEntity{b, p})
}
func (s *SystemAddRemove) Remove(basic BasicEntity) {
delete := -1
for index, e := range s.entities {
if e.BasicEntity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
s.entities = append(s.entities[:delete], s.entities[delete+1:]...)
}
}
func (s *SystemAddRemove) Update(dt float32) {}
// TestAddRemove tests adding and removing entities in systems that don't implement
// SystemAddByInterfacer to the world.
func TestAddRemove(t *testing.T) {
w := &World{}
sys := SystemAddRemove{}
w.AddSystem(&sys)
ent := struct {
BasicEntity
PriorityComponent
}{
BasicEntity: NewBasic(),
}
w.AddEntity(ent)
if len(sys.entities) != 0 {
t.Error("Entity was added even though the system does not implement SystemAddByInterfacer")
}
sys.Add(ent.BasicEntity, &ent.PriorityComponent)
if len(sys.entities) != 1 {
t.Error("Failed to add entity to system")
}
w.RemoveEntity(ent.BasicEntity)
if len(sys.entities) != 0 {
t.Error("Removing the entity from the world did not remove it from the system.")
}
}