-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.go
182 lines (154 loc) · 3.39 KB
/
command.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
package ecs
import "fmt"
// 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
CmdTypeCustom
)
type singleCmd struct {
Type CmdType
id Id
bundler *Bundler
}
func (c singleCmd) apply(world *World) {
switch c.Type {
case CmdTypeNone:
// Do nothing, Command was probably cancelled
case CmdTypeSpawn:
c.bundler.Write(world, c.id)
case CmdTypeWrite:
c.bundler.Write(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) Cancel() {
e.cmd.Type = CmdTypeNone
}
func (e EntityCommand) Empty() bool {
return (e == EntityCommand{})
}
func (e EntityCommand) Insert(bun Writer) EntityCommand {
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
}
return t, false
}
type CommandQueue struct {
world *World
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 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,
})
return EntityCommand{
cmd: &(c.commands[len(c.commands)-1]),
}
}
func (c *CommandQueue) Write(id Id) EntityCommand {
bundler := c.NextBundler()
c.commands = append(c.commands, singleCmd{
Type: CmdTypeWrite,
id: id,
bundler: bundler,
})
return EntityCommand{
cmd: &(c.commands[len(c.commands)-1]),
}
}
func (c *CommandQueue) Execute() {
// Perform all commands
for i := range c.commands {
c.commands[i].apply(c.world)
}
// Cleanup Queue
c.commands = c.commands[:0]
c.currentBundlerIndex = 0
}