/
entity.go
90 lines (78 loc) · 1.92 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
package main
import (
"fmt"
"log"
"reflect"
rl "github.com/gen2brain/raylib-go/raylib"
)
type entity struct {
// position should be center of object.
position rl.Vector2
rotation float32
// Scale default should be 1.0f
scale float32
active bool
components []component
}
type component interface {
update() error
draw() error
}
func (e *entity) addComponent(new component) error {
for _, existing := range e.components {
if reflect.TypeOf(new) == reflect.TypeOf(existing) {
return fmt.Errorf("already have component of type: %v", reflect.TypeOf(new))
}
}
e.components = append(e.components, new)
return nil
}
// entity.getComponent(componentName{}).(*componentName)
func (e *entity) getComponent(c component) component {
for _, component := range e.components {
if reflect.TypeOf(component) == reflect.TypeOf(c) {
return component
}
}
return nil
}
// Itterate though attached componets and call thier draw function.
func (e *entity) draw() error {
for _, component := range e.components {
err := component.draw()
if err != nil {
log.Fatalf("failed to draw: %s \n", err)
}
}
return nil
}
// Itterate though attached componets and call thier update function.
func (e *entity) update() error {
for _, component := range e.components {
err := component.update()
if err != nil {
log.Fatalf("failed to update entity: %s \n", err)
}
}
return nil
}
func (e *entity) checkCollision() error {
if e.getComponent(&collisonCircle{}) == nil {
return nil
}
cc := e.getComponent(&collisonCircle{}).(*collisonCircle)
for _, otherEntity := range entities {
if e == otherEntity {
return nil
}
if otherEntity.getComponent(&collisonCircle{}) == nil {
return nil
}
occ := otherEntity.getComponent(&collisonCircle{}).(*collisonCircle)
if rl.CheckCollisionCircles(cc.position, cc.radius, occ.position, occ.radius) {
e.active = false
occ.parent.active = false
}
}
return nil
}