-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.go
72 lines (59 loc) · 1.23 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
package suslik
import (
"time"
)
type Animation struct {
TotalFrames int
Current int
Delay time.Duration
Playing bool
Timer Timer
}
func NewAnimation(frames int, delay time.Duration) *Animation {
return &Animation{
TotalFrames: frames,
Current: 0,
Delay: delay,
Playing: false,
Timer: MakeTimer(),
}
}
func (anim *Animation) Play(num int) {
anim.Timer.Start()
anim.Current = num
anim.Playing = true
}
func (anim *Animation) Stop() {
anim.Timer.Stop()
anim.Playing = false
}
func (anim *Animation) NextFrame() int {
if anim.Playing && anim.Timer.Elapsed() > anim.Delay {
anim.Timer.Reset()
anim.Current = (anim.Current + 1) % anim.TotalFrames
}
return anim.Current
}
func (anim *Animation) GetFrame() int {
return anim.Current
}
func (anim *Animation) SetFrame(n int) {
if n > 0 && n < anim.TotalFrames {
anim.Current = n
}
}
func (anim *Animation) ResetTime() {
anim.Timer.Reset()
}
func (anim *Animation) GetDelay() time.Duration {
return anim.Delay
}
func (anim *Animation) SetDelay(d time.Duration) {
anim.Delay = d
}
func (anim *Animation) GetTotalFrames() int {
return anim.TotalFrames
}
func (anim *Animation) SetTotalFrames(total int) {
anim.TotalFrames = total
}