forked from Argus-Labs/world-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_test.go
104 lines (89 loc) · 2.43 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
package cardinal_test
import (
"errors"
"testing"
"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"
)
func HealthSystem(wCtx cardinal.WorldContext) error {
var errs []error
errs = append(errs, cardinal.NewSearch().Entity(filter.
Exact(filter.Component[Health]())).
Each(wCtx, func(id types.EntityID) bool {
errs = append(errs, cardinal.UpdateComponent[Health](wCtx, id, func(h *Health) *Health {
h.Value++
return h
}))
return true
}))
err := errors.Join(errs...)
return err
}
func TestSystemExample(t *testing.T) {
tf := cardinal.NewTestFixture(t, nil)
world, doTick := tf.World, tf.DoTick
assert.NilError(t, cardinal.RegisterComponent[Health](world))
err := cardinal.RegisterSystems(world, HealthSystem)
assert.NilError(t, err)
worldCtx := cardinal.NewWorldContext(world)
doTick()
ids, err := cardinal.CreateMany(worldCtx, 100, Health{})
assert.NilError(t, err)
// Make sure we have 100 entities all with a health of 0
for _, id := range ids {
var health *Health
health, err = cardinal.GetComponent[Health](worldCtx, id)
assert.NilError(t, err)
assert.Equal(t, 0, health.Value)
}
// do 5 ticks
for i := 0; i < 5; i++ {
doTick()
}
// Health should be 5 for everyone
for _, id := range ids {
var health *Health
health, err = cardinal.GetComponent[Health](worldCtx, id)
assert.NilError(t, err)
assert.Equal(t, 5, health.Value)
}
}
func TestCanRegisterMultipleSystem(t *testing.T) {
tf := cardinal.NewTestFixture(t, nil)
world, doTick := tf.World, tf.DoTick
var firstSystemCalled bool
var secondSystemCalled bool
firstSystem := func(cardinal.WorldContext) error {
firstSystemCalled = true
return nil
}
secondSystem := func(cardinal.WorldContext) error {
secondSystemCalled = true
return nil
}
err := cardinal.RegisterSystems(world, firstSystem, secondSystem)
assert.NilError(t, err)
doTick()
assert.Check(t, firstSystemCalled)
assert.Check(t, secondSystemCalled)
}
func TestInitSystemRunsOnce(t *testing.T) {
tf := cardinal.NewTestFixture(t, nil)
w := tf.World
count := 0
count2 := 0
err := cardinal.RegisterInitSystems(w, func(_ cardinal.WorldContext) error {
count++
return nil
}, func(_ cardinal.WorldContext) error {
count2 += 2
return nil
})
assert.NilError(t, err)
tf.DoTick()
tf.DoTick()
assert.Equal(t, count, 1)
assert.Equal(t, count2, 2)
}