/
gameObject.go
70 lines (60 loc) · 1.66 KB
/
gameObject.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
package sh2d
import (
"fmt"
"reflect"
"github.com/hajimehoshi/ebiten"
"github.com/lethal-bacon0/sh2d/geometry2d"
)
//GameObject is a generic type all elements in the game should inherit
type GameObject struct {
Position geometry2d.Vector2D
Rotation float64
Active bool
Components []Component
// Width, Height float64
}
//AddComponent adds a new component to the game object
func (g *GameObject) AddComponent(newComponent Component) {
for _, existing := range g.Components {
if reflect.TypeOf(newComponent) == reflect.TypeOf(existing) {
panic(fmt.Sprintf("Duplicate component of type %v", reflect.TypeOf(newComponent)))
}
}
g.Components = append(g.Components, newComponent)
}
//GetComponent gets a component of the speciied type
func (g *GameObject) GetComponent(withType Component) Component {
for _, component := range g.Components {
if reflect.TypeOf(withType) == reflect.TypeOf(component) {
return component
}
}
panic(fmt.Sprintf("Component of type %v was not found", reflect.TypeOf(withType)))
}
//Update updates all conmponents
func (g *GameObject) Update(delta int64) error {
for _, component := range g.Components {
err := component.Update(delta)
if err != nil {
return err
}
}
return nil
}
//Draw draws all components to screen
func (g *GameObject) Draw(screen *ebiten.Image) error {
for _, component := range g.Components {
err := component.Draw(screen)
if err != nil {
return err
}
}
return nil
}
// //GetCenter gets the center vector of the current game object
// func (g *GameObject) GetCenter() Vector2D {
// return Vector2D{
// X: g.Position.X - (g.Width / 2),
// Y: g.Position.Y - (g.Height / 2),
// }
// }