-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathanimation.go
327 lines (285 loc) · 8.37 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package ganim8
import (
"fmt"
"image"
"log"
"time"
"github.com/hajimehoshi/ebiten/v2"
)
type imageCache map[*ebiten.Image]map[*image.Rectangle]*ebiten.Image
var _imageCache imageCache
var DefaultDelta = time.Millisecond * 16
func init() {
_imageCache = make(map[*ebiten.Image]map[*image.Rectangle]*ebiten.Image)
}
func parseDurations(durations interface{}, frameCount int) []time.Duration {
result := make([]time.Duration, frameCount)
switch val := durations.(type) {
case time.Duration:
for i := 0; i < frameCount; i++ {
result[i] = val
}
case []time.Duration:
for i := range val {
result[i] = val[i]
}
case []interface{}:
for i := range val {
result[i] = parseDurationValue(val[i])
}
case map[string]time.Duration:
for key, duration := range val {
min, max, step := parseInterval(key)
for i := min; i <= max; i += step {
result[i-1] = duration
}
}
case map[string]interface{}:
for key, duration := range val {
min, max, step := parseInterval(key)
for i := min; i <= max; i += step {
result[i-1] = parseDurationValue(duration)
}
}
case interface{}:
for i := 0; i < frameCount; i++ {
result[i] = parseDurationValue(val)
}
default:
log.Fatal(fmt.Sprintf("failed to parse durations: type=%T val=%+v", durations, durations))
}
return result
}
func parseDurationValue(value interface{}) time.Duration {
switch val := value.(type) {
case time.Duration:
return val
case int:
return time.Millisecond * time.Duration(val)
case float64:
return time.Millisecond * time.Duration(val)
default:
log.Fatal(fmt.Sprintf("failed to parse duration value: %+v", value))
}
return 0
}
func parseIntervals(durations []time.Duration) ([]time.Duration, time.Duration) {
result := []time.Duration{0}
var time time.Duration = 0
for _, v := range durations {
time += v
result = append(result, time)
}
return result, time
}
// Status represents the animation status.
type Status int
const (
Playing = iota
Paused
)
// Animation represents an animation created from specified frames
// and an *ebiten.Image
type Animation struct {
sprite *Sprite
position int
timer time.Duration
durations []time.Duration
intervals []time.Duration
totalDuration time.Duration
onLoop OnLoop
status Status
}
// OnLoop is callback function which representing
// one of the animation methods.
// it will be called every time an animation "loops".
//
// It will have two parameters: the animation instance,
// and how many loops have been elapsed.
//
// The value would be Nop (No operation) if there's nothing
// to do except for looping the animation.
//
// The most usual value (apart from none) is the string 'pauseAtEnd'.
// It will make the animation loop once and then pause
// and stop on the last frame.
type OnLoop func(anim *Animation, loops int)
// Nop does nothing.
func Nop(anim *Animation, loops int) {}
// Pause pauses the animation on loop finished.
func Pause(anim *Animation, loops int) {
anim.Pause()
}
// PauseAtEnd pauses the animation and set the position to
// the last frame.
func PauseAtEnd(anim *Animation, loops int) {
anim.PauseAtEnd()
}
// PauseAtStart pauses the animation and set the position to
// the first frame.
func PauseAtStart(anim *Animation, loops int) {
anim.PauseAtStart()
}
// NewAnimation returns a new animation object
//
// durations is a time.Duration or a []time.Duration or
// a map[string]time.Duration.
// When it's a time.Duration, it represents the duration of
// all frames in the animation.
// When it's a []time.Duration, it can represent different
// durations for different frames.
// You can specify durations for all frames individually,
// like this: []time.Duration { 100 * time.Millisecond,
// 100 * time.Millisecond } or you can specify durations for
// ranges of frames: map[string]time.Duration { "1-2":
// 100 * time.Millisecond, "3-5": 200 * time.Millisecond }.
func NewAnimation(sprite *Sprite, durations interface{}, onLoop ...OnLoop) *Animation {
_durations := parseDurations(durations, sprite.length)
intervals, totalDuration := parseIntervals(_durations)
ol := Nop
if len(onLoop) > 0 {
ol = onLoop[0]
}
anim := &Animation{
sprite: sprite,
position: 0,
timer: 0,
durations: _durations,
intervals: intervals,
totalDuration: totalDuration,
onLoop: ol,
status: Playing,
}
return anim
}
// New creates a new animation from the specified image
func New(img *ebiten.Image, frames []*image.Rectangle, durations interface{}, onLoop ...OnLoop) *Animation {
spr := NewSprite(img, frames)
return NewAnimation(spr, durations, onLoop...)
}
// Clone return a copied animation object.
func (anim *Animation) Clone() *Animation {
new := *anim
return &new
}
// SetOnLoop sets the callback function which representing
func (anim *Animation) SetOnLoop(onLoop OnLoop) {
anim.onLoop = onLoop
}
func (anim *Animation) IsEnd() bool {
if anim.status == Paused && anim.position == anim.sprite.length-1 {
return true
}
return false
}
func seekFrameIndex(intervals []time.Duration, timer time.Duration) int {
high, low, i := len(intervals)-2, 0, 0
for low <= high {
i = (low + high) / 2
if timer >= intervals[i+1] {
low = i + 1
} else if timer < intervals[i] {
high = i - 1
} else {
return i
}
}
return i
}
// Update updates the animation.
func (anim *Animation) Update() {
anim.UpdateWithDelta(DefaultDelta)
}
// UpdateWithDelta updates the animation with the specified delta.
func (anim *Animation) UpdateWithDelta(elapsedTime time.Duration) {
if anim.status != Playing || anim.sprite.length <= 1 {
return
}
anim.timer += elapsedTime
loops := anim.timer / anim.totalDuration
if loops != 0 {
anim.timer = anim.timer - anim.totalDuration*loops
(anim.onLoop)(anim, int(loops))
}
anim.position = seekFrameIndex(anim.intervals, anim.timer)
}
// SetDurations sets the durations of the animation.
func (anim *Animation) SetDurations(durations interface{}) {
_durations := parseDurations(durations, anim.sprite.length)
anim.durations = _durations
anim.intervals, anim.totalDuration = parseIntervals(_durations)
anim.timer = 0
}
// Status returns the status of the animation.
func (anim *Animation) Status() Status {
return anim.status
}
// Pause pauses the animation.
func (anim *Animation) Pause() {
anim.status = Paused
}
// Position returns the current position of the frame.
// The position counts from 1 (not 0).
func (anim *Animation) Position() int {
return anim.position + 1
}
// Duration returns the current durations of each frames.
func (anim *Animation) Durations() []time.Duration {
return anim.durations
}
// TotalDuration returns the total duration of the animation.
func (anim *Animation) TotalDuration() time.Duration {
return anim.totalDuration
}
// Size returns the size of the current frame.
func (anim *Animation) Size() (int, int) {
return anim.sprite.Size()
}
// W is a shortcut for Size().X.
func (anim *Animation) W() int {
return anim.sprite.W()
}
// H is a shortcut for Size().Y.
func (anim *Animation) H() int {
return anim.sprite.H()
}
// Timer returns the current accumulated times of current frame.
func (anim *Animation) Timer() time.Duration {
return anim.timer
}
// Sprite returns the sprite of the animation.
func (anim *Animation) Sprite() *Sprite {
return anim.sprite
}
// GoToFrame sets the position of the animation and
// sets the timer at the start of the frame.
func (anim *Animation) GoToFrame(position int) {
anim.position = position - 1
anim.timer = anim.intervals[anim.position]
}
// PauseAtEnd pauses the animation and set the position
// to the last frame.
func (anim *Animation) PauseAtEnd() {
anim.position = anim.sprite.length - 1
anim.timer = anim.totalDuration
anim.Pause()
}
// PauseAtStart pauses the animation and set the position
// to the first frame.
func (anim *Animation) PauseAtStart() {
anim.position = 0
anim.timer = 0
anim.status = Paused
}
// Resume resumes the animation
func (anim *Animation) Resume() {
anim.status = Playing
}
// Draw draws the animation with the specified option parameters.
func (anim *Animation) Draw(screen *ebiten.Image, opts *DrawOptions) {
anim.sprite.Draw(screen, anim.position, opts)
}
// DrawWithShader draws the animation with the specified option parameters.
func (anim *Animation) DrawWithShader(screen *ebiten.Image, opts *DrawOptions, shaderOpts *ShaderOptions) {
anim.sprite.DrawWithShader(screen, anim.position, opts, shaderOpts)
}