-
Notifications
You must be signed in to change notification settings - Fork 1
/
world.go
150 lines (126 loc) · 2.81 KB
/
world.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
package ecs
type World struct {
systems []System
entities []Entity
state []State
currId int
}
func (w *World) GetNextId() int {
id := w.currId
w.currId++
return id
}
func (w *World) AddSystem(system System) {
system.Setup(w)
w.systems = append(w.systems, system)
}
func (w *World) AddSystems(systems ...System) {
w.systems = append(w.systems, systems...)
}
func (w *World) RemoveSystem(system System) {
idx_to_delete := -1
for i, s := range w.systems {
if s.Id() == system.Id() {
idx_to_delete = i
break
}
}
if idx_to_delete >= 0 {
w.systems = append(w.systems[:idx_to_delete], w.systems[idx_to_delete+1:]...)
}
}
func (w *World) Start() {
for _, system := range w.systems {
system.Setup(w)
}
}
func (w *World) Update() {
for _, system := range w.systems {
system.Update(w)
}
}
func (w *World) AddEntity(name string, components ...Component) {
w.entities = append(w.entities, NewEntity(w.currId, name, components...))
w.currId++
}
func (w *World) AddState(state State) {
w.state = append(w.state, state)
}
func (w *World) QueryState(query Query) ([]State, bool) {
var states []State
numTypes := query.numTypes
if numTypes == 0 {
return nil, false
}
count := 0
for _, s := range w.state {
state, found := query.MatchState(*w, s)
if found {
states = append(states, state)
count++
if numTypes == count {
break
}
}
}
if len(states) == 0 {
return nil, false
}
return states, true
}
func (w *World) QueryWithEntity(query Query) ([]Entity, [][]Component, bool) {
var entities []Entity
var allComponents [][]Component
numTypes := query.numTypes
if numTypes == 0 {
return nil, nil, false
}
for _, e := range w.entities {
components, found := query.Match(&e)
if found {
entities = append(entities, e)
allComponents = append(allComponents, components)
}
}
if len(entities) == 0 {
return nil, nil, false
}
return entities, allComponents, true
}
func (w *World) Query(query Query) ([][]Component, bool) {
var allComponents [][]Component
numTypes := query.numTypes
if numTypes == 0 {
return nil, false
}
for _, e := range w.entities {
components, found := query.Match(&e)
if found {
allComponents = append(allComponents, components)
}
}
if len(allComponents) == 0 {
return nil, false
}
return allComponents, true
}
func (w *World) QueryWithEntityMut(query Query) ([]*Entity, [][]ComponentRef, bool) {
var entities []*Entity
var allComponents [][]ComponentRef
numTypes := query.numTypes
if numTypes == 0 {
return nil, nil, false
}
for i := 0; i < len(w.entities); i++ {
e := &w.entities[i]
components, found := query.MatchMut(e)
if found {
entities = append(entities, e)
allComponents = append(allComponents, components)
}
}
if len(entities) == 0 {
return nil, nil, false
}
return entities, allComponents, true
}