-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.go
62 lines (53 loc) · 1.5 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
package main
// Animation declares a way of how to manipulate the internal animation frames
type Animation interface {
NextFrame() string
CurrentFrame() string
MoveFrameIndex()
framesAmount() int
hasNextFrame() bool
}
type animation struct {
frames []string
currentFrameIndex int
frameDuration int
currentFrameTime int
}
// NewAnimation creates new animation object with specified frames array
func NewAnimation(frames []string, duration int) Animation {
return &animation{
frames: frames,
currentFrameIndex: 0,
frameDuration: duration,
currentFrameTime: 0}
}
func (a *animation) framesAmount() int {
return len(a.frames)
}
func (a *animation) hasNextFrame() bool {
return a.currentFrameIndex < a.framesAmount()-1
}
// MoveFrameIndex moves the current frame caret to the next frame.
// If there are no frames left in the sequence - caret will be reset and point to the 0 frame
func (a *animation) MoveFrameIndex() {
if a.currentFrameTime < a.frameDuration-1 {
a.currentFrameTime++
} else {
a.currentFrameTime = 0
if a.hasNextFrame() {
a.currentFrameIndex++
} else {
a.currentFrameIndex = 0
}
}
}
// CurrentFrame obtains current frame from the animation sequence
func (a *animation) CurrentFrame() string {
return a.frames[a.currentFrameIndex]
}
// NextFrame get the next animation frame.
// If there are no frames in the animation sequence - sequence will reset from 0
func (a *animation) NextFrame() string {
a.MoveFrameIndex()
return a.CurrentFrame()
}