-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelement.go
74 lines (62 loc) · 1.29 KB
/
element.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
package main
import (
"fmt"
"reflect"
"github.com/veandco/go-sdl2/sdl"
)
type Element struct {
rotation float64
active bool
tag string
origin Rect
frame Rect
collisions []CollisionArea
components []Component
}
func (el *Element) draw(renderer *sdl.Renderer) error {
for _, comp := range el.components {
err := comp.onDraw(renderer)
if err != nil {
return err
}
}
return nil
}
func (el *Element) update() error {
for _, comp := range el.components {
err := comp.onUpdate()
if err != nil {
return err
}
}
return nil
}
func (el *Element) collide(other *Element) error {
for _, comp := range el.components {
err := comp.onCollide(other)
if err != nil {
return err
}
}
return nil
}
func (el *Element) addComponent(new Component) {
for _, existing := range el.components {
if reflect.TypeOf(new) == reflect.TypeOf(existing) {
panic(fmt.Sprintf(
"attempt to add new component with existing type %v",
reflect.TypeOf(new)))
}
}
el.components = append(el.components, new)
}
func (el *Element) getComponent(withType Component) Component {
for _, comp := range el.components {
if reflect.TypeOf(comp) == reflect.TypeOf(withType) {
return comp
}
}
panic(fmt.Sprintf(
"no component with type %v",
reflect.TypeOf(withType)))
}