/
components_example_directions.go
193 lines (150 loc) · 4.11 KB
/
components_example_directions.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
package ecs
import (
ecspb "github.com/delaneyj/geck/cmd/example/ecs/pb/gen/ecs/v1"
)
type Direction EnumDirection
func DirectionFromEnumDirection(c EnumDirection) Direction {
return Direction(c)
}
func (c Direction) ToEnumDirection() EnumDirection {
return EnumDirection(c)
}
func (w *World) ResetDirection() EnumDirection {
return EnumDirection(0)
}
//#region Events
//#endregion
func (e Entity) HasDirection() bool {
return e.w.directionsStore.Has(e)
}
func (e Entity) ReadDirection() (Direction, bool) {
return e.w.directionsStore.Read(e)
}
func (e Entity) RemoveDirection() Entity {
e.w.directionsStore.Remove(e)
e.w.patch.DirectionComponents[e.val] = nil
return e
}
func (e Entity) WritableDirection() (*Direction, bool) {
return e.w.directionsStore.Writeable(e)
}
func (e Entity) SetDirection(other Direction) Entity {
e.w.directionsStore.Set(other, e)
e.w.patch.DirectionComponents[e.w.resourceEntity.val] = Direction(other).ToPB()
return e
}
func (w *World) SetDirections(c Direction, entities ...Entity) {
if len(entities) == 0 {
panic("no entities provided, are you sure you didn't mean to call SetDirectionResource?")
}
w.directionsStore.Set(c, entities...)
w.patch.DirectionComponents[w.resourceEntity.val] = c.ToPB()
}
func (w *World) RemoveDirections(entities ...Entity) {
w.directionsStore.Remove(entities...)
for _, entity := range entities {
w.patch.DirectionComponents[entity.val] = nil
}
}
//#region Resources
// HasDirection checks if the world has a Direction}}
func (w *World) HasDirectionResource() bool {
return w.resourceEntity.HasDirection()
}
// Retrieve the Direction resource from the world
func (w *World) DirectionResource() (Direction, bool) {
return w.resourceEntity.ReadDirection()
}
// Set the Direction resource in the world
func (w *World) SetDirectionResource(c Direction) Entity {
w.resourceEntity.SetDirection(c)
return w.resourceEntity
}
// Remove the Direction resource from the world
func (w *World) RemoveDirectionResource() Entity {
w.resourceEntity.RemoveDirection()
return w.resourceEntity
}
//#endregion
//#region Iterators
type DirectionReadIterator struct {
w *World
currIdx int
store *SparseSet[Direction]
}
func (iter *DirectionReadIterator) HasNext() bool {
return iter.currIdx < iter.store.Len()
}
func (iter *DirectionReadIterator) NextEntity() Entity {
e := iter.store.dense[iter.currIdx]
iter.currIdx++
return e
}
func (iter *DirectionReadIterator) NextDirection() (Entity, Direction) {
e := iter.store.dense[iter.currIdx]
c := iter.store.components[iter.currIdx]
iter.currIdx++
return e, c
}
func (iter *DirectionReadIterator) Reset() {
iter.currIdx = 0
}
func (w *World) DirectionReadIter() *DirectionReadIterator {
iter := &DirectionReadIterator{
w: w,
store: w.directionsStore,
}
iter.Reset()
return iter
}
type DirectionWriteIterator struct {
w *World
currIdx int
store *SparseSet[Direction]
}
func (iter *DirectionWriteIterator) HasNext() bool {
return iter.currIdx >= 0
}
func (iter *DirectionWriteIterator) NextEntity() Entity {
e := iter.store.dense[iter.currIdx]
iter.currIdx--
return e
}
func (iter *DirectionWriteIterator) NextDirection() (Entity, *Direction) {
e := iter.store.dense[iter.currIdx]
c := &iter.store.components[iter.currIdx]
iter.currIdx--
return e, c
}
func (iter *DirectionWriteIterator) Reset() {
iter.currIdx = iter.store.Len() - 1
}
func (w *World) DirectionWriteIter() *DirectionWriteIterator {
iter := &DirectionWriteIterator{
w: w,
store: w.directionsStore,
}
iter.Reset()
return iter
}
//#endregion
func (w *World) DirectionEntities() []Entity {
return w.directionsStore.entities()
}
func (w *World) SetDirectionSortFn(lessThan func(a, b Entity) bool) {
w.directionsStore.LessThan = lessThan
}
func (w *World) SortDirections() {
w.directionsStore.Sort()
}
func (w *World) ApplyDirectionPatch(e Entity, patch *ecspb.DirectionComponent) Entity {
c := Direction(patch.Values)
e.w.directionsStore.Set(c, e)
return e
}
func (c Direction) ToPB() *ecspb.DirectionComponent {
pb := &ecspb.DirectionComponent{
Values: ecspb.DirectionEnum(c.ToEnumDirection()),
}
return pb
}