2013-08-06 02:43:53 +00:00
|
|
|
/// <reference path="../../Phaser/Game.ts" />
|
|
|
|
/// <reference path="../../Phaser/core/Plugin.ts" />
|
2013-05-01 03:10:21 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-06 02:43:53 +00:00
|
|
|
* Phaser - Plugins - Camera FX - Fade
|
2013-05-01 03:10:21 +00:00
|
|
|
*
|
|
|
|
* The camera is filled with the given color and returns to normal at the given duration.
|
|
|
|
*/
|
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
module Phaser.Plugins.CameraFX {
|
2013-05-01 03:10:21 +00:00
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
export class Fade extends Phaser.Plugin {
|
2013-05-01 03:10:21 +00:00
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
constructor(game: Phaser.Game, parent) {
|
2013-05-01 03:10:21 +00:00
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
super(game, parent);
|
|
|
|
this.camera = parent;
|
2013-05-01 03:10:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
public camera: Phaser.Camera;
|
2013-05-01 03:10:21 +00:00
|
|
|
|
|
|
|
private _fxFadeColor: string;
|
|
|
|
private _fxFadeComplete = null;
|
|
|
|
private _fxFadeDuration: number = 0;
|
|
|
|
private _fxFadeAlpha: number = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The camera is gradually filled with this color.
|
|
|
|
*
|
|
|
|
* @param Color The color you want to use in 0xRRGGBB format, i.e. 0xffffff for white.
|
|
|
|
* @param Duration How long it takes for the flash to fade.
|
|
|
|
* @param OnComplete An optional function you want to run when the flash finishes. Set to null for no callback.
|
|
|
|
* @param Force Force an already running flash effect to reset.
|
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public start(color: number = 0x000000, duration: number = 1, onComplete = null, force: bool = false) {
|
2013-05-01 03:10:21 +00:00
|
|
|
|
|
|
|
if (force === false && this._fxFadeAlpha > 0)
|
|
|
|
{
|
|
|
|
// You can't fade again unless you force it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duration <= 0)
|
|
|
|
{
|
|
|
|
duration = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var red = color >> 16 & 0xFF;
|
|
|
|
var green = color >> 8 & 0xFF;
|
|
|
|
var blue = color & 0xFF;
|
|
|
|
|
|
|
|
this._fxFadeColor = 'rgba(' + red + ',' + green + ',' + blue + ',';
|
|
|
|
this._fxFadeDuration = duration;
|
|
|
|
this._fxFadeAlpha = 0.01;
|
|
|
|
this._fxFadeComplete = onComplete;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public postUpdate() {
|
|
|
|
|
|
|
|
// Update the Fade effect
|
|
|
|
if (this._fxFadeAlpha > 0)
|
|
|
|
{
|
2013-08-06 02:43:53 +00:00
|
|
|
this._fxFadeAlpha += this.game.time.elapsed / this._fxFadeDuration;
|
2013-05-01 03:10:21 +00:00
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
if (this.game.math.roundTo(this._fxFadeAlpha, -2) >= 1)
|
2013-05-01 03:10:21 +00:00
|
|
|
{
|
|
|
|
this._fxFadeAlpha = 1;
|
|
|
|
|
|
|
|
if (this._fxFadeComplete !== null)
|
|
|
|
{
|
|
|
|
this._fxFadeComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-06 02:43:53 +00:00
|
|
|
public postRender() {
|
2013-05-01 03:10:21 +00:00
|
|
|
|
|
|
|
// "Fade" FX
|
|
|
|
if (this._fxFadeAlpha > 0)
|
|
|
|
{
|
2013-08-06 02:43:53 +00:00
|
|
|
this.game.stage.context.fillStyle = this._fxFadeColor + this._fxFadeAlpha + ')';
|
|
|
|
this.game.stage.context.fillRect(this.camera.screenView.x, this.camera.screenView.y, this.camera.width, this.camera.height);
|
2013-05-01 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|