-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
303 lines (250 loc) · 7.16 KB
/
examples_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
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
293
294
295
296
297
298
299
300
301
302
303
package ecs_test
import (
"fmt"
"sort"
"strings"
"github.com/Tnze/go-ecs"
)
func ExampleEntity_basic() {
type (
Position struct{ x, y float64 }
Walking struct{}
)
w := ecs.NewWorld()
name := ecs.NewComponent(w)
ecs.SetComp(w, ecs.Entity(name), name, "Name")
position := ecs.NewComponent(w)
ecs.SetComp(w, ecs.Entity(position), name, "Position")
walking := ecs.NewComponent(w)
ecs.SetComp(w, ecs.Entity(walking), name, "Walking")
// Create an entity with name Bob
bob := ecs.NewEntity(w)
ecs.SetComp(w, bob, name, "Bob")
// The set operation finds or creates a component, and sets it.
ecs.SetComp(w, bob, position, Position{10, 20})
// The add operation adds a component without setting a value. This is
// useful for tags, or when adding a component with its default value.
ecs.SetComp(w, bob, walking, Walking{})
// Get the value for the Position component
pos := ecs.GetComp[Position](w, bob, position)
fmt.Printf("{%f, %f}\n", pos.x, pos.y)
// Overwrite the value of the Position component
ecs.SetComp(w, bob, position, Position{20, 30})
// Create another named entity
alice := ecs.NewEntity(w)
ecs.SetComp(w, alice, name, "Alice")
ecs.SetComp(w, alice, position, Position{10, 20})
ecs.SetComp(w, alice, walking, Walking{})
// Print all the Components the entity has. This will output:
// Position, Walking, (Identifier,Name)
fmt.Printf("[%s]\n", ecs.Type(w, alice, name))
// Iterate all entities with Position
ecs.QueryAll(position).Run(w, func(entities []ecs.Entity, data []any) {
p := *data[0].(*[]Position)
for i, e := range entities {
entityName := ecs.GetComp[string](w, e, name)
fmt.Printf("%s: {%f, %f}\n", *entityName, p[i].x, p[i].y)
}
})
// DelComp tag
ecs.DelComp(w, alice, walking)
// Output:
// {10.000000, 20.000000}
// [Name, Position, Walking]
// Bob: {20.000000, 30.000000}
// Alice: {10.000000, 20.000000}
}
func ExampleQueryAll() {
w := ecs.NewWorld()
// Create 10 entities.
var entities [10]ecs.Entity
for i := range entities {
entities[i] = ecs.NewEntity(w)
}
// Create 2 Components.
c1 := ecs.NewComponent(w)
c2 := ecs.NewComponent(w)
// Add Components to entities.
for i, e := range entities[:5] {
ecs.SetComp(w, e, c1, i)
}
for i, e := range entities[3:7] {
ecs.SetComp(w, e, c2, i+3)
}
// Current layout:
//
// entity:[0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
// c1&c2: [ 3 4 ]
// CachedQuery all entities which have both c1 and c2.
ecs.QueryAll(c1, c2).Run(w, func(entities []ecs.Entity, data []any) {
// The type of the data's element is `Table[T]`,
// which can be converted to `[]T` only after type assertion.
fmt.Println(*data[0].(*[]int))
})
// Output:
// [3 4]
}
func ExampleQueryAll_iter() {
w := ecs.NewWorld()
// Create 10 entities.
var entities [10]ecs.Entity
for i := range entities {
entities[i] = ecs.NewEntity(w)
}
// Create 2 Components.
c1 := ecs.NewComponent(w)
c2 := ecs.NewComponent(w)
// Add Components to entities.
for i, e := range entities[:5] {
ecs.SetComp(w, e, c1, i)
}
for i, e := range entities[3:7] {
ecs.SetComp(w, e, c2, i+3)
}
// Current layout:
//
// entity:[0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
// c1&c2: [ 3 4 ]
// CachedQuery all entities which have both c1 and c2.
for entity, components := range ecs.QueryAll(c1, c2).Iter(w) {
// The type of the data's element is `Table[T]`,
// which can be converted to `[]T` only after type assertion.
fmt.Println(entity, components)
}
// Output:
// 3 [3 3]
// 4 [4 4]
}
func ExampleQueryAny() {
w := ecs.NewWorld()
// Create 10 entities.
var entities [10]ecs.Entity
for i := range entities {
entities[i] = ecs.NewEntity(w)
}
// Create 2 Components.
c1 := ecs.NewComponent(w)
c2 := ecs.NewComponent(w)
// Add Components to entities.
for i, e := range entities[:5] {
ecs.SetComp(w, e, c1, int32(i))
}
for i, e := range entities[3:7] {
ecs.SetComp(w, e, c2, int64(i+3))
}
// Current layout:
//
// entity:[0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
// c1&c2: [ 3 4 ]
// CachedQuery all entities which have c1 or c2.
var results []string
ecs.QueryAny(c1, c2).Run(w, func(entities []ecs.Entity, data []any) {
// The type of the data's element is `Table[T]`,
// which can be converted to `[]T` only after type assertion.
var sb strings.Builder
fmt.Fprintf(&sb, "%v:", entities)
if data[0] != nil {
fmt.Fprintf(&sb, " c1: [%v]", *data[0].(*[]int32))
}
if data[1] != nil {
fmt.Fprintf(&sb, " c2: [%v]", *data[1].(*[]int64))
}
results = append(results, sb.String())
})
sort.Strings(results)
fmt.Print(strings.Join(results, "\n"))
// Output:
// [0 1 2]: c1: [[0 1 2]]
// [3 4]: c1: [[3 4]] c2: [[3 4]]
// [5 6]: c2: [[5 6]]
}
func ExampleQueryAny_iter() {
w := ecs.NewWorld()
// Create 10 entities.
var entities [10]ecs.Entity
for i := range entities {
entities[i] = ecs.NewEntity(w)
}
// Create 2 Components.
c1 := ecs.NewComponent(w)
c2 := ecs.NewComponent(w)
// Add Components to entities.
for i, e := range entities[:5] {
ecs.SetComp(w, e, c1, int32(i))
}
for i, e := range entities[3:7] {
ecs.SetComp(w, e, c2, int64(i+3))
}
// Current layout:
//
// entity:[0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
// c1&c2: [ 3 4 ]
// CachedQuery all entities which have c1 or c2.
var results []string
for entity, data := range ecs.QueryAny(c1, c2).Iter(w) {
// The type of the data's element is `Table[T]`,
// which can be converted to `[]T` only after type assertion.
results = append(results, fmt.Sprintf("e%v: [c1: %v c2: %v]", entity, data[0], data[1]))
}
sort.Strings(results)
fmt.Print(strings.Join(results, "\n"))
// Output:
// e0: [c1: 0 c2: <nil>]
// e1: [c1: 1 c2: <nil>]
// e2: [c1: 2 c2: <nil>]
// e3: [c1: 3 c2: 3]
// e4: [c1: 4 c2: 4]
// e5: [c1: <nil> c2: 5]
// e6: [c1: <nil> c2: 6]
}
func ExampleQueryAny_cache_iter() {
w := ecs.NewWorld()
// Create 10 entities.
var entities [10]ecs.Entity
for i := range entities {
entities[i] = ecs.NewEntity(w)
}
// Create 2 Components.
c1 := ecs.NewComponent(w)
c2 := ecs.NewComponent(w)
// Cache query
query := ecs.QueryAny(c1, c2).Cache(w)
// Add Components to entities.
for i, e := range entities[:5] {
ecs.SetComp(w, e, c1, int32(i))
}
for i, e := range entities[3:7] {
ecs.SetComp(w, e, c2, int64(i+3))
}
// Current layout:
//
// entity:[0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
// c1&c2: [ 3 4 ]
// CachedQuery all entities which have c1 or c2.
var results []string
for entity, data := range query.Iter {
// The type of the data's element is `Table[T]`,
// which can be converted to `[]T` only after type assertion.
results = append(results, fmt.Sprintf("e%v: [c1: %v c2: %v]", entity, data[0], data[1]))
}
sort.Strings(results)
fmt.Print(strings.Join(results, "\n"))
// Output:
// e0: [c1: 0 c2: <nil>]
// e1: [c1: 1 c2: <nil>]
// e2: [c1: 2 c2: <nil>]
// e3: [c1: 3 c2: 3]
// e4: [c1: 4 c2: 4]
// e5: [c1: <nil> c2: 5]
// e6: [c1: <nil> c2: 6]
}