forked from unitoftime/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
61 lines (51 loc) · 1.41 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
package ecs
// Represents a list of commands that need to be executed on the world
type Command struct {
world *World
list map[Id]*writeCmd // TODO - Note to self: if you ever add deletion inside of commands, then the packing commands into a map based on entity Id assumption wont hold, because you'll need some amount of specific ordering
}
// Create a new command to be executed
func NewCommand(world *World) *Command {
return &Command{
world: world,
list: make(map[Id]*writeCmd),
}
}
// Execute the command
func (c *Command) Execute() {
// TODO - Batch similar commands, if you ever switch to something more complex than just writing
// Execute all the commands
for i := range c.list {
c.list[i].execute(c.world)
}
// Clearing Optimization: https://go.dev/doc/go1.11#performance-compiler
for k := range c.list {
delete(c.list, k)
}
}
// TODO - maybe rename as just Write?
// Adds a write command
func WriteCmd[A any](c *Command, id Id, comp A) {
cmd, ok := c.list[id]
if !ok {
cmd = newWriteCmd(id)
c.list[id] = cmd
}
cmd.comps = append(cmd.comps, C(comp))
}
// type cmd interface {
// execute(*World)
// }
type writeCmd struct {
id Id
comps []Component
}
func newWriteCmd(id Id) *writeCmd {
return &writeCmd{
id: id,
comps: make([]Component, 0, 2), // TODO - guaranteed to at least have 1, but a bit arbitrary
}
}
func (c *writeCmd) execute(world *World) {
world.Write(c.id, c.comps...)
}