-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
115 lines (97 loc) · 2.83 KB
/
example_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
package archeserde_test
import (
"fmt"
"math/rand"
archeserde "github.com/mlange-42/arche-serde"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/arche/generic"
)
const (
width = 40
height = 12
)
type Coords struct {
X int
Y int
}
func Example() {
rng := rand.New(rand.NewSource(42))
// Create a world.
world := ecs.NewWorld()
// Populate the world with entities, components and resources.
builder := generic.NewMap1[Coords](&world)
query := builder.NewBatchQ(60)
for query.Next() {
coord := query.Get()
coord.X = rng.Intn(width)
coord.Y = rng.Intn(height)
}
// Print the original world
fmt.Println("====== Original world ========")
printWorld(&world)
// Serialize the world.
jsonData, err := archeserde.Serialize(&world)
if err != nil {
fmt.Printf("could not serialize: %s\n", err)
return
}
// Print the resulting JSON.
//fmt.Println(string(jsonData))
// Create a new, empty world.
newWorld := ecs.NewWorld()
// Register required components and resources
_ = ecs.ComponentID[Coords](&newWorld)
// Deserialize into the new world.
err = archeserde.Deserialize(jsonData, &newWorld)
if err != nil {
fmt.Printf("could not deserialize: %s\n", err)
return
}
// Print the deserialized world
fmt.Println("====== Deserialized world ========")
printWorld(&newWorld)
// Output: ====== Original world ========
// --------------------------------O-O---O-
// -----------------------O----------------
// -O-------------O------OO--------------O-
// ----O------------------------OOO--------
// O--------------OO-O---------------------
// ------------O-----------O---------------
// --O-------------O-------O---O------O----
// O-O-----O----OOO-O--O--------------OO---
// -----------O---OO----O--O------------O--
// ------------O-----O---------------------
// ---O---------------O------O--O----------
// ------O-OO--O---------OO-OOO-----------O
// ====== Deserialized world ========
// --------------------------------O-O---O-
// -----------------------O----------------
// -O-------------O------OO--------------O-
// ----O------------------------OOO--------
// O--------------OO-O---------------------
// ------------O-----------O---------------
// --O-------------O-------O---O------O----
// O-O-----O----OOO-O--O--------------OO---
// -----------O---OO----O--O------------O--
// ------------O-----O---------------------
// ---O---------------O------O--O----------
// ------O-OO--O---------OO-OOO-----------O
}
func printWorld(world *ecs.World) {
grid := make([][]rune, height)
for i := range grid {
grid[i] = make([]rune, width)
for j := range grid[i] {
grid[i][j] = '-'
}
}
filter := generic.NewFilter1[Coords]()
query := filter.Query(world)
for query.Next() {
coords := query.Get()
grid[coords.Y][coords.X] = 'O'
}
for i := 0; i < len(grid); i++ {
fmt.Println(string(grid[i]))
}
}