-
Notifications
You must be signed in to change notification settings - Fork 0
/
components_xxx_ruled_bys.go
200 lines (156 loc) · 3.98 KB
/
components_xxx_ruled_bys.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
package ecs
import (
ecspb "github.com/delaneyj/geck/cmd/example/ecs/pb/gen/ecs/v1"
)
type RuledBy Entity
func RuledByFromEntity(c Entity) RuledBy {
return RuledBy(c)
}
func (c RuledBy) ToEntity() Entity {
return Entity(c)
}
func (w *World) ResetRuledBy() Entity {
return w.EntityFromU32(0)
}
func (c RuledBy) FromEntity(e Entity) RuledBy {
return RuledBy(e)
}
//#region Events
//#endregion
func (e Entity) HasRuledBy() bool {
return e.w.ruledBysStore.Has(e)
}
func (e Entity) ReadRuledBy() (Entity, bool) {
val, ok := e.w.ruledBysStore.Read(e)
if !ok {
return Entity{}, false
}
return Entity(val), true
}
func (e Entity) RemoveRuledBy() Entity {
e.w.ruledBysStore.Remove(e)
return e
}
func (e Entity) WritableRuledBy() (*RuledBy, bool) {
return e.w.ruledBysStore.Writeable(e)
}
func (e Entity) SetRuledBy(other Entity) Entity {
e.w.ruledBysStore.Set(RuledBy(other), e)
e.w.patch.RuledByComponents[e.w.resourceEntity.val] = RuledBy(other).ToPB()
return e
}
func (w *World) SetRuledBys(c RuledBy, entities ...Entity) {
if len(entities) == 0 {
panic("no entities provided, are you sure you didn't mean to call SetRuledByResource?")
}
w.ruledBysStore.Set(c, entities...)
w.patch.RuledByComponents[w.resourceEntity.val] = c.ToPB()
}
func (w *World) RemoveRuledBys(entities ...Entity) {
w.ruledBysStore.Remove(entities...)
for _, entity := range entities {
w.patch.RuledByComponents[entity.val] = nil
}
}
//#region Resources
// HasRuledBy checks if the world has a RuledBy}}
func (w *World) HasRuledByResource() bool {
return w.resourceEntity.HasRuledBy()
}
// Retrieve the RuledBy resource from the world
func (w *World) RuledByResource() (Entity, bool) {
return w.resourceEntity.ReadRuledBy()
}
// Set the RuledBy resource in the world
func (w *World) SetRuledByResource(e Entity) Entity {
w.resourceEntity.SetRuledBy(e)
return w.resourceEntity
}
// Remove the RuledBy resource from the world
func (w *World) RemoveRuledByResource() Entity {
w.resourceEntity.RemoveRuledBy()
return w.resourceEntity
}
//#endregion
//#region Iterators
type RuledByReadIterator struct {
w *World
currIdx int
store *SparseSet[RuledBy]
}
func (iter *RuledByReadIterator) HasNext() bool {
return iter.currIdx < iter.store.Len()
}
func (iter *RuledByReadIterator) NextEntity() Entity {
e := iter.store.dense[iter.currIdx]
iter.currIdx++
return e
}
func (iter *RuledByReadIterator) NextRuledBy() (Entity, RuledBy) {
e := iter.store.dense[iter.currIdx]
c := iter.store.components[iter.currIdx]
iter.currIdx++
return e, c
}
func (iter *RuledByReadIterator) Reset() {
iter.currIdx = 0
}
func (w *World) RuledByReadIter() *RuledByReadIterator {
iter := &RuledByReadIterator{
w: w,
store: w.ruledBysStore,
}
iter.Reset()
return iter
}
type RuledByWriteIterator struct {
w *World
currIdx int
store *SparseSet[RuledBy]
}
func (iter *RuledByWriteIterator) HasNext() bool {
return iter.currIdx >= 0
}
func (iter *RuledByWriteIterator) NextEntity() Entity {
e := iter.store.dense[iter.currIdx]
iter.currIdx--
return e
}
func (iter *RuledByWriteIterator) NextRuledBy() (Entity, *RuledBy) {
e := iter.store.dense[iter.currIdx]
c := &iter.store.components[iter.currIdx]
iter.currIdx--
return e, c
}
func (iter *RuledByWriteIterator) Reset() {
iter.currIdx = iter.store.Len() - 1
}
func (w *World) RuledByWriteIter() *RuledByWriteIterator {
iter := &RuledByWriteIterator{
w: w,
store: w.ruledBysStore,
}
iter.Reset()
return iter
}
//#endregion
func (w *World) RuledByEntities() []Entity {
return w.ruledBysStore.entities()
}
func (w *World) SetRuledBySortFn(lessThan func(a, b Entity) bool) {
w.ruledBysStore.LessThan = lessThan
}
func (w *World) SortRuledBys() {
w.ruledBysStore.Sort()
}
func (w *World) ApplyRuledByPatch(e Entity, patch *ecspb.RuledByComponent) Entity {
c := RuledBy(w.EntityFromU32(patch.Entity))
e.w.ruledBysStore.Set(c, e)
return e
}
func (c RuledBy) ToPB() *ecspb.RuledByComponent {
pb := &ecspb.RuledByComponent{
Entity: c.val,
}
return pb
}