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;
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
this.currentFrame = animationFrame;
|
|
|
|
|
|
|
|
this.parent.texture = animationFrame.frame.texture;
|
|
|
|
this.parent.frame = animationFrame.frame;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
|
|
return this.parent;
|
2017-04-04 22:59:16 +00:00
|
|
|
},
|
|
|
|
|
2017-04-05 01:06:28 +00:00
|
|
|
// Example data:
|
|
|
|
// timestamp = 2356.534000020474
|
2017-04-05 03:18:08 +00:00
|
|
|
// frameDelta = 17.632333353807383 (diff since last timestamp?)
|
|
|
|
update: function (timestamp)
|
2017-04-04 22:59:16 +00:00
|
|
|
{
|
2017-04-05 00:15:53 +00:00
|
|
|
if (this.isPlaying)
|
2017-04-04 22:59:16 +00:00
|
|
|
{
|
2017-04-05 03:18:08 +00:00
|
|
|
this.accumulator += (timestamp - this.prevTick) * this.timescale;
|
|
|
|
|
|
|
|
this.prevTick = timestamp;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
// 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 ()
|
|
|
|
{
|
|
|
|
var p = this.currentFrame.index / this.currentAnim.frames.length;
|
|
|
|
|
|
|
|
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;
|