/
animation.go
104 lines (84 loc) · 2.29 KB
/
animation.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
package engi
import (
"log"
"github.com/Lealen/engi/ecs"
)
type AnimationAction struct {
Name string
Frames []int
}
// Component that controls animation in rendering entities
type AnimationComponent struct {
index int // What frame in the is being used
Rate float32 // How often frames should increment, in seconds.
change float32 // The time since the last incrementation
Drawables []Drawable // Renderables
Animations map[string][]int // All possible animations
CurrentAnimation []int // The current animation
}
func NewAnimationComponent(drawables []Drawable, rate float32) *AnimationComponent {
return &AnimationComponent{
Animations: make(map[string][]int),
Drawables: drawables,
Rate: rate,
}
}
func (ac *AnimationComponent) SelectAnimationByName(name string) {
ac.CurrentAnimation = ac.Animations[name]
}
func (ac *AnimationComponent) SelectAnimationByAction(action *AnimationAction) {
ac.CurrentAnimation = ac.Animations[action.Name]
}
func (ac *AnimationComponent) AddAnimationAction(action *AnimationAction) {
ac.Animations[action.Name] = action.Frames
}
func (ac *AnimationComponent) AddAnimationActions(actions []*AnimationAction) {
for _, action := range actions {
ac.Animations[action.Name] = action.Frames
}
}
func (ac *AnimationComponent) Cell() Drawable {
idx := ac.CurrentAnimation[ac.index]
return ac.Drawables[idx]
}
func (ac *AnimationComponent) NextFrame() {
if len(ac.CurrentAnimation) == 0 {
log.Println("No data for this animation")
return
}
ac.index += 1
if ac.index >= len(ac.CurrentAnimation) {
ac.index = 0
}
ac.change = 0
}
func (*AnimationComponent) Type() string {
return "AnimationComponent"
}
type AnimationSystem struct {
*ecs.System
}
func (a *AnimationSystem) New(*ecs.World) {
a.System = ecs.NewSystem()
}
func (AnimationSystem) Type() string {
return "AnimationSystem"
}
func (a *AnimationSystem) Update(e *ecs.Entity, dt float32) {
var (
ac *AnimationComponent
r *RenderComponent
ok bool
)
if ac, ok = e.ComponentFast(ac).(*AnimationComponent); !ok {
return
}
if r, ok = e.ComponentFast(r).(*RenderComponent); !ok {
return
}
ac.change += dt
if ac.change >= ac.Rate {
ac.NextFrame()
r.SetDrawable(ac.Cell())
}
}