-
Notifications
You must be signed in to change notification settings - Fork 5
/
object.go
229 lines (197 loc) · 4.64 KB
/
object.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
package object
import (
"reflect"
"github.com/johanhenriksson/goworld/core/input"
"github.com/johanhenriksson/goworld/core/input/keys"
"github.com/johanhenriksson/goworld/core/input/mouse"
"github.com/johanhenriksson/goworld/core/transform"
)
type Object interface {
Component
input.Handler
// Children returns a slice containing the objects children.
Children() []Component
attach(...Component)
detach(Component)
}
// objectType caches a reference to Object's reflect.Type
var objectType = reflect.TypeOf((*Object)(nil)).Elem()
type object struct {
component
transform transform.T
children []Component
}
func emptyObject(name string) object {
return object{
component: emptyComponent(name),
transform: transform.Identity(),
}
}
// Empty creates a new, empty object.
func Empty(name string) Object {
obj := emptyObject(name)
return &obj
}
func New[K Object](name string, obj K) K {
t := reflect.TypeOf(obj).Elem()
v := reflect.ValueOf(obj).Elem()
// find & initialize base object
baseIdx := -1
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.Anonymous {
// only anonymous fields are considered
continue
}
if !field.IsExported() {
// only exported fields can be base fields
continue
}
value := v.Field(i)
if field.Type == objectType {
// the object directly extends the base object
// if its nil, create a new empty object base
if value.IsZero() {
base := Empty(name)
value.Set(reflect.ValueOf(base))
}
} else if _, isObject := value.Interface().(Object); isObject {
// this object extends some other non-base object
} else {
// its not an object, move on
continue
}
// if we already found a base field, the user has embedded multiple objects
if baseIdx >= 0 {
panic("struct embeds multiple Object types")
}
baseIdx = i
}
if baseIdx < 0 {
panic("struct does not embed an Object")
}
// add Component fields as children
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if i == baseIdx {
continue
}
if !field.IsExported() {
continue
}
// all uninitialized fields are ignored since they cant contain valid component references
value := v.Field(i)
if value.IsZero() {
continue
}
// the field contains a reference to an instantiated component
// if its an orphan, add it to the object's children
if child, ok := value.Interface().(Component); ok {
if child.Parent() == nil {
Attach(obj, child)
}
}
}
return obj
}
func (g *object) Transform() transform.T {
return g.transform
}
func (o *object) setParent(parent Object) {
// check for cycles
ancestor := parent
for ancestor != nil {
if ancestor.ID() == o.ID() {
panic("cyclical object hierarchies are not allowed")
}
ancestor = ancestor.Parent()
}
o.component.setParent(parent)
if parent != nil {
o.transform.SetParent(parent.Transform())
} else {
o.transform.SetParent(nil)
}
}
func (g *object) Update(scene Component, dt float32) {
for _, child := range g.children {
if child.Enabled() {
child.Update(scene, dt)
}
}
}
func (g *object) Children() []Component {
return g.children
}
func (g *object) attach(children ...Component) {
for _, child := range children {
g.attachIfNotChild(child)
}
}
func (g *object) attachIfNotChild(child Component) {
for _, existing := range g.children {
if existing.ID() == child.ID() {
return
}
}
g.children = append(g.children, child)
}
func (g *object) detach(child Component) {
for i, existing := range g.children {
if existing.ID() == child.ID() {
g.children = append(g.children[:i], g.children[i+1:]...)
return
}
}
}
func (g *object) setActive(active bool) bool {
wasActive := g.component.setActive(active)
if active {
for _, child := range g.children {
activate(child)
}
} else {
for _, child := range g.children {
deactivate(child)
}
}
return wasActive
}
func (g *object) KeyEvent(e keys.Event) {
for _, child := range g.children {
if !child.Enabled() {
continue
}
if handler, ok := child.(input.KeyHandler); ok {
handler.KeyEvent(e)
if e.Handled() {
return
}
}
}
}
func (g *object) MouseEvent(e mouse.Event) {
for _, child := range g.children {
if !child.Enabled() {
continue
}
if handler, ok := child.(input.MouseHandler); ok {
handler.MouseEvent(e)
if e.Handled() {
return
}
}
}
}
func (o *object) Destroy() {
// iterate over a copy of the child slice, since it will be mutated
// when the child detaches itself during destruction
children := make([]Component, len(o.Children()))
copy(children, o.Children()[:])
for _, child := range o.Children() {
child.Destroy()
}
if o.parent != nil {
o.parent.detach(o)
}
}