-
Notifications
You must be signed in to change notification settings - Fork 0
/
components_xxx_allied_withs.go
246 lines (196 loc) · 5.07 KB
/
components_xxx_allied_withs.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
package ecs
import (
ecspb "github.com/delaneyj/geck/cmd/example/ecs/pb/gen/ecs/v1"
)
type AlliedWith []Entity
func AlliedWithFromEntity(c []Entity) AlliedWith {
return AlliedWith(c)
}
func (c AlliedWith) ToEntity() []Entity {
return []Entity(c)
}
func (c AlliedWith) ToEntities() []Entity {
entities := make([]Entity, len(c))
copy(entities, c)
return entities
}
func (c AlliedWith) ToU32s() []uint32 {
u32s := make([]uint32, len(c))
for i, e := range c {
u32s[i] = e.val
}
return u32s
}
func AlliedWithFromEntities(e ...Entity) AlliedWith {
c := make(AlliedWith, len(e))
copy(c, e)
return c
}
//#region Events
//#endregion
func (e Entity) HasAlliedWith() bool {
return e.w.alliedWithsStore.Has(e)
}
func (e Entity) ReadAlliedWith() ([]Entity, bool) {
entities, ok := e.w.alliedWithsStore.Read(e)
if !ok {
return nil, false
}
return entities, true
}
func (e Entity) AlliedWithContains(other Entity) bool {
entities, ok := e.w.alliedWithsStore.Read(e)
if !ok {
return false
}
for _, entity := range entities {
if entity == other {
return true
}
}
return false
}
func (e Entity) RemoveAlliedWith(toRemove ...Entity) Entity {
entities, ok := e.w.alliedWithsStore.Read(e)
if !ok {
return e
}
clean := make([]Entity, 0, len(entities))
for _, tr := range toRemove {
for _, entity := range entities {
if entity != tr {
clean = append(clean, entity)
}
}
}
e.w.alliedWithsStore.Set(clean, e)
e.w.patch.AlliedWithComponents[e.val] = nil
return e
}
func (e Entity) RemoveAllAlliedWith() Entity {
e.w.alliedWithsStore.Remove(e)
return e
}
func (e Entity) WritableAlliedWith() (*AlliedWith, bool) {
return e.w.alliedWithsStore.Writeable(e)
}
func (e Entity) SetAlliedWith(other ...Entity) Entity {
e.w.alliedWithsStore.Set(AlliedWith(other), e)
e.w.patch.AlliedWithComponents[e.w.resourceEntity.val] = AlliedWith(other).ToPB()
return e
}
func (w *World) SetAlliedWiths(c AlliedWith, entities ...Entity) {
if len(entities) == 0 {
panic("no entities provided, are you sure you didn't mean to call SetAlliedWithResource?")
}
w.alliedWithsStore.Set(c, entities...)
w.patch.AlliedWithComponents[w.resourceEntity.val] = c.ToPB()
}
func (w *World) RemoveAlliedWiths(entities ...Entity) {
w.alliedWithsStore.Remove(entities...)
for _, entity := range entities {
w.patch.AlliedWithComponents[entity.val] = nil
}
}
//#region Resources
// HasAlliedWith checks if the world has a AlliedWith}}
func (w *World) HasAlliedWithResource() bool {
return w.resourceEntity.HasAlliedWith()
}
// Retrieve the AlliedWith resource from the world
func (w *World) AlliedWithResource() ([]Entity, bool) {
return w.resourceEntity.ReadAlliedWith()
}
// Set the AlliedWith resource in the world
func (w *World) SetAlliedWithResource(e ...Entity) Entity {
w.resourceEntity.SetAlliedWith(e...)
return w.resourceEntity
}
// Remove the AlliedWith resource from the world
func (w *World) RemoveAlliedWithResource() Entity {
w.resourceEntity.RemoveAlliedWith()
return w.resourceEntity
}
//#endregion
//#region Iterators
type AlliedWithReadIterator struct {
w *World
currIdx int
store *SparseSet[AlliedWith]
}
func (iter *AlliedWithReadIterator) HasNext() bool {
return iter.currIdx < iter.store.Len()
}
func (iter *AlliedWithReadIterator) NextEntity() Entity {
e := iter.store.dense[iter.currIdx]
iter.currIdx++
return e
}
func (iter *AlliedWithReadIterator) NextAlliedWith() (Entity, AlliedWith) {
e := iter.store.dense[iter.currIdx]
c := iter.store.components[iter.currIdx]
iter.currIdx++
return e, c
}
func (iter *AlliedWithReadIterator) Reset() {
iter.currIdx = 0
}
func (w *World) AlliedWithReadIter() *AlliedWithReadIterator {
iter := &AlliedWithReadIterator{
w: w,
store: w.alliedWithsStore,
}
iter.Reset()
return iter
}
type AlliedWithWriteIterator struct {
w *World
currIdx int
store *SparseSet[AlliedWith]
}
func (iter *AlliedWithWriteIterator) HasNext() bool {
return iter.currIdx >= 0
}
func (iter *AlliedWithWriteIterator) NextEntity() Entity {
e := iter.store.dense[iter.currIdx]
iter.currIdx--
return e
}
func (iter *AlliedWithWriteIterator) NextAlliedWith() (Entity, *AlliedWith) {
e := iter.store.dense[iter.currIdx]
c := &iter.store.components[iter.currIdx]
iter.currIdx--
return e, c
}
func (iter *AlliedWithWriteIterator) Reset() {
iter.currIdx = iter.store.Len() - 1
}
func (w *World) AlliedWithWriteIter() *AlliedWithWriteIterator {
iter := &AlliedWithWriteIterator{
w: w,
store: w.alliedWithsStore,
}
iter.Reset()
return iter
}
//#endregion
func (w *World) AlliedWithEntities() []Entity {
return w.alliedWithsStore.entities()
}
func (w *World) SetAlliedWithSortFn(lessThan func(a, b Entity) bool) {
w.alliedWithsStore.LessThan = lessThan
}
func (w *World) SortAlliedWiths() {
w.alliedWithsStore.Sort()
}
func (w *World) ApplyAlliedWithPatch(e Entity, patch *ecspb.AlliedWithComponent) Entity {
c := AlliedWith(w.EntitiesFromU32s(patch.Entity...))
e.w.alliedWithsStore.Set(c, e)
return e
}
func (c AlliedWith) ToPB() *ecspb.AlliedWithComponent {
pb := &ecspb.AlliedWithComponent{
Entity: c.ToU32s(),
}
return pb
}