2013-04-29 01:41:19 +00:00
|
|
|
/// <reference path="../../Game.ts" />
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phaser - PauseScreen
|
|
|
|
*
|
|
|
|
* The PauseScreen is displayed whenever the game loses focus or the player switches to another browser tab.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
|
|
|
export class PauseScreen {
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* PauseScreen constructor
|
|
|
|
* Create a new <code>PauseScreen</code> with specific width and height.
|
|
|
|
*
|
2013-05-04 11:53:11 +00:00
|
|
|
* @param width {number} Screen canvas width.
|
|
|
|
* @param height {number} Screen canvas height.
|
2013-05-03 11:32:39 +00:00
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
constructor(game: Game, width: number, height: number) {
|
|
|
|
|
|
|
|
this._game = game;
|
|
|
|
this._canvas = <HTMLCanvasElement> document.createElement('canvas');
|
|
|
|
this._canvas.width = width;
|
|
|
|
this._canvas.height = height;
|
|
|
|
this._context = this._canvas.getContext('2d');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Local private reference to game.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private _game: Game;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Canvas element used by engine.
|
|
|
|
* @type {HTMLCanvasElement}
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private _canvas: HTMLCanvasElement;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Render context of stage's canvas.
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private _context: CanvasRenderingContext2D;
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Background color.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private _color;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Fade effect tween.
|
|
|
|
* @type {Phaser.Tween}
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private _fade: Phaser.Tween;
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Called when the game enters pause mode.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
public onPaused() {
|
|
|
|
|
|
|
|
// Take a grab of the current canvas to our temporary one
|
|
|
|
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
|
|
|
this._context.drawImage(this._game.stage.canvas, 0, 0);
|
|
|
|
this._color = { r: 255, g: 255, b: 255 };
|
|
|
|
this.fadeOut();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Called when the game resume from pause mode.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
public onResume() {
|
|
|
|
this._fade.stop();
|
|
|
|
this._game.tweens.remove(this._fade);
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Update background color.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
public update() {
|
|
|
|
this._color.r = Math.round(this._color.r);
|
|
|
|
this._color.g = Math.round(this._color.g);
|
|
|
|
this._color.b = Math.round(this._color.b);
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Render PauseScreen.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
public render() {
|
|
|
|
|
|
|
|
this._game.stage.context.drawImage(this._canvas, 0, 0);
|
|
|
|
|
|
|
|
this._game.stage.context.fillStyle = 'rgba(0, 0, 0, 0.4)';
|
|
|
|
this._game.stage.context.fillRect(0, 0, this._game.stage.width, this._game.stage.height);
|
|
|
|
|
|
|
|
// Draw a 'play' arrow
|
|
|
|
var arrowWidth = Math.round(this._game.stage.width / 2);
|
|
|
|
var arrowHeight = Math.round(this._game.stage.height / 2);
|
|
|
|
|
|
|
|
var sx = this._game.stage.centerX - arrowWidth / 2;
|
|
|
|
var sy = this._game.stage.centerY - arrowHeight / 2;
|
|
|
|
|
|
|
|
this._game.stage.context.beginPath();
|
|
|
|
this._game.stage.context.moveTo(sx, sy);
|
|
|
|
this._game.stage.context.lineTo(sx, sy + arrowHeight);
|
|
|
|
this._game.stage.context.lineTo(sx + arrowWidth, this._game.stage.centerY);
|
|
|
|
this._game.stage.context.fillStyle = 'rgba(' + this._color.r + ', ' + this._color.g + ', ' + this._color.b + ', 0.8)';
|
|
|
|
this._game.stage.context.fill();
|
|
|
|
this._game.stage.context.closePath();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Start fadeOut effect.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private fadeOut() {
|
|
|
|
|
|
|
|
this._fade = this._game.createTween(this._color);
|
|
|
|
|
|
|
|
this._fade.to({ r: 50, g: 50, b: 50 }, 1000, Phaser.Easing.Linear.None);
|
|
|
|
this._fade.onComplete.add(this.fadeIn, this);
|
|
|
|
this._fade.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Start fadeIn effect.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private fadeIn() {
|
|
|
|
|
|
|
|
this._fade = this._game.createTween(this._color);
|
|
|
|
|
|
|
|
this._fade.to({ r: 255, g: 255, b: 255 }, 1000, Phaser.Easing.Linear.None);
|
|
|
|
this._fade.onComplete.add(this.fadeOut, this);
|
|
|
|
this._fade.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|