-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
162 lines (140 loc) · 4.07 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
package pearl
// The struct that all entities are
type Entity struct {
// The name / ID of the entity
ID string
// Whether or not the Entity is destroyed
Destroyed bool
components []IComponent
tags []string
parent *Entity
children []*Entity
}
// Flags entity as destroyed and will destroy it in the next update loop
func (e *Entity) Destroy() {
e.Destroyed = true
}
// Adds component specified to entity's component list
func (e *Entity) AddComponent(component IComponent) {
e.components = append(e.components, component)
}
// Adds each of the components specified to entity's component list
func (e *Entity) AddComponents(components []IComponent) {
for _, component := range components {
e.AddComponent(component)
}
}
// Returns the component with the id specified, but returns nil if entity doesn't a component with that id
func (e *Entity) GetComponent(id string) IComponent {
if !e.HasComponent(id) { return nil }
for _, component := range e.components {
if component.ID() == id {
return component
}
}
return nil
}
// Returns a slice of components found on entity, returns an empty slice if none found
func (e *Entity) GetComponents(id string) []IComponent {
found := []IComponent {}
for _, component := range e.components {
if component.ID() == id {
found = append(found, component)
}
}
return found
}
// Returns the component in the parent with id specified. Returns nil if parent doesn't have component
func (e *Entity) GetComponentInParent(id string) IComponent {
return e.parent.GetComponent(id)
}
// Returns a slice of components with id specified found in the parent. Returns empty list if none found
func (e *Entity) GetComponentsInParent(id string) []IComponent {
return e.parent.GetComponents(id)
}
// Checks each child for component with id and returns the first one found. Returns nil if not found
func (e *Entity) GetComponentInChildren(id string) IComponent {
for _, child := range e.children {
if child.HasComponent(id) {
return child.GetComponent(id)
}
}
return nil
}
// Returns slice of all components with id found in each children, returns empty slice if none found
func (e *Entity) GetComponentsInChildren(id string) []IComponent {
found := []IComponent {}
for _, child := range e.children {
for _, component := range child.components {
if component.ID() == id {
found = append(found, component)
}
}
}
return found
}
// Returns bool on wether or not the entity has the component
func (e *Entity) HasComponent(id string) bool {
for _, component := range e.components {
if component.ID() == id {
return true
}
}
return false
}
// Adds tag to the entity's tag list
func (e *Entity) AddTag(tag string) {
e.tags = append(e.tags, tag)
}
// Adds each of the tags specified to entity's tag list
func (e *Entity) AddTags(tags []string) {
for _, tag := range tags {
e.AddTag(tag)
}
}
// Returns whether or not the Entity has the tag specified
func (e *Entity) HasTag(tag string) bool {
for _, t := range e.tags {
if t == tag {
return true
}
}
return false
}
// Sets Entity's parent variable
func (e *Entity) SetParent(entity *Entity) {
e.parent = entity
entity.AddChild(e)
}
// Returns Entity's parent
func (e *Entity) GetParent() *Entity {
return e.parent
}
// Adds Entity to child list
func (e *Entity) AddChild(entity *Entity) {
e.children = append(e.children, entity)
}
// Adds Entities to child list
func (e *Entity) AddChildren(entities []*Entity) {
for _, entity := range entities {
e.AddChild(entity)
}
}
// Removes Entity at index from child list, but does nothing if entity specified isn't a child of entity
func (e *Entity) RemoveChildAt(index int) {
e.children[index].parent = nil
e.children = append(e.children[:index], e.children[index + 1:]...)
}
// Removes Entity from child list, but does nothing if entity specified isn't a child of entity
func (e *Entity) RemoveChild(entity *Entity) {
for i := 0; i < len(e.children); i++ {
if e.children[i] == entity {
e.RemoveChildAt(i)
break
}
}
}
// Returns Entity's children
func (e *Entity) GetChildren() []*Entity {
return e.children
}