Added Animation pause, resume and paused methods. Also remove animation, and includeDelay option to getFirstTick.

This commit is contained in:
Richard Davey 2017-04-06 00:58:48 +01:00
parent aa8f0820c0
commit c4924fbd70
4 changed files with 121 additions and 14 deletions

View file

@ -44,6 +44,9 @@ AnimationManager.prototype = {
// { key: textureKey, frame: textureFrame },
// { key: textureKey, frame: textureFrame, duration: float },
// { key: textureKey, frame: textureFrame, onUpdate: function }
// { key: textureKey, frame: textureFrame, flipX: boolean, flipY: boolean }
// { key: textureKey, frame: textureFrame, alpha: float }
// { key: textureKey, frame: textureFrame, visible: boolean }
// ],
// framerate: integer,
// duration: float (seconds, optional, ignored if framerate is set),
@ -64,6 +67,21 @@ AnimationManager.prototype = {
// ]
// }
add: function (key, animation)
{
if (this.anims.has(key))
{
console.error('Animation with key', key, 'already exists');
return;
}
animation.key = key;
this.anims.set(key, animation);
return this;
},
create: function (key, config)
{
if (this.anims.has(key))
@ -84,6 +102,18 @@ AnimationManager.prototype = {
return this.anims.get(key);
},
remove: function (key)
{
var anim = this.get(key);
if (anim)
{
this.anims.delete(key);
}
return anim;
},
// Load an Animation into a Game Objects Animation Component
load: function (child, key, startFrame)
{

View file

@ -82,11 +82,18 @@ Animation.prototype = {
return (index < this.frames.length);
},
getFirstTick: function (component)
getFirstTick: function (component, includeDelay)
{
if (includeDelay === undefined) { includeDelay = true; }
// When is the first update due?
component.accumulator = 0;
component.nextTick = this.msPerFrame + component.currentFrame.duration + (this.delay * 1000);
component.nextTick = this.msPerFrame + component.currentFrame.duration;
if (includeDelay)
{
component.nextTick += (this.delay * 1000);
}
},
getNextTick: function (component)

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '15430db0-1a19-11e7-90e9-c9fa246fdb12'
build: '04051ec0-1a5b-11e7-b4b4-b34a3c03518b'
};
module.exports = CHECKSUM;

View file

@ -11,6 +11,8 @@ var Animation = function (parent)
this.animationManager = parent.state.sys.anims;
this.mainloop = parent.state.game.mainloop;
this.isPlaying = false;
// Reference to the Phaser.Animation object
@ -35,6 +37,9 @@ var Animation = function (parent)
this.repeatCounter = 0;
this.pendingRepeat = false;
this._paused = false;
this._wasPlaying = false;
};
Animation.prototype.constructor = Animation;
@ -91,26 +96,25 @@ Animation.prototype = {
this.isPlaying = true;
this.pendingRepeat = false;
this.prevTick = this.parent.state.game.mainloop.lastFrameTimeMs;
this.prevTick = this.mainloop.lastFrameTimeMs;
return this.parent;
},
// Example data:
// timestamp = 2356.534000020474
// frameDelta = 17.632333353807383 (diff since last timestamp?)
update: function (timestamp)
{
if (this.isPlaying)
if (!this.isPlaying)
{
this.accumulator += (timestamp - this.prevTick) * this.timescale;
return;
}
this.prevTick = timestamp;
this.accumulator += (timestamp - this.prevTick) * this.timescale;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
this.prevTick = timestamp;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
},
@ -121,6 +125,72 @@ Animation.prototype = {
return this.parent;
},
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;
},
// 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