From ab7e55ef27d8e69ad99aacac41ab7336144ae90d Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 8 Aug 2013 05:43:22 +0100 Subject: [PATCH] Added ability for the TimeManager to monitor the pause duration. This is then applied to tweens and other game services, meaning the tweens now restart properly after a pause resumes. --- Phaser/Game.ts | 62 +++++++++++-------- Phaser/Stage.ts | 4 +- Phaser/time/TimeManager.ts | 37 +++++++++-- Phaser/tweens/Tween.ts | 33 +++++----- Phaser/tweens/TweenManager.ts | 40 ++++++++++++ README.md | 29 ++++----- Tests/Tests.csproj | 4 ++ Tests/phaser.js | 112 +++++++++++++++++++++++----------- Tests/tweens/pause test.js | 14 +++++ Tests/tweens/pause test.ts | 21 +++++++ build/phaser.d.ts | 39 +++++++++--- build/phaser.js | 112 +++++++++++++++++++++++----------- 12 files changed, 358 insertions(+), 149 deletions(-) create mode 100644 Tests/tweens/pause test.js create mode 100644 Tests/tweens/pause test.ts diff --git a/Phaser/Game.ts b/Phaser/Game.ts index aec9d53c6..b62ff8522 100644 --- a/Phaser/Game.ts +++ b/Phaser/Game.ts @@ -88,12 +88,6 @@ module Phaser { */ public _raf: RequestAnimationFrame; - /** - * Milliseconds of time per step of the game loop. - * @type {number} - */ - //private _step: number = 0; - /** * Whether load complete loading or not. * @type {boolean} @@ -183,6 +177,18 @@ module Phaser { */ public onDestroyCallback = null; + /** + * This Signal is dispatched whenever the game pauses. + * @type {Phaser.Signal} + */ + public onPause: Phaser.Signal; + + /** + * This Signal is dispatched whenever the game resumes from a paused state. + * @type {Phaser.Signal} + */ + public onResume: Phaser.Signal; + /** * Reference to the GameObject Factory. * @type {GameObjectFactory} @@ -307,6 +313,9 @@ module Phaser { document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot); window.removeEventListener('load', Phaser.GAMES[this.id].boot); + this.onPause = new Phaser.Signal; + this.onResume = new Phaser.Signal; + this.device = new Device(); this.net = new Net(this); this.math = new GameMath(this); @@ -365,24 +374,6 @@ module Phaser { } - public setRenderer(type: number) { - - switch (type) - { - case Phaser.Types.RENDERER_AUTO_DETECT: - this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this); - break; - - case Phaser.Types.RENDERER_AUTO_DETECT: - case Phaser.Types.RENDERER_CANVAS: - this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this); - break; - - // WebGL coming soon :) - } - - } - /** * Called when the load has finished after preload was run. */ @@ -513,6 +504,24 @@ module Phaser { } + public setRenderer(type: number) { + + switch (type) + { + case Phaser.Types.RENDERER_AUTO_DETECT: + this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this); + break; + + case Phaser.Types.RENDERER_AUTO_DETECT: + case Phaser.Types.RENDERER_CANVAS: + this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this); + break; + + // WebGL coming soon :) + } + + } + /** * Set the most common state callbacks (init, create, update, render). * @param preloadCallback {function} Init callback invoked when init state. @@ -675,13 +684,16 @@ module Phaser { if (value == true && this._paused == false) { this._paused = true; + this.onPause.dispatch(); + // Hook to the above this.sound.pauseAll(); this._raf.callback = this.pausedLoop; } else if (value == false && this._paused == true) { this._paused = false; - //this.time.time = window.performance.now ? (performance.now() + performance.timing.navigationStart) : Date.now(); + this.onResume.dispatch(); + // Hook to the above this.input.reset(); this.sound.resumeAll(); diff --git a/Phaser/Stage.ts b/Phaser/Stage.ts index 5e74610f5..e8550d59f 100644 --- a/Phaser/Stage.ts +++ b/Phaser/Stage.ts @@ -290,6 +290,8 @@ module Phaser { public pauseGame() { + this.game.paused = true; + if (this.disablePauseScreen == false && this.pauseScreen) { this.pauseScreen.onPaused(); @@ -297,8 +299,6 @@ module Phaser { this.saveCanvasValues(); - this.game.paused = true; - } public resumeGame() { diff --git a/Phaser/time/TimeManager.ts b/Phaser/time/TimeManager.ts index 5207917af..a74e741ee 100644 --- a/Phaser/time/TimeManager.ts +++ b/Phaser/time/TimeManager.ts @@ -24,6 +24,9 @@ module Phaser { this._timeLastSecond = this._started; this.time = this._started; + this.game.onPause.add(this.gamePaused, this); + this.game.onResume.add(this.gameResumed, this); + } /** @@ -42,7 +45,7 @@ module Phaser { * Set it to 0.5 for slow motion, to 2.0 makes game twice faster. * @type {number} */ - public timeScale: number = 1.0; + //public timeScale: number = 1.0; /** * Elapsed since last frame. @@ -57,6 +60,13 @@ module Phaser { */ public time: number = 0; + /** + * How long the game has been paused for. Gets reset each time the game pauses. + * @property pausedTime + * @type {number} + */ + public pausedTime: number = 0; + /** * Time of current frame. * @property now @@ -101,13 +111,13 @@ module Phaser { public fpsMax: number = 0; /** - * Mininal duration between 2 frames. + * Minimum duration between 2 frames. * @type {number} */ public msMin: number = 1000; /** - * Maximal duration between 2 frames. + * Maximum duration between 2 frames. * @type {number} */ public msMax: number = 0; @@ -133,7 +143,6 @@ module Phaser { public update(raf: number) { this.now = raf; // mark - //this.now = Date.now(); // mark this.delta = this.now - this.time; // elapsedMS this.msMin = Math.min(this.msMin, this.delta); @@ -153,8 +162,26 @@ module Phaser { this.time = this.now; // _total + // Paused? + if (this.game.paused) + { + this.pausedTime = this.now - this._pauseStarted; + } + } + private gamePaused() { + this._pauseStarted = this.now; + } + + private gameResumed() { + // Level out the delta timer to avoid spikes + this.pauseDuration = this.pausedTime; + } + + public pauseDuration: number = 0; + private _pauseStarted: number = 0; + /** * How long has passed since given time. * @method elapsedSince @@ -168,7 +195,7 @@ module Phaser { } /** - * How long has passed since give time (in seconds). + * How long has passed since the given time (in seconds). * @method elapsedSecondsSince * @param {number} since The time you want to measure (in seconds). * @return {number} Duration between given time and now (in seconds). diff --git a/Phaser/tweens/Tween.ts b/Phaser/tweens/Tween.ts index 7a911a72f..516264042 100644 --- a/Phaser/tweens/Tween.ts +++ b/Phaser/tweens/Tween.ts @@ -60,6 +60,7 @@ module Phaser { * @type {object} */ private _object = null; + private _pausedTime: number = 0; /** @@ -344,6 +345,19 @@ module Phaser { } + public pause() { + this._paused = true; + } + + public resume() { + + this._paused = false; + this._startTime += this.game.time.pauseDuration; + + } + + private _paused: bool; + /** * Update tweening. * @param time {number} Current time from game clock. @@ -351,24 +365,7 @@ module Phaser { */ public update(time) { - if (this.game.paused == true) - { - if (this._pausedTime == 0) - { - this._pausedTime = time; - } - } - else - { - // Ok we aren't paused, but was there some time gained? - if (this._pausedTime > 0) - { - this._startTime += (time - this._pausedTime); - this._pausedTime = 0; - } - } - - if (time < this._startTime) + if (this._paused || time < this._startTime) { return true; } diff --git a/Phaser/tweens/TweenManager.ts b/Phaser/tweens/TweenManager.ts index 43013435a..a727baf7f 100644 --- a/Phaser/tweens/TweenManager.ts +++ b/Phaser/tweens/TweenManager.ts @@ -22,8 +22,12 @@ module Phaser { constructor(game: Phaser.Game) { this.game = game; + this._tweens = []; + this.game.onPause.add(this.pauseAll, this); + this.game.onResume.add(this.resumeAll, this); + } /** @@ -140,5 +144,41 @@ module Phaser { } + public pauseAll() { + + if (this._tweens.length === 0) + { + return false; + } + + var i = 0; + var numTweens = this._tweens.length; + + while (i < numTweens) + { + this._tweens[i].pause(); + i++; + } + + } + + public resumeAll() { + + if (this._tweens.length === 0) + { + return false; + } + + var i = 0; + var numTweens = this._tweens.length; + + while (i < numTweens) + { + this._tweens[i].resume(); + i++; + } + + } + } } diff --git a/README.md b/README.md index 7cab5971b..c7dd13cdc 100644 --- a/README.md +++ b/README.md @@ -22,23 +22,19 @@ Known Issues * Input detection on Sprites/Buttons doesn't work if the CAMERA is rotated or scaled. +Future Plans +------------ -Latest Update -------------- - -TODO: - -* Inject game into a
+* Ability to layer another DOM object and have it controlled by the game somehow. Can then do stacked canvas effects. * Add ability to create extra
s within the game container, layered above/below the canvas -* Rename init to preload and call start automatically -* Allow camera to directly render to the stage rather than its hidden (maybe does this by default? or have under Mobile Optimisations list) +* Basic Window UI component (maybe a propogating Group?) +ToDo before release +------------------- - -* Investigate why tweens don't restart after the game pauses +* Hook sound/input to the pause/resume signals * Fix bug in Tween yoyo + loop combo -* Check that tween pausing works with the new performance.now -* Game.Time should monitor pause duration +* Allow camera to directly render to the stage rather than hidden ctx (maybe does this by default? or have under Mobile Optimisations list) * Investigate bug re: tilemap collision and animation frames * Add clip support + shape options to Texture Component. * Need to be able to set the current tilemap layer, then the getTileXY default layer uses that one if no other given @@ -48,18 +44,16 @@ TODO: * Tilemap.render - move layers length to var * Tilemap.destroy needs doing * Sprite.transform.bottomRight/Left doesn't seem to take origin into account -* Stage.opaqueBackground = 'rgb()' or null, alpha, blendMode, filters, mask, rotation+XYZ,scaleXYZ,visible -* Stage CSS3 Transforms? -* Ability to layer another DOM object and have it controlled by the game somehow. Can then do stacked canvas effects. * Stage lost to mute * Bitmap Font support -* Basic Window component (maybe a propogating Group?) * Put ArcadePhysics back in * Look at the N+ tile support maybe with ArcadePhysics? * Pixel-perfect click check * Check Flash atlas export is supported * DynamicTexture.setPixel needs to be swapped for a proper pixel put, not the filledRect it currently is. +Latest Update +------------- V1.0.0 @@ -165,7 +159,8 @@ V1.0.0 * Added the new PluginManager. Moved all the Camera FX over to plugins. Everything will be a plugin from now on. * Added Sprite.transform.centerOn(x,y) to quickly center a sprite on a coordinate without messing with the sprite origin and regardless of rotation. * Added Input.pollRate - this lets you limit how often Pointer events are handled (0 = every frame, 1 = every other frame, etc) -* Renamed the 'init' function to 'preload' and now call load.start automatically. +* Renamed the 'init' function to 'preload'. It now calls load.start automatically. +* Added V0.9.6 diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 45c025bb8..e83340ee7 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -395,6 +395,10 @@ + + + pause test.ts + tween loop 1.ts diff --git a/Tests/phaser.js b/Tests/phaser.js index 05f74d9ee..e0648f290 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -10693,24 +10693,20 @@ var Phaser; this._chainedTweens.push(tween); return this; }; + Tween.prototype.pause = function () { + this._paused = true; + }; + Tween.prototype.resume = function () { + this._paused = false; + this._startTime += this.game.time.pauseDuration; + }; Tween.prototype.update = /** * Update tweening. * @param time {number} Current time from game clock. * @return {boolean} Return false if this completed and no need to update, otherwise return true. */ function (time) { - if(this.game.paused == true) { - if(this._pausedTime == 0) { - this._pausedTime = time; - } - } else { - // Ok we aren't paused, but was there some time gained? - if(this._pausedTime > 0) { - this._startTime += (time - this._pausedTime); - this._pausedTime = 0; - } - } - if(time < this._startTime) { + if(this._paused || time < this._startTime) { return true; } this._tempElapsed = (time - this._startTime) / this._duration; @@ -14192,11 +14188,11 @@ var Phaser; this.canvas.style['-ms-interpolation-mode'] = 'nearest-neighbor'; }; Stage.prototype.pauseGame = function () { + this.game.paused = true; if(this.disablePauseScreen == false && this.pauseScreen) { this.pauseScreen.onPaused(); } this.saveCanvasValues(); - this.game.paused = true; }; Stage.prototype.resumeGame = function () { if(this.disablePauseScreen == false && this.pauseScreen) { @@ -14336,7 +14332,7 @@ var Phaser; * Set it to 0.5 for slow motion, to 2.0 makes game twice faster. * @type {number} */ - this.timeScale = 1.0; + //public timeScale: number = 1.0; /** * Elapsed since last frame. * @type {number} @@ -14349,6 +14345,12 @@ var Phaser; */ this.time = 0; /** + * How long the game has been paused for. Gets reset each time the game pauses. + * @property pausedTime + * @type {number} + */ + this.pausedTime = 0; + /** * Time of current frame. * @property now * @type {number} @@ -14376,12 +14378,12 @@ var Phaser; */ this.fpsMax = 0; /** - * Mininal duration between 2 frames. + * Minimum duration between 2 frames. * @type {number} */ this.msMin = 1000; /** - * Maximal duration between 2 frames. + * Maximum duration between 2 frames. * @type {number} */ this.msMax = 0; @@ -14395,10 +14397,14 @@ var Phaser; * @type {number} */ this._timeLastSecond = 0; + this.pauseDuration = 0; + this._pauseStarted = 0; this.game = game; this._started = 0; this._timeLastSecond = this._started; this.time = this._started; + this.game.onPause.add(this.gamePaused, this); + this.game.onResume.add(this.gameResumed, this); } Object.defineProperty(TimeManager.prototype, "totalElapsedSeconds", { get: /** @@ -14421,7 +14427,6 @@ var Phaser; function (raf) { this.now = raf// mark ; - //this.now = Date.now(); // mark this.delta = this.now - this.time// elapsedMS ; this.msMin = Math.min(this.msMin, this.delta); @@ -14436,6 +14441,17 @@ var Phaser; } this.time = this.now// _total ; + // Paused? + if(this.game.paused) { + this.pausedTime = this.now - this._pauseStarted; + } + }; + TimeManager.prototype.gamePaused = function () { + this._pauseStarted = this.now; + }; + TimeManager.prototype.gameResumed = function () { + // Level out the delta timer to avoid spikes + this.pauseDuration = this.pausedTime; }; TimeManager.prototype.elapsedSince = /** * How long has passed since given time. @@ -14447,7 +14463,7 @@ var Phaser; return this.now - since; }; TimeManager.prototype.elapsedSecondsSince = /** - * How long has passed since give time (in seconds). + * How long has passed since the given time (in seconds). * @method elapsedSecondsSince * @param {number} since The time you want to measure (in seconds). * @return {number} Duration between given time and now (in seconds). @@ -14487,6 +14503,8 @@ var Phaser; function TweenManager(game) { this.game = game; this._tweens = []; + this.game.onPause.add(this.pauseAll, this); + this.game.onResume.add(this.resumeAll, this); } TweenManager.prototype.getAll = /** * Get all the tween objects in an array. @@ -14560,6 +14578,28 @@ var Phaser; } return true; }; + TweenManager.prototype.pauseAll = function () { + if(this._tweens.length === 0) { + return false; + } + var i = 0; + var numTweens = this._tweens.length; + while(i < numTweens) { + this._tweens[i].pause(); + i++; + } + }; + TweenManager.prototype.resumeAll = function () { + if(this._tweens.length === 0) { + return false; + } + var i = 0; + var numTweens = this._tweens.length; + while(i < numTweens) { + this._tweens[i].resume(); + i++; + } + }; return TweenManager; })(); Phaser.TweenManager = TweenManager; @@ -18419,11 +18459,6 @@ var Phaser; if (typeof destroyCallback === "undefined") { destroyCallback = null; } var _this = this; /** - * Milliseconds of time per step of the game loop. - * @type {number} - */ - //private _step: number = 0; - /** * Whether load complete loading or not. * @type {boolean} */ @@ -18532,6 +18567,8 @@ var Phaser; } else { document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot); window.removeEventListener('load', Phaser.GAMES[this.id].boot); + this.onPause = new Phaser.Signal(); + this.onResume = new Phaser.Signal(); this.device = new Phaser.Device(); this.net = new Phaser.Net(this); this.math = new Phaser.GameMath(this); @@ -18574,18 +18611,6 @@ var Phaser; } } }; - Game.prototype.setRenderer = function (type) { - switch(type) { - case Phaser.Types.RENDERER_AUTO_DETECT: - this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this); - break; - case Phaser.Types.RENDERER_AUTO_DETECT: - case Phaser.Types.RENDERER_CANVAS: - this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this); - break; - // WebGL coming soon :) - } - }; Game.prototype.loadComplete = /** * Called when the load has finished after preload was run. */ @@ -18673,6 +18698,18 @@ var Phaser; this._loadComplete = true; } }; + Game.prototype.setRenderer = function (type) { + switch(type) { + case Phaser.Types.RENDERER_AUTO_DETECT: + this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this); + break; + case Phaser.Types.RENDERER_AUTO_DETECT: + case Phaser.Types.RENDERER_CANVAS: + this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this); + break; + // WebGL coming soon :) + } + }; Game.prototype.setCallbacks = /** * Set the most common state callbacks (init, create, update, render). * @param preloadCallback {function} Init callback invoked when init state. @@ -18796,11 +18833,14 @@ var Phaser; set: function (value) { if(value == true && this._paused == false) { this._paused = true; + this.onPause.dispatch(); + // Hook to the above this.sound.pauseAll(); this._raf.callback = this.pausedLoop; } else if(value == false && this._paused == true) { this._paused = false; - //this.time.time = window.performance.now ? (performance.now() + performance.timing.navigationStart) : Date.now(); + this.onResume.dispatch(); + // Hook to the above this.input.reset(); this.sound.resumeAll(); if(this.isRunning == false) { diff --git a/Tests/tweens/pause test.js b/Tests/tweens/pause test.js new file mode 100644 index 000000000..1178e1b4b --- /dev/null +++ b/Tests/tweens/pause test.js @@ -0,0 +1,14 @@ +/// +(function () { + var game = new Phaser.Game(this, 'game', 800, 600, preload, create); + function preload() { + game.load.image('atari', 'assets/sprites/atari130xe.png'); + } + var atari; + function create() { + atari = game.add.sprite(300, 0, 'atari'); + game.add.tween(atari).to({ + y: 500 + }, 5000, Phaser.Easing.Bounce.Out, true, 5000); + } +})(); diff --git a/Tests/tweens/pause test.ts b/Tests/tweens/pause test.ts new file mode 100644 index 000000000..5cd491aba --- /dev/null +++ b/Tests/tweens/pause test.ts @@ -0,0 +1,21 @@ +/// + +(function () { + + var game = new Phaser.Game(this, 'game', 800, 600, preload, create); + + function preload() { + game.load.image('atari', 'assets/sprites/atari130xe.png'); + } + + var atari: Phaser.Sprite; + + function create() { + + atari = game.add.sprite(300, 0, 'atari'); + + game.add.tween(atari).to({ y: 500 }, 5000, Phaser.Easing.Bounce.Out, true, 5000); + + } + +})(); diff --git a/build/phaser.d.ts b/build/phaser.d.ts index c89980000..4dff60347 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -5439,6 +5439,9 @@ module Phaser { * @return {Phaser.Tween} Itselfe. */ public chain(tween: Tween): Tween; + public pause(): void; + public resume(): void; + private _paused; /** * Update tweening. * @param time {number} Current time from game clock. @@ -7366,12 +7369,6 @@ module Phaser { */ private _started; /** - * Time scale factor. - * Set it to 0.5 for slow motion, to 2.0 makes game twice faster. - * @type {number} - */ - public timeScale: number; - /** * Elapsed since last frame. * @type {number} */ @@ -7383,6 +7380,12 @@ module Phaser { */ public time: number; /** + * How long the game has been paused for. Gets reset each time the game pauses. + * @property pausedTime + * @type {number} + */ + public pausedTime: number; + /** * Time of current frame. * @property now * @type {number} @@ -7416,12 +7419,12 @@ module Phaser { */ public fpsMax: number; /** - * Mininal duration between 2 frames. + * Minimum duration between 2 frames. * @type {number} */ public msMin: number; /** - * Maximal duration between 2 frames. + * Maximum duration between 2 frames. * @type {number} */ public msMax: number; @@ -7442,6 +7445,10 @@ module Phaser { * @param {number} raf The current timestamp, either performance.now or Date.now */ public update(raf: number): void; + private gamePaused(); + private gameResumed(); + public pauseDuration: number; + private _pauseStarted; /** * How long has passed since given time. * @method elapsedSince @@ -7450,7 +7457,7 @@ module Phaser { */ public elapsedSince(since: number): number; /** - * How long has passed since give time (in seconds). + * How long has passed since the given time (in seconds). * @method elapsedSecondsSince * @param {number} since The time you want to measure (in seconds). * @return {number} Duration between given time and now (in seconds). @@ -7523,6 +7530,8 @@ module Phaser { * @return {boolean} Return false if there's no tween to update, otherwise return true. */ public update(): bool; + public pauseAll(): bool; + public resumeAll(): bool; } } /** @@ -9602,6 +9611,16 @@ module Phaser { */ public onDestroyCallback; /** + * This Signal is dispatched whenever the game pauses. + * @type {Phaser.Signal} + */ + public onPause: Signal; + /** + * This Signal is dispatched whenever the game resumes from a paused state. + * @type {Phaser.Signal} + */ + public onResume: Signal; + /** * Reference to the GameObject Factory. * @type {GameObjectFactory} */ @@ -9693,7 +9712,6 @@ module Phaser { * @param height {number} Height of the game screen. */ private boot(parent, width, height); - public setRenderer(type: number): void; /** * Called when the load has finished after preload was run. */ @@ -9715,6 +9733,7 @@ module Phaser { * Start current state. */ private startState(); + public setRenderer(type: number): void; /** * Set the most common state callbacks (init, create, update, render). * @param preloadCallback {function} Init callback invoked when init state. diff --git a/build/phaser.js b/build/phaser.js index 05f74d9ee..e0648f290 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -10693,24 +10693,20 @@ var Phaser; this._chainedTweens.push(tween); return this; }; + Tween.prototype.pause = function () { + this._paused = true; + }; + Tween.prototype.resume = function () { + this._paused = false; + this._startTime += this.game.time.pauseDuration; + }; Tween.prototype.update = /** * Update tweening. * @param time {number} Current time from game clock. * @return {boolean} Return false if this completed and no need to update, otherwise return true. */ function (time) { - if(this.game.paused == true) { - if(this._pausedTime == 0) { - this._pausedTime = time; - } - } else { - // Ok we aren't paused, but was there some time gained? - if(this._pausedTime > 0) { - this._startTime += (time - this._pausedTime); - this._pausedTime = 0; - } - } - if(time < this._startTime) { + if(this._paused || time < this._startTime) { return true; } this._tempElapsed = (time - this._startTime) / this._duration; @@ -14192,11 +14188,11 @@ var Phaser; this.canvas.style['-ms-interpolation-mode'] = 'nearest-neighbor'; }; Stage.prototype.pauseGame = function () { + this.game.paused = true; if(this.disablePauseScreen == false && this.pauseScreen) { this.pauseScreen.onPaused(); } this.saveCanvasValues(); - this.game.paused = true; }; Stage.prototype.resumeGame = function () { if(this.disablePauseScreen == false && this.pauseScreen) { @@ -14336,7 +14332,7 @@ var Phaser; * Set it to 0.5 for slow motion, to 2.0 makes game twice faster. * @type {number} */ - this.timeScale = 1.0; + //public timeScale: number = 1.0; /** * Elapsed since last frame. * @type {number} @@ -14349,6 +14345,12 @@ var Phaser; */ this.time = 0; /** + * How long the game has been paused for. Gets reset each time the game pauses. + * @property pausedTime + * @type {number} + */ + this.pausedTime = 0; + /** * Time of current frame. * @property now * @type {number} @@ -14376,12 +14378,12 @@ var Phaser; */ this.fpsMax = 0; /** - * Mininal duration between 2 frames. + * Minimum duration between 2 frames. * @type {number} */ this.msMin = 1000; /** - * Maximal duration between 2 frames. + * Maximum duration between 2 frames. * @type {number} */ this.msMax = 0; @@ -14395,10 +14397,14 @@ var Phaser; * @type {number} */ this._timeLastSecond = 0; + this.pauseDuration = 0; + this._pauseStarted = 0; this.game = game; this._started = 0; this._timeLastSecond = this._started; this.time = this._started; + this.game.onPause.add(this.gamePaused, this); + this.game.onResume.add(this.gameResumed, this); } Object.defineProperty(TimeManager.prototype, "totalElapsedSeconds", { get: /** @@ -14421,7 +14427,6 @@ var Phaser; function (raf) { this.now = raf// mark ; - //this.now = Date.now(); // mark this.delta = this.now - this.time// elapsedMS ; this.msMin = Math.min(this.msMin, this.delta); @@ -14436,6 +14441,17 @@ var Phaser; } this.time = this.now// _total ; + // Paused? + if(this.game.paused) { + this.pausedTime = this.now - this._pauseStarted; + } + }; + TimeManager.prototype.gamePaused = function () { + this._pauseStarted = this.now; + }; + TimeManager.prototype.gameResumed = function () { + // Level out the delta timer to avoid spikes + this.pauseDuration = this.pausedTime; }; TimeManager.prototype.elapsedSince = /** * How long has passed since given time. @@ -14447,7 +14463,7 @@ var Phaser; return this.now - since; }; TimeManager.prototype.elapsedSecondsSince = /** - * How long has passed since give time (in seconds). + * How long has passed since the given time (in seconds). * @method elapsedSecondsSince * @param {number} since The time you want to measure (in seconds). * @return {number} Duration between given time and now (in seconds). @@ -14487,6 +14503,8 @@ var Phaser; function TweenManager(game) { this.game = game; this._tweens = []; + this.game.onPause.add(this.pauseAll, this); + this.game.onResume.add(this.resumeAll, this); } TweenManager.prototype.getAll = /** * Get all the tween objects in an array. @@ -14560,6 +14578,28 @@ var Phaser; } return true; }; + TweenManager.prototype.pauseAll = function () { + if(this._tweens.length === 0) { + return false; + } + var i = 0; + var numTweens = this._tweens.length; + while(i < numTweens) { + this._tweens[i].pause(); + i++; + } + }; + TweenManager.prototype.resumeAll = function () { + if(this._tweens.length === 0) { + return false; + } + var i = 0; + var numTweens = this._tweens.length; + while(i < numTweens) { + this._tweens[i].resume(); + i++; + } + }; return TweenManager; })(); Phaser.TweenManager = TweenManager; @@ -18419,11 +18459,6 @@ var Phaser; if (typeof destroyCallback === "undefined") { destroyCallback = null; } var _this = this; /** - * Milliseconds of time per step of the game loop. - * @type {number} - */ - //private _step: number = 0; - /** * Whether load complete loading or not. * @type {boolean} */ @@ -18532,6 +18567,8 @@ var Phaser; } else { document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot); window.removeEventListener('load', Phaser.GAMES[this.id].boot); + this.onPause = new Phaser.Signal(); + this.onResume = new Phaser.Signal(); this.device = new Phaser.Device(); this.net = new Phaser.Net(this); this.math = new Phaser.GameMath(this); @@ -18574,18 +18611,6 @@ var Phaser; } } }; - Game.prototype.setRenderer = function (type) { - switch(type) { - case Phaser.Types.RENDERER_AUTO_DETECT: - this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this); - break; - case Phaser.Types.RENDERER_AUTO_DETECT: - case Phaser.Types.RENDERER_CANVAS: - this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this); - break; - // WebGL coming soon :) - } - }; Game.prototype.loadComplete = /** * Called when the load has finished after preload was run. */ @@ -18673,6 +18698,18 @@ var Phaser; this._loadComplete = true; } }; + Game.prototype.setRenderer = function (type) { + switch(type) { + case Phaser.Types.RENDERER_AUTO_DETECT: + this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this); + break; + case Phaser.Types.RENDERER_AUTO_DETECT: + case Phaser.Types.RENDERER_CANVAS: + this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this); + break; + // WebGL coming soon :) + } + }; Game.prototype.setCallbacks = /** * Set the most common state callbacks (init, create, update, render). * @param preloadCallback {function} Init callback invoked when init state. @@ -18796,11 +18833,14 @@ var Phaser; set: function (value) { if(value == true && this._paused == false) { this._paused = true; + this.onPause.dispatch(); + // Hook to the above this.sound.pauseAll(); this._raf.callback = this.pausedLoop; } else if(value == false && this._paused == true) { this._paused = false; - //this.time.time = window.performance.now ? (performance.now() + performance.timing.navigationStart) : Date.now(); + this.onResume.dispatch(); + // Hook to the above this.input.reset(); this.sound.resumeAll(); if(this.isRunning == false) {