forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.js
More file actions
427 lines (328 loc) · 9.99 KB
/
Copy pathAnimation.js
File metadata and controls
427 lines (328 loc) · 9.99 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
var Class = require('../../utils/Class');
// Game Object Animation Controller
// Phaser.GameObjects.Components.Animation
var Animation = new Class({
initialize:
function Animation (parent)
{
// Sprite / Game Object
this.parent = parent;
this.animationManager = parent.scene.sys.anims;
this.animationManager.once('remove', this.remove, this);
this.isPlaying = false;
// Reference to the Phaser.Animation object
this.currentAnim = null;
// Reference to the Phaser.AnimationFrame object
this.currentFrame = null;
// Animation specific values
// -------------------------
// Scale the time (make it go faster / slower)
// Factor that's used to scale time where 1 = normal speed (the default), 0.5 = half speed, 2 = double speed, etc.
this._timeScale = 1;
// The frame rate of playback in frames per second (default 24 if duration is null)
this.frameRate = 0;
// How long the animation should play for. If frameRate is set it overrides this value
// otherwise frameRate is derived from duration
this.duration = 0;
// ms per frame (without including frame specific modifiers)
this.msPerFrame = 0;
// Skip frames if the time lags, or always advanced anyway?
this.skipMissedFrames = true;
// Delay before starting playback (in seconds)
this._delay = 0;
// Number of times to repeat the animation (-1 for infinity)
this._repeat = 0;
// Delay before the repeat starts (in seconds)
this._repeatDelay = 0;
// Should the animation yoyo? (reverse back down to the start) before repeating?
this._yoyo = false;
// Playhead values
// ---------------
// Move the playhead forward (true) or in reverse (false)
this.forward = true;
this.accumulator = 0;
this.nextTick = 0;
this.repeatCounter = 0;
this.pendingRepeat = false;
this._paused = false;
this._wasPlaying = false;
this._callbackArgs = [ parent, null ];
this._updateParams = [];
},
// Gets or sets the amount of time in seconds between repeats.
// For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
// then wait for 1 second before it repeats, then play again, then wait 1 second again
// before doing its final repeat.
delay: function (value)
{
if (value === undefined)
{
return this._delay;
}
else
{
this._delay = value;
return this;
}
},
delayedPlay: function (delay, key, startFrame)
{
this.play(key, true, startFrame);
this.nextTick += (delay * 1000);
return this;
},
getCurrentKey: function ()
{
if (this.currentAnim)
{
return this.currentAnim.key;
}
},
load: function (key, startFrame)
{
if (startFrame === undefined) { startFrame = 0; }
if (this.isPlaying)
{
this.stop();
}
// Load the new animation in
this.animationManager.load(this, key, startFrame);
return this;
},
pause: function (atFrame)
{
if (!this._paused)
{
this._paused = true;
this._wasPlaying = this.isPlaying;
this.isPlaying = false;
}
if (atFrame !== undefined)
{
this.updateFrame(atFrame);
}
return this;
},
paused: function (value)
{
if (value !== undefined)
{
// Setter
if (value)
{
return this.pause();
}
else
{
return this.resume();
}
}
else
{
return this._paused;
}
},
play: function (key, ignoreIfPlaying, startFrame)
{
if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; }
if (startFrame === undefined) { startFrame = 0; }
if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key)
{
return this;
}
this.load(key, startFrame);
var anim = this.currentAnim;
// Should give us 9,007,199,254,740,991 safe repeats
this.repeatCounter = (this._repeat === -1) ? Number.MAX_SAFE_INTEGER : this._repeat;
anim.getFirstTick(this);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
if (anim.showOnStart)
{
this.parent.visible = true;
}
if (anim.onStart)
{
anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams));
}
return this;
},
// Value between 0 and 1. How far this animation is through, ignoring repeats and yoyos.
// If the animation has a non-zero repeat defined, progress and totalProgress will be different
// because progress doesn't include any repeats or repeatDelays whereas totalProgress does.
progress: function (value)
{
if (value === undefined)
{
var p = this.currentFrame.progress;
if (!this.forward)
{
p = 1 - p;
}
return p;
}
else
{
// TODO: Set progress
return this;
}
},
remove: function (event)
{
if (event === undefined) { event = this.currentAnim; }
if (this.isPlaying && event.key === this.currentAnim.key)
{
this.stop();
var sprite = this.parent;
var frame = this.currentAnim.frames[0];
this.currentFrame = frame;
sprite.texture = frame.frame.texture;
sprite.frame = frame.frame;
}
},
// Gets or sets the number of times that the animation should repeat
// after its first iteration. For example, if repeat is 1, the animation will
// play a total of twice (the initial play plus 1 repeat).
// To repeat indefinitely, use -1. repeat should always be an integer.
repeat: function (value)
{
if (value === undefined)
{
return this._repeat;
}
else
{
this._repeat = value;
this.repeatCounter = 0;
return this;
}
},
// Gets or sets the amount of time in seconds between repeats.
// For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
// then wait for 1 second before it repeats, then play again, then wait 1 second again
// before doing its final repeat.
repeatDelay: function (value)
{
if (value === undefined)
{
return this._repeatDelay;
}
else
{
this._repeatDelay = value;
return this;
}
},
restart: function (includeDelay)
{
if (includeDelay === undefined) { includeDelay = false; }
this.currentAnim.getFirstTick(this, includeDelay);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
this._paused = false;
// Set frame
this.updateFrame(this.currentAnim.frames[0]);
return this;
},
resume: function (fromFrame)
{
if (this._paused)
{
this._paused = false;
this.isPlaying = this._wasPlaying;
}
if (fromFrame !== undefined)
{
this.updateFrame(fromFrame);
}
return this;
},
stop: function (dispatchCallbacks)
{
if (dispatchCallbacks === undefined) { dispatchCallbacks = false; }
this.isPlaying = false;
var anim = this.currentAnim;
if (dispatchCallbacks && anim.onComplete)
{
anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams));
}
return this;
},
timeScale: function (value)
{
if (value === undefined)
{
return this._timeScale;
}
else
{
this._timeScale = value;
return this;
}
},
totalFrames: function ()
{
return this.currentAnim.frames.length;
},
// Value between 0 and 1. How far this animation is through, including things like delays
// repeats, custom frame durations, etc. If the animation is set to repeat -1 it can never
// have a duration, therefore this will return -1.
totalProgres: function ()
{
// TODO
},
update: function (timestamp, delta)
{
if (!this.isPlaying || this.currentAnim.paused)
{
return;
}
this.accumulator += delta * this._timeScale;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
},
updateFrame: function (animationFrame)
{
var sprite = this.parent;
this.currentFrame = animationFrame;
sprite.texture = animationFrame.frame.texture;
sprite.frame = animationFrame.frame;
if (this.isPlaying)
{
if (animationFrame.setAlpha)
{
sprite.alpha = animationFrame.alpha;
}
var anim = this.currentAnim;
if (anim.onUpdate)
{
anim.onUpdate.apply(anim.callbackScope, this._updateParams);
}
if (animationFrame.onUpdate)
{
animationFrame.onUpdate(sprite, animationFrame);
}
}
},
yoyo: function (value)
{
if (value === undefined)
{
return this._yoyo;
}
else
{
this._yoyo = value;
return this;
}
},
destroy: function ()
{
// TODO
}
});
module.exports = Animation;