-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcommand.go
More file actions
328 lines (280 loc) · 6.97 KB
/
command.go
File metadata and controls
328 lines (280 loc) · 6.97 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package ecs
import "fmt"
type onInsert interface {
Component
OnInsert(ent EntityCommand)
}
// type singleCmd interface {
// apply(*World)
// }
// type spawnCmd struct {
// bundler *Bundler
// }
// func (c spawnCmd) apply(world *World) {
// id := world.NewId()
// c.bundler.Write(world, id)
// }
type CmdType uint8
const (
CmdTypeNone CmdType = iota
CmdTypeSpawn
CmdTypeWrite
CmdTypeTrigger
CmdTypeDelete
// CmdTypeCustom
)
type singleCmd struct {
Type CmdType
id Id
bundler *Bundler
world *World
event Event
}
func (c *singleCmd) apply(world *World) {
switch c.Type {
case CmdTypeNone:
// Do nothing, Command was probably cancelled
case CmdTypeSpawn:
if world.cmd.preWrite != nil {
world.cmd.preWrite(EntityCommand{c})
}
c.bundler.Write(world, c.id) // TODO: This could probably use a Spawn function which would be faster
// if world.cmd.postWrite != nil {
// world.cmd.postWrite(c.id)
// }
case CmdTypeWrite:
if world.cmd.preWrite != nil {
world.cmd.preWrite(EntityCommand{c})
}
c.bundler.Write(world, c.id)
// if world.cmd.postWrite != nil {
// world.cmd.postWrite(c.id)
// }
case CmdTypeTrigger:
world.Trigger(c.event, c.id)
case CmdTypeDelete:
if world.cmd.preDelete != nil {
world.cmd.preDelete(c.id)
}
Delete(world, c.id)
}
}
type EntityCommand struct {
cmd *singleCmd
}
// func (e EntityCommand) Printout() {
// fmt.Println("---")
// for i := range e.cmd.bundler.Components {
// if e.cmd.bundler.Set[i] {
// fmt.Printf("+%v\n", e.cmd.bundler.Components[i])
// }
// }
// // fmt.Printf("+%v\n", e.cmd.bundler)
// }
func (e EntityCommand) Cmd() *CommandQueue{
return e.cmd.world.Cmd()
}
func (e EntityCommand) Cancel() {
e.cmd.Type = CmdTypeNone
e.cmd.id = InvalidEntity
}
// Removes the supplied component type from this entity command.
// TODO: Should this also remove it from the world? if it exists there?
func (e EntityCommand) Remove(comp Component) {
compId := comp.CompId()
e.cmd.bundler.Remove(compId)
}
func (e EntityCommand) Empty() bool {
return (e == EntityCommand{})
}
func (e EntityCommand) Insert(bun Writer) EntityCommand {
if bun == nil {
panic(fmt.Sprintf("EntityCommand.Insert: Nil insert: %T", bun))
}
inserter, ok := bun.(onInsert)
alreadyHas := false
if ok {
alreadyHas = e.cmd.bundler.Has(inserter)
}
unbundle(bun, e.cmd.bundler)
// Only run OnInsert, if the writer supports it and we havent already inserted it to the bundler
if ok && !alreadyHas {
inserter.OnInsert(e)
}
return e
}
// Inserts the component if it is missing
func (e EntityCommand) InsertIfMissing(bun Component) EntityCommand {
if e.cmd.bundler.Has(bun) {
return e
}
if e.cmd.world.hasCompId(e.Id(), bun.CompId()) {
return e
}
unbundle(bun, e.cmd.bundler)
return e
}
func (e EntityCommand) Id() Id {
return e.cmd.id
}
// func (e EntityCommand) Remove(bun Bundle) EntityCommand {
// bun.Unbundle(e.cmd.bundler)
// return e
// }
// func (e EntityCommand) Add(seq iter.Seq[Component]) EntityCommand {
// for c := range seq {
// e.cmd.bundler.Add(c)
// }
// return e
// }
func ReadComp[T Component](e EntityCommand) (T, bool) {
var t T
// comp, ok := e.cmd.bundler.Read(t)
// if ok {
// box := comp.(*box[T])
// return box.val, true
// }
comp, ok := readBundle[T](e.cmd.bundler)
if ok {
return comp, true
}
return t, false
}
type CommandQueue struct {
world *World
preWrite func(EntityCommand)
preDelete func(Id)
commands []singleCmd
currentBundlerIndex int
bundlers []*Bundler
}
func NewCommandQueue(world *World) *CommandQueue {
return &CommandQueue{
world: world,
}
}
func (c *CommandQueue) Initialize(world *World) any {
return NewCommandQueue(world)
}
func (c *CommandQueue) NextBundler() *Bundler {
if c.currentBundlerIndex >= len(c.bundlers) {
bundler := &Bundler{}
c.bundlers = append(c.bundlers, bundler)
c.currentBundlerIndex = len(c.bundlers)
return bundler
} else {
bundler := c.bundlers[c.currentBundlerIndex]
bundler.Clear()
c.currentBundlerIndex++
return bundler
}
}
func unbundle(bundle Writer, bundler *Bundler) {
wd := W{bundler: bundler}
bundle.CompWrite(wd)
}
func remove(bundle Writer, bundler *Bundler) {
wd := W{bundler: bundler}
bundle.CompWrite(wd)
}
// func CmdSpawn[T Writer](c *CommandQueue, ub T) {
// bundler := c.NextBundler()
// unbundle(ub, bundler)
// // ub.Unbundle(bundler)
// c.commands = append(c.commands, singleCmd{
// Type: CmdTypeSpawn,
// id: c.world.NewId(),
// bundler: bundler,
// })
// }
// func (c *CommandQueue) Spawn(bun Writer) {
// entCmd := c.SpawnEmpty()
// entCmd.Insert(bun)
// }
func (c *CommandQueue) SpawnEmpty() EntityCommand {
bundler := c.NextBundler()
c.commands = append(c.commands, singleCmd{
Type: CmdTypeSpawn,
id: c.world.NewId(),
bundler: bundler,
world: c.world,
})
return EntityCommand{
cmd: &(c.commands[len(c.commands)-1]),
}
}
// // Pushes a command to delete the entity
// func (c *CommandQueue) Delete(id Id) {
// c.commands = append(c.commands, singleCmd{
// Type: CmdTypeDelete,
// id: id,
// })
// }
func (c *CommandQueue) Write(id Id) EntityCommand {
bundler := c.NextBundler()
c.commands = append(c.commands, singleCmd{
Type: CmdTypeWrite,
id: id,
bundler: bundler,
world: c.world,
})
return EntityCommand{
cmd: &(c.commands[len(c.commands)-1]),
}
}
func (c *CommandQueue) Trigger(event Event, ids ...Id) {
// Special Case: no ids provided, so just trigger a single, unrelated
if len(ids) == 0 {
c.commands = append(c.commands, singleCmd{
Type: CmdTypeTrigger,
id: InvalidEntity,
event: event,
world: c.world,
})
return
}
for _, id := range ids {
c.commands = append(c.commands, singleCmd{
Type: CmdTypeTrigger,
id: id,
event: event,
world: c.world,
})
}
}
// Adds a prewrite function to be executed before every write or spawn command is executed
// Useful for ensuring entities are fully formed before pushing them into the ECS
func (c *CommandQueue) SetPrewrite(lambda func(EntityCommand)) {
c.preWrite = lambda
}
// // Adds a predelite function to be executed before every delete command is executed
// // Useful for ensuring any external datastructures get cleaned up when an entity is deleted
// func (c *CommandQueue) SetPredelete(lambda func(Id)) {
// c.preDelete = lambda
// }
func (c *CommandQueue) Execute() {
// Perform all commands
// Note: We must check length every time in case calling one command adds more commands
for i := 0; i < len(c.commands); i++ {
c.commands[i].apply(c.world)
}
// Cleanup Queue
c.commands = c.commands[:0]
c.currentBundlerIndex = 0
}
// TODO: Maybe?
// func (c *CommandQueue) ExecutePostWrite(postWrite func (ecs.Id)) {
// // Perform all commands
// for i := range c.commands {
// c.commands[i].apply(c.world)
// }
// for i := range c.commands {
// if c.commands[i].id == InvalidEntity {
// continue
// }
// postWrite(c.commands[i].id)
// }
// // Cleanup Queue
// c.commands = c.commands[:0]
// c.currentBundlerIndex = 0
// }