2017-04-04 22:59:16 +00:00
|
|
|
/**
|
|
|
|
* The Animation Component.
|
|
|
|
* Should be as small as possible, really just playhead info.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
var Animation = function (parent)
|
|
|
|
{
|
|
|
|
// Sprite / Game Object
|
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
this.animationManager = parent.state.sys.anims;
|
|
|
|
|
2017-04-05 23:58:48 +00:00
|
|
|
this.mainloop = parent.state.game.mainloop;
|
|
|
|
|
2017-04-04 22:59:16 +00:00
|
|
|
this.isPlaying = false;
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Reference to the Phaser.Animation object
|
2017-04-04 22:59:16 +00:00
|
|
|
this.currentAnim = null;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
|
|
|
// Reference to the Phaser.AnimationFrame object
|
2017-04-04 22:59:16 +00:00
|
|
|
this.currentFrame = null;
|
|
|
|
|
2017-04-05 01:06:28 +00:00
|
|
|
// Scale the time (make it go faster / slower)
|
|
|
|
this.timescale = 1;
|
|
|
|
|
|
|
|
// Playhead values
|
|
|
|
|
|
|
|
// Move the playhead forward (true) or in reverse (false)
|
|
|
|
this.forward = true;
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
this.accumulator = 0;
|
2017-04-05 01:06:28 +00:00
|
|
|
|
2017-04-05 03:18:08 +00:00
|
|
|
this.prevTick = 0;
|
2017-04-04 22:59:16 +00:00
|
|
|
this.nextTick = 0;
|
2017-04-05 01:06:28 +00:00
|
|
|
|
|
|
|
this.repeatCounter = 0;
|
2017-04-05 02:22:54 +00:00
|
|
|
|
|
|
|
this.pendingRepeat = false;
|
2017-04-05 23:58:48 +00:00
|
|
|
|
|
|
|
this._paused = false;
|
|
|
|
this._wasPlaying = false;
|
2017-04-04 22:59:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Animation.prototype.constructor = Animation;
|
|
|
|
|
|
|
|
Animation.prototype = {
|
|
|
|
|
2017-04-05 01:06:28 +00:00
|
|
|
updateAnimation: function (animation)
|
|
|
|
{
|
|
|
|
this.currentAnim = animation;
|
|
|
|
},
|
|
|
|
|
|
|
|
updateFrame: function (animationFrame)
|
|
|
|
{
|
2017-04-06 02:45:45 +00:00
|
|
|
var sprite = this.parent;
|
|
|
|
|
2017-04-05 01:06:28 +00:00
|
|
|
this.currentFrame = animationFrame;
|
|
|
|
|
2017-04-06 02:45:45 +00:00
|
|
|
sprite.texture = animationFrame.frame.texture;
|
|
|
|
sprite.frame = animationFrame.frame;
|
|
|
|
|
|
|
|
if (animationFrame.setAlpha)
|
|
|
|
{
|
|
|
|
sprite.alpha = animationFrame.alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animationFrame.setVisible)
|
|
|
|
{
|
|
|
|
sprite.visible = animationFrame.visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animationFrame.onUpdate)
|
|
|
|
{
|
|
|
|
animationFrame.onUpdate(sprite, animationFrame);
|
|
|
|
}
|
2017-04-05 01:06:28 +00:00
|
|
|
},
|
|
|
|
|
2017-04-04 22:59:16 +00:00
|
|
|
load: function (key, startFrame)
|
|
|
|
{
|
|
|
|
if (startFrame === undefined) { startFrame = 0; }
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
if (this.isPlaying)
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
|
2017-04-04 22:59:16 +00:00
|
|
|
// Load the new animation in
|
2017-04-05 00:15:53 +00:00
|
|
|
this.animationManager.load(this, key, startFrame);
|
2017-04-04 22:59:16 +00:00
|
|
|
},
|
|
|
|
|
2017-04-05 03:18:08 +00:00
|
|
|
delayedPlay: function (delay, key, startFrame)
|
|
|
|
{
|
|
|
|
this.play(key, startFrame);
|
|
|
|
|
|
|
|
this.nextTick += (delay * 1000);
|
|
|
|
|
|
|
|
return this.parent;
|
|
|
|
},
|
|
|
|
|
2017-04-04 22:59:16 +00:00
|
|
|
play: function (key, startFrame)
|
|
|
|
{
|
|
|
|
if (startFrame === undefined) { startFrame = 0; }
|
|
|
|
|
|
|
|
this.load(key, startFrame);
|
|
|
|
|
2017-04-05 01:06:28 +00:00
|
|
|
// Should give us 9,007,199,254,740,991 safe repeats
|
|
|
|
this.repeatCounter = (this.currentAnim.repeat === -1) ? Number.MAX_SAFE_INTEGER : this.currentAnim.repeat;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
2017-04-05 03:18:08 +00:00
|
|
|
this.currentAnim.getFirstTick(this);
|
2017-04-04 22:59:16 +00:00
|
|
|
|
2017-04-05 01:06:28 +00:00
|
|
|
this.forward = true;
|
2017-04-05 00:15:53 +00:00
|
|
|
this.isPlaying = true;
|
2017-04-05 02:22:54 +00:00
|
|
|
this.pendingRepeat = false;
|
2017-04-05 03:18:08 +00:00
|
|
|
|
2017-04-05 23:58:48 +00:00
|
|
|
this.prevTick = this.mainloop.lastFrameTimeMs;
|
2017-04-05 16:05:51 +00:00
|
|
|
|
2017-04-05 03:18:08 +00:00
|
|
|
return this.parent;
|
2017-04-04 22:59:16 +00:00
|
|
|
},
|
|
|
|
|
2017-04-05 03:18:08 +00:00
|
|
|
update: function (timestamp)
|
2017-04-04 22:59:16 +00:00
|
|
|
{
|
2017-04-05 23:58:48 +00:00
|
|
|
if (!this.isPlaying)
|
2017-04-04 22:59:16 +00:00
|
|
|
{
|
2017-04-05 23:58:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-05 03:18:08 +00:00
|
|
|
|
2017-04-05 23:58:48 +00:00
|
|
|
this.accumulator += (timestamp - this.prevTick) * this.timescale;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
2017-04-05 23:58:48 +00:00
|
|
|
this.prevTick = timestamp;
|
|
|
|
|
|
|
|
if (this.accumulator >= this.nextTick)
|
|
|
|
{
|
|
|
|
this.currentAnim.setFrame(this);
|
2017-04-04 22:59:16 +00:00
|
|
|
}
|
2017-04-05 01:06:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function ()
|
|
|
|
{
|
|
|
|
this.isPlaying = false;
|
2017-04-05 02:22:54 +00:00
|
|
|
|
2017-04-05 03:18:08 +00:00
|
|
|
return this.parent;
|
2017-04-05 14:27:26 +00:00
|
|
|
},
|
|
|
|
|
2017-04-05 23:58:48 +00:00
|
|
|
restart: function (includeDelay)
|
|
|
|
{
|
|
|
|
if (includeDelay === undefined) { includeDelay = false; }
|
|
|
|
|
|
|
|
this.currentAnim.getFirstTick(this, includeDelay);
|
|
|
|
|
|
|
|
this.forward = true;
|
|
|
|
this.isPlaying = true;
|
|
|
|
this.pendingRepeat = false;
|
|
|
|
|
|
|
|
this.prevTick = this.mainloop.lastFrameTimeMs;
|
|
|
|
|
|
|
|
// Set frame
|
|
|
|
this.updateFrame(this.currentAnim.frames[0]);
|
|
|
|
|
|
|
|
return this.parent;
|
|
|
|
},
|
|
|
|
|
|
|
|
paused: function (value)
|
|
|
|
{
|
|
|
|
if (value !== undefined)
|
|
|
|
{
|
|
|
|
// Setter
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
return this.pause();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this.resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this._paused;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
pause: function ()
|
|
|
|
{
|
|
|
|
if (!this._paused)
|
|
|
|
{
|
|
|
|
this._paused = true;
|
|
|
|
this._wasPlaying = this.isPlaying;
|
|
|
|
this.isPlaying = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
resume: function ()
|
|
|
|
{
|
|
|
|
if (this._paused)
|
|
|
|
{
|
|
|
|
this._paused = false;
|
|
|
|
this.isPlaying = this._wasPlaying;
|
|
|
|
|
|
|
|
if (this.isPlaying)
|
|
|
|
{
|
|
|
|
this.prevTick = this.mainloop.lastFrameTimeMs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-04-05 14:27:26 +00:00
|
|
|
// How far through the current animation are we?
|
|
|
|
// Value between 0 and 1
|
|
|
|
// I.e. [a,b,c,d,e,f] if on frame c progress would be 0.5
|
|
|
|
|
|
|
|
// TODO: Add value argument
|
|
|
|
progress: function ()
|
|
|
|
{
|
2017-04-05 16:05:51 +00:00
|
|
|
var p = this.currentFrame.progress;
|
2017-04-05 14:27:26 +00:00
|
|
|
|
|
|
|
if (!this.forward)
|
|
|
|
{
|
|
|
|
p = 1 - p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
2017-04-04 22:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-04-05 14:27:26 +00:00
|
|
|
Object.defineProperties(Animation.prototype, {
|
|
|
|
|
|
|
|
totalFrames: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.currentAnim.frames.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-04-04 22:59:16 +00:00
|
|
|
module.exports = Animation;
|