-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentity.go
237 lines (199 loc) · 5.52 KB
/
entity.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
package ecs
// An Entity is essentially a map of components that is held external to a world. Useful for pulling full entities in and out of the world.
// Deprecated: This type and its corresponding methods are tentative and might be replaced by something else.
type Entity struct {
// comp map[componentId]Component
comp []Component
}
// Creates a new entity with the specified components
func NewEntity(components ...Component) *Entity {
return &Entity{
comp: components,
}
// c := make(map[componentId]Component)
// for i := range components {
// c[components[i].id()] = components[i]
// }
// return &Entity{
// comp: c,
// }
}
// Returns the index that contains the same componentId or returns -1
func (e *Entity) findIndex(compId CompId) int {
for i := range e.comp {
if compId == e.comp[i].CompId() {
return i
}
}
return -1
}
// Adds a component to an entity
func (e *Entity) Add(components ...Component) {
for i := range components {
idx := e.findIndex(components[i].CompId())
if idx < 0 {
e.comp = append(e.comp, components[i])
} else {
e.comp[idx] = components[i]
}
}
// for i := range components {
// e.comp[components[i].id()] = components[i]
// }
}
// Merges e2 on top of e (meaning that we will overwrite e with values from e2)
func (e *Entity) Merge(e2 *Entity) {
e.Add(e2.comp...)
// for k, v := range e2.comp {
// e.comp[k] = v
// }
}
// Returns a list of the components held by the entity
func (e *Entity) Comps() []Component {
return e.comp
// ret := make([]Component, 0, len(e.comp))
// for _, v := range e.comp {
// ret = append(ret, v)
// }
// return ret
}
// Reads a specific component from the entity, returns false if the component doesn't exist
func ReadFromEntity[T any](ent *Entity) (T, bool) {
var t T
n := name(t)
idx := ent.findIndex(n)
if idx < 0 {
return t, false
}
icomp := ent.comp[idx]
return icomp.(box[T]).val, true
// var t T
// n := name(t)
// icomp, ok := ent.comp[n]
// if !ok {
// return t, false
// }
// return icomp.(Box[T]).Comp, true
}
// Writes the entire entity to the world
func (ent *Entity) Write(world *World, id Id) {
world.Write(id, ent.comp...)
// comps := ent.Comps()
// world.Write(id, comps...)
}
// Reads the entire entity out of the world and into an *Entity object. Returns nil if the entity doesn't exist
func ReadEntity(world *World, id Id) *Entity {
archId, ok := world.arch.Get(id)
if !ok {
return nil
}
return world.engine.ReadEntity(archId, id)
}
// Deletes a component on this entity
func (e *Entity) Delete(c Component) {
compId := c.CompId()
idx := e.findIndex(compId)
if idx < 0 {
return
}
// If index does exist, then cut it out
e.comp[idx] = e.comp[len(e.comp)-1]
e.comp = e.comp[:len(e.comp)-1]
// delete(e.comp, c.id())
}
// Clears the map, but retains the space
func (e *Entity) Clear() {
e.comp = e.comp[:0]
// // Clearing Optimization: https://go.dev/doc/go1.11#performance-compiler
// for k := range e.comp {
// delete(e.comp, k)
// }
}
// TODO revisit this abstraction
// type Copier interface {
// Copy() interface{}
// }
// func (e Entity) Copy() Entity {
// copy := BlankEntity()
// for k,v := range e {
// c, ok := v.(Copier)
// if ok {
// // fmt.Println("Copying:", k)
// // If the component has a custom copy interface, then copy it
// copy[k] = c.Copy()
// } else {
// // Else just copy the top level struct
// copy[k] = v
// }
// }
// return copy
// }
// A RawEntity is like an Entity, but every component is actually a pointer to the underlying component. I mostly use this to build inspector UIs that can directly modify an entity
// Deprecated: This type and its corresponding methods are tentative and might be replaced by something else.
type RawEntity struct {
comp map[CompId]any
}
// Creates a new entity with the specified components
func NewRawEntity(components ...any) *RawEntity {
c := make(map[CompId]any)
for i := range components {
c[name(components[i])] = components[i]
}
return &RawEntity{
comp: c,
}
}
// Adds a component to an entity
func (e *RawEntity) Add(components ...any) {
for i := range components {
e.comp[name(components[i])] = components[i]
}
}
// Merges e2 on top of e (meaning that we will overwrite e with values from e2)
func (e *RawEntity) Merge(e2 *RawEntity) {
for k, v := range e2.comp {
e.comp[k] = v
}
}
// Returns a list of the components held by the entity
func (e *RawEntity) Comps() []any {
ret := make([]any, 0, len(e.comp))
for _, v := range e.comp {
ret = append(ret, v)
}
return ret
}
// // Reads a specific component from the entity, returns false if the component doesn't exist
// func ReadFromRawEntity[T any](ent *RawEntity) (T, bool) {
// var t T
// n := name(t)
// icomp, ok := ent.comp[n]
// if !ok {
// return t, false
// }
// return icomp.(Box[T]).Comp, true
// }
// Writes the entire entity to the world
// func (ent *RawEntity) Write(world *World, id Id) {
// comps := ent.Comps()
// world.Write(id, comps...)
// }
// Reads the entire entity out of the world and into an *RawEntity object. Returns nil if the entity doesn't exist. RawEntity is lik
func ReadRawEntity(world *World, id Id) *RawEntity {
archId, ok := world.arch.Get(id)
if !ok {
return nil
}
return world.engine.ReadRawEntity(archId, id)
}
// Deletes a component on this entity
func (e *RawEntity) Delete(c Component) {
delete(e.comp, name(c))
}
// Clears the map, but retains the space
func (e *RawEntity) Clear() {
// Clearing Optimization: https://go.dev/doc/go1.11#performance-compiler
for k := range e.comp {
delete(e.comp, k)
}
}