-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
91 lines (78 loc) · 2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"reflect"
"github.com/veandco/go-sdl2/sdl"
)
type vector struct {
x, y float64
}
// Good use of interface here. Anything that uses the component interface
// needs to have at lease the properties in the component interface
type component interface {
onUpdate() error
onDraw(renderer *sdl.Renderer) error
onCollision(other *element) error
}
// takes shared information that every element in game will need
type element struct {
position vector
rotation float64
active bool
tag string
components []component
collisions []circle
}
// draws component inside element.component
func (elem *element) draw(renderer *sdl.Renderer) error {
for _, comp := range elem.components {
err := comp.onDraw(renderer)
if err != nil {
return err
}
}
return nil
}
// updates component inside element.component
func (elem *element) update() error {
for _, comp := range elem.components {
err := comp.onUpdate()
if err != nil {
return err
}
}
return nil
}
func (elem *element) collision(other *element) error {
for _, comp := range elem.components {
err := comp.onCollision(other)
if err != nil {
return err
}
}
return nil
}
// add a new component to element
func (elem *element) addComponent(new component) {
// at compile, loop through every existing component and make sure it's not of the same type as the new component
for _, existing := range elem.components {
if reflect.TypeOf(new) == reflect.TypeOf(existing) {
panic(fmt.Sprintf(
"attempt to add new component with existing type %v",
reflect.TypeOf(new)))
}
}
// add component after check
elem.components = append(elem.components, new)
}
// easy access component type check for element
func (elem *element) getComponent(withType component) component {
typ := reflect.TypeOf(withType)
for _, comp := range elem.components {
if reflect.TypeOf(comp) == typ {
return comp
}
}
panic(fmt.Sprintf("no component with type %v", reflect.TypeOf(withType)))
}
var elements []*element