forked from EngoEngine/engo
/
entity.go
52 lines (44 loc) · 1.05 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
package engi
import (
"reflect"
)
type Entity struct {
id string
components map[string]Component
requires map[string]bool
Pattern string
}
func NewEntity(requires []string) *Entity {
e := &Entity{
id: generateUUID(),
requires: make(map[string]bool),
components: make(map[string]Component),
}
for _, req := range requires {
e.requires[req] = true
}
return e
}
func (e *Entity) DoesRequire(name string) bool {
return e.requires[name]
}
func (e *Entity) AddComponent(component Component) {
e.components[reflect.TypeOf(component).String()] = component
}
func (e *Entity) RemoveComponent(component Component) {
delete(e.components, reflect.TypeOf(component).String())
}
// GetComponent takes a double pointer to a Component,
// and populates it with the value of the right type.
func (e *Entity) Component(x interface{}) bool {
v := reflect.ValueOf(x).Elem() // *T
c, ok := e.components[v.Type().String()]
if !ok {
return false
}
v.Set(reflect.ValueOf(c))
return true
}
func (e *Entity) ID() string {
return e.id
}