-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.go
101 lines (88 loc) · 2.55 KB
/
info.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
package aseprite
// AnimationInfo stores essential information used
// when using the update game loop.
type AnimationInfo struct {
CurrentAnimation *Animation
PlaySpeed float32
Playing bool
pingedOnce bool
animationFinished bool
PrevFrame int
PrevCurrentFrame int // what?
CurrentFrame int
frameCounter float32
}
func setupAnimationInfo() AnimationInfo {
return AnimationInfo{
PlaySpeed: 1.0,
}
}
// IsPlaying returns a boolean if the provided animation is currently being
// used for the Game Loop.
func (i *AnimationInfo) IsPlaying(animation string) bool {
return i.CurrentAnimation != nil && i.CurrentAnimation.Name == animation
}
// AnimationFinished returns a boolean if the currently used animation being
// used for the Game Loop is finished playing.
func (i *AnimationInfo) AnimationFinished() bool {
return i.animationFinished
}
func (i *AnimationInfo) playAnimation(anim *Animation) {
i.CurrentAnimation = anim
i.CurrentFrame = i.CurrentAnimation.From
i.animationFinished = false // default value
i.pingedOnce = false // default value
// If the animation is reverse then start the animation from the end.
if i.CurrentAnimation.Direction == PlayReverse {
i.CurrentFrame = i.CurrentAnimation.To
}
}
func (i *AnimationInfo) advanceFrame() {
// Reset the frame counter.
i.frameCounter = 0
// Increment or decrement the current frame.
switch i.CurrentAnimation.Direction {
case PlayReverse:
i.CurrentFrame--
case PlayPingPong:
if i.pingedOnce {
i.CurrentFrame--
break
}
fallthrough
case PlayForward:
fallthrough
default:
i.CurrentFrame++
}
}
func (i *AnimationInfo) updateAnimationValues() {
switch i.CurrentAnimation.Direction {
case PlayForward:
if i.CurrentFrame > i.CurrentAnimation.To {
i.CurrentFrame = i.CurrentAnimation.From
i.animationFinished = true //? return
}
case PlayReverse:
if i.CurrentFrame < i.CurrentAnimation.From {
i.CurrentFrame = i.CurrentAnimation.To
i.animationFinished = true //? return
}
case PlayPingPong:
if i.CurrentFrame > i.CurrentAnimation.To {
i.pingedOnce = !i.pingedOnce
i.CurrentFrame = i.CurrentAnimation.To - 1
if i.CurrentFrame < i.CurrentAnimation.From {
i.CurrentFrame = i.CurrentAnimation.From
i.animationFinished = true //? return
}
} else if i.CurrentFrame < i.CurrentAnimation.From {
i.pingedOnce = !i.pingedOnce
i.CurrentFrame = i.CurrentAnimation.From + 1
i.animationFinished = true //? return
if i.CurrentFrame > i.CurrentAnimation.To {
i.CurrentFrame = i.CurrentAnimation.To
}
}
}
}