2017-07-04 00:59:31 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-05-02 15:49:48 +00:00
|
|
|
// Game Object Animation Controller
|
|
|
|
|
2017-10-13 10:56:39 +00:00
|
|
|
// Phaser.GameObjects.Components.Animation
|
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
var Animation = new Class({
|
2017-04-05 23:58:48 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
initialize:
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
function Animation (parent)
|
2017-04-05 01:06:28 +00:00
|
|
|
{
|
2017-04-06 23:07:20 +00:00
|
|
|
// Sprite / Game Object
|
|
|
|
this.parent = parent;
|
2017-04-05 01:06:28 +00:00
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
this.animationManager = parent.scene.sys.anims;
|
2017-04-06 02:45:45 +00:00
|
|
|
|
2018-01-12 17:09:09 +00:00
|
|
|
this.animationManager.once('remove', this.remove, this);
|
2017-05-02 15:02:49 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
this.isPlaying = false;
|
2017-04-06 02:45:45 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Reference to the Phaser.Animation object
|
|
|
|
this.currentAnim = null;
|
2017-04-06 02:45:45 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Reference to the Phaser.AnimationFrame object
|
|
|
|
this.currentFrame = null;
|
2017-04-06 02:45:45 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Animation specific values
|
|
|
|
// -------------------------
|
2017-04-05 01:06:28 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Scale the time (make it go faster / slower)
|
2017-04-06 23:34:06 +00:00
|
|
|
// Factor that's used to scale time where 1 = normal speed (the default), 0.5 = half speed, 2 = double speed, etc.
|
|
|
|
this._timeScale = 1;
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// The frame rate of playback in frames per second (default 24 if duration is null)
|
|
|
|
this.frameRate = 0;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
2017-07-27 07:22:52 +00:00
|
|
|
// How long the animation should play for. If frameRate is set it overrides this value
|
|
|
|
// otherwise frameRate is derived from duration
|
2017-04-06 23:07:20 +00:00
|
|
|
this.duration = 0;
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// ms per frame (without including frame specific modifiers)
|
|
|
|
this.msPerFrame = 0;
|
2017-04-05 03:18:08 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Skip frames if the time lags, or always advanced anyway?
|
|
|
|
this.skipMissedFrames = true;
|
2017-04-05 03:18:08 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Delay before starting playback (in seconds)
|
2017-04-06 23:29:20 +00:00
|
|
|
this._delay = 0;
|
2017-04-05 03:18:08 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Number of times to repeat the animation (-1 for infinity)
|
2017-04-06 23:29:20 +00:00
|
|
|
this._repeat = 0;
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Delay before the repeat starts (in seconds)
|
2017-04-06 23:29:20 +00:00
|
|
|
this._repeatDelay = 0;
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Should the animation yoyo? (reverse back down to the start) before repeating?
|
2017-04-06 23:29:20 +00:00
|
|
|
this._yoyo = false;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Playhead values
|
|
|
|
// ---------------
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// Move the playhead forward (true) or in reverse (false)
|
2017-04-05 01:06:28 +00:00
|
|
|
this.forward = true;
|
2017-04-05 23:58:48 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
this.accumulator = 0;
|
|
|
|
this.nextTick = 0;
|
2017-04-05 23:58:48 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
this.repeatCounter = 0;
|
2017-04-05 23:58:48 +00:00
|
|
|
|
|
|
|
this.pendingRepeat = false;
|
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
this._paused = false;
|
|
|
|
this._wasPlaying = false;
|
2017-04-10 15:27:32 +00:00
|
|
|
|
|
|
|
this._callbackArgs = [ parent, null ];
|
|
|
|
this._updateParams = [];
|
2017-04-05 23:58:48 +00:00
|
|
|
},
|
|
|
|
|
2018-01-16 15:39:18 +00:00
|
|
|
// 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.
|
2017-05-02 15:02:49 +00:00
|
|
|
|
2018-01-16 15:39:18 +00:00
|
|
|
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
|
2018-01-18 14:59:32 +00:00
|
|
|
this.repeatCounter = (this._repeat === -1) ? Number.MAX_VALUE : this._repeat;
|
2018-01-16 15:39:18 +00:00
|
|
|
|
|
|
|
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)
|
2017-05-02 15:02:49 +00:00
|
|
|
{
|
2018-01-16 15:39:18 +00:00
|
|
|
if (this._paused)
|
|
|
|
{
|
|
|
|
this._paused = false;
|
|
|
|
this.isPlaying = this._wasPlaying;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromFrame !== undefined)
|
|
|
|
{
|
|
|
|
this.updateFrame(fromFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
2017-05-02 15:02:49 +00:00
|
|
|
|
2018-01-16 15:39:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-05-02 15:02:49 +00:00
|
|
|
},
|
|
|
|
|
2018-01-16 15:39:18 +00:00
|
|
|
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
|
|
|
|
}
|
2017-04-05 14:27:26 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-04-04 22:59:16 +00:00
|
|
|
module.exports = Animation;
|