-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_to_layers.go
73 lines (61 loc) · 1.58 KB
/
grid_to_layers.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
package observer
import "github.com/mlange-42/arche/ecs"
// GridToLayers creates an observer that serves as adapter from multiple [Grid] observers to a [GridLayers] observer.
func GridToLayers(obs ...Grid) GridLayers {
if len(obs) == 0 {
panic("no observers given")
}
return &gridToLayers{
Observers: obs,
}
}
// gridToLayers is an observer that serves as adapter from multiple [Grid] observers to a [GridLayers] observer.
type gridToLayers struct {
Observers []Grid
values [][]float64
}
// Initialize the child observer.
func (o *gridToLayers) Initialize(w *ecs.World) {
for i, obs := range o.Observers {
obs.Initialize(w)
if i == 0 {
continue
}
obs0 := o.Observers[0]
w1, h1 := obs0.Dims()
w2, h2 := obs.Dims()
if w1 != w2 || h1 != h2 {
panic("grids for layers have different dimensions")
}
}
o.values = make([][]float64, len(o.Observers))
}
// Update the child observer.
func (o *gridToLayers) Update(w *ecs.World) {
for _, obs := range o.Observers {
obs.Update(w)
}
}
// Dims returns the matrix dimensions.
func (o *gridToLayers) Dims() (int, int) {
return o.Observers[0].Dims()
}
// Layers returns the number of layers.
func (o *gridToLayers) Layers() int {
return len(o.Observers)
}
// Values for the current model tick.
func (o *gridToLayers) Values(w *ecs.World) [][]float64 {
for i, obs := range o.Observers {
o.values[i] = obs.Values(w)
}
return o.values
}
// X axis coordinates.
func (o *gridToLayers) X(c int) float64 {
return o.Observers[0].X(c)
}
// Y axis coordinates.
func (o *gridToLayers) Y(r int) float64 {
return o.Observers[0].Y(r)
}