-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentity.go
More file actions
62 lines (54 loc) · 1.6 KB
/
entity.go
File metadata and controls
62 lines (54 loc) · 1.6 KB
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
package engine
import (
"fmt"
"reflect"
)
// Entity is used to group components with an unique identifier in order to form a virtual/game object
type Entity struct {
*unit
id uint
components []ComponentRoutine
}
// ID returns unique entity identifier
func (e *Entity) ID() uint {
return e.id
}
// ID returns unique entity identifier
func (e *Entity) Components() []ComponentRoutine {
return e.components
}
// AddComponent adds a new component to an entity
func (e *Entity) AddComponent(component ComponentRoutine) {
if play {
component.initUnit(e.engine)
component.initComponent(e)
e.components = append(e.components, component)
for _, system := range systems {
for _, componentType := range system.ComponentTypes() {
if fmt.Sprintf("%T", component) == fmt.Sprintf("%T", componentType) {
system.AddComponent(component)
}
}
}
component.SetActive(true)
}
}
// The below method is more tedious and resource intensive but generic way to get components of any type that implements ComponentRoutine
// Component returns a ComponentRoutine based on the given Component type
// func (e *Entity) Component(lookup interface{}) ComponentRoutine {
// for _, component := range e.components {
// if reflect.TypeOf(component) == reflect.TypeOf(&Transform{}) {
// if fmt.Sprintf("%T", component) == fmt.Sprintf("%T", lookup) {
// return component
// }
// }
// return nil
// }
func (e *Entity) Component(lookup interface{}) ComponentRoutine {
for _, component := range e.components {
if reflect.TypeOf(component) == reflect.TypeOf(lookup) {
return component
}
}
return nil
}