forked from quasilyte/ge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.go
142 lines (115 loc) · 2.81 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ge
import (
"math"
"github.com/quasilyte/ge/gesignal"
)
// TODO: change animation speed to FPS instead of seconds per frame.
type AnimationMode int
const (
AnimationForward AnimationMode = iota
AnimationBackward
)
type Animation struct {
sprite *Sprite
frameWidth float64
numFrames int
offsetY float64
animationSpan float64
deltaPerFrame float64
repeated bool
Mode AnimationMode
frame int
frameTicker float64
EventFrameChanged gesignal.Event[int]
}
func NewRepeatedAnimation(s *Sprite, numFrames int) *Animation {
a := NewAnimation(s, numFrames)
a.repeated = true
return a
}
func NewAnimation(s *Sprite, numFrames int) *Animation {
a := &Animation{}
a.SetSprite(s, numFrames)
a.SetSecondsPerFrame(0.05)
return a
}
func (a *Animation) SetSprite(s *Sprite, numFrames int) {
a.sprite = s
if numFrames < 0 {
numFrames = int(s.ImageWidth() / s.FrameWidth)
}
a.frameWidth = s.FrameWidth
a.numFrames = numFrames
a.SetSecondsPerFrame(a.deltaPerFrame)
}
func (a *Animation) SetOffsetY(offset float64) {
a.offsetY = offset
}
func (a *Animation) SetAnimationSpan(value float64) {
a.animationSpan = value
a.deltaPerFrame = value / float64(a.numFrames)
}
func (a *Animation) SetSecondsPerFrame(seconds float64) {
a.animationSpan = seconds * float64(a.numFrames)
a.deltaPerFrame = seconds
}
func (a *Animation) Sprite() *Sprite {
return a.sprite
}
func (a *Animation) IsDisposed() bool {
return a.sprite.IsDisposed()
}
func (a *Animation) Rewind() {
a.frameTicker = 0
a.frame = 0
if a.Mode == AnimationForward {
a.sprite.FrameOffset.X = 0
} else {
a.sprite.FrameOffset.X = a.frameWidth * float64(a.numFrames-1)
}
}
func (a *Animation) RewindTo(value float64) {
a.frameTicker = 0
a.frame = -1
a.Tick(value)
}
func (a *Animation) Tick(delta float64) bool {
if !a.repeated {
if a.frameTicker >= a.animationSpan {
return true
}
}
// TODO: remove this Y axis overwrite?
a.sprite.FrameOffset.Y = a.offsetY
finished := false
a.frameTicker += delta
var frame int
if a.frameTicker >= a.animationSpan {
finished = true
if a.repeated {
rem := math.Mod(a.frameTicker, a.animationSpan)
a.frameTicker = rem
frame = int(a.frameTicker / a.deltaPerFrame)
} else {
a.frameTicker = a.animationSpan
frame = a.numFrames - 1
}
} else {
frame = int(a.frameTicker / a.deltaPerFrame)
}
if a.Mode == AnimationBackward {
frame = (a.numFrames - 1) - frame
}
framesDelta := frame - a.frame
a.frame = frame
if framesDelta != 0 {
// A small optimization: don't call Emit if there are no listeners.
// This is more useful for repeated animations as they're less likely to have
// any frame event listeners.
if !a.EventFrameChanged.IsEmpty() {
a.EventFrameChanged.Emit(framesDelta)
}
a.sprite.FrameOffset.X = a.frameWidth * float64(frame)
}
return finished
}