forked from unitoftime/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
185 lines (158 loc) · 4.58 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
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
}
// Creates a new entity with the specified components
func NewEntity(components ...Component) *Entity {
c := make(map[componentId]Component)
for i := range components {
c[components[i].id()] = components[i]
}
return &Entity{
comp: c,
}
}
// Adds a component to an entity
func (e *Entity) Add(components ...Component) {
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) {
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 {
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)
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) {
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[id]
if !ok {
return nil
}
return world.engine.ReadEntity(archId, id)
}
// Deletes a component on this entity
func (e *Entity) Delete(c Component) {
delete(e.comp, c.id())
}
// Clears the map, but retains the space
func (e *Entity) Clear() {
// 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[componentId]any
}
// Creates a new entity with the specified components
func NewRawEntity(components ...any) *RawEntity {
c := make(map[componentId]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[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)
}
}