From c4924fbd70a5eaa0961a91513d4caafddd598fcb Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 6 Apr 2017 00:58:48 +0100 Subject: [PATCH] Added Animation pause, resume and paused methods. Also remove animation, and includeDelay option to getFirstTick. --- v3/src/animation/AnimationManager.js | 30 +++++++++ v3/src/animation/frame/Animation.js | 11 +++- v3/src/checksum.js | 2 +- v3/src/components/Animation.js | 92 ++++++++++++++++++++++++---- 4 files changed, 121 insertions(+), 14 deletions(-) diff --git a/v3/src/animation/AnimationManager.js b/v3/src/animation/AnimationManager.js index 3326cb286..8a0965181 100644 --- a/v3/src/animation/AnimationManager.js +++ b/v3/src/animation/AnimationManager.js @@ -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) { diff --git a/v3/src/animation/frame/Animation.js b/v3/src/animation/frame/Animation.js index 9a450ee07..ea2d2e23d 100644 --- a/v3/src/animation/frame/Animation.js +++ b/v3/src/animation/frame/Animation.js @@ -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) diff --git a/v3/src/checksum.js b/v3/src/checksum.js index 7c0866b07..f6dc01aaf 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '15430db0-1a19-11e7-90e9-c9fa246fdb12' +build: '04051ec0-1a5b-11e7-b4b4-b34a3c03518b' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/components/Animation.js b/v3/src/components/Animation.js index ee4b72338..780b2a66b 100644 --- a/v3/src/components/Animation.js +++ b/v3/src/components/Animation.js @@ -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