Finish another 10 classes' document.

This commit is contained in:
Sean 2013-05-03 19:32:39 +08:00 committed by Richard Davey
parent 9da64e6baf
commit 5cf8b846f8
10 changed files with 667 additions and 68 deletions

View file

@ -13,6 +13,17 @@ module Phaser {
export class GameObject extends Basic { export class GameObject extends Basic {
/**
* GameObject constructor
*
* Create a new <code>GameObject</code> object at specific position with
* specific width and height.
*
* @param x Optinal, the x position of the object.
* @param y Optinal, the y position of the object.
* @param width Optinal, the width of the object.
* @param height Optinal, the height of the object.
*/
constructor(game: Game, x?: number = 0, y?: number = 0, width?: number = 16, height?: number = 16) { constructor(game: Game, x?: number = 0, y?: number = 0, width?: number = 16, height?: number = 16) {
super(game); super(game);
@ -56,66 +67,242 @@ module Phaser {
} }
/**
* Angle of this object.
* @type {number}
*/
private _angle: number = 0; private _angle: number = 0;
/**
* Pivot position enum: at the top-left corner.
* @type {number}
*/
public static ALIGN_TOP_LEFT: number = 0; public static ALIGN_TOP_LEFT: number = 0;
/**
* Pivot position enum: at the top-center corner.
* @type {number}
*/
public static ALIGN_TOP_CENTER: number = 1; public static ALIGN_TOP_CENTER: number = 1;
/**
* Pivot position enum: at the top-right corner.
* @type {number}
*/
public static ALIGN_TOP_RIGHT: number = 2; public static ALIGN_TOP_RIGHT: number = 2;
/**
* Pivot position enum: at the center-left corner.
* @type {number}
*/
public static ALIGN_CENTER_LEFT: number = 3; public static ALIGN_CENTER_LEFT: number = 3;
/**
* Pivot position enum: at the center corner.
* @type {number}
*/
public static ALIGN_CENTER: number = 4; public static ALIGN_CENTER: number = 4;
/**
* Pivot position enum: at the center-right corner.
* @type {number}
*/
public static ALIGN_CENTER_RIGHT: number = 5; public static ALIGN_CENTER_RIGHT: number = 5;
/**
* Pivot position enum: at the bottom-left corner.
* @type {number}
*/
public static ALIGN_BOTTOM_LEFT: number = 6; public static ALIGN_BOTTOM_LEFT: number = 6;
/**
* Pivot position enum: at the bottom-center corner.
* @type {number}
*/
public static ALIGN_BOTTOM_CENTER: number = 7; public static ALIGN_BOTTOM_CENTER: number = 7;
/**
* Pivot position enum: at the bottom-right corner.
* @type {number}
*/
public static ALIGN_BOTTOM_RIGHT: number = 8; public static ALIGN_BOTTOM_RIGHT: number = 8;
/**
* Enum value for outOfBoundsAction. Stop the object when is out of world bounds.
* @type {number}
*/
public static OUT_OF_BOUNDS_STOP: number = 0; public static OUT_OF_BOUNDS_STOP: number = 0;
/**
* Enum value for outOfBoundsAction. Kill the object when is out of world bounds.
* @type {number}
*/
public static OUT_OF_BOUNDS_KILL: number = 1; public static OUT_OF_BOUNDS_KILL: number = 1;
/**
* Position of this object after scrolling.
* @type {MicroPoint}
*/
public _point: MicroPoint; public _point: MicroPoint;
public cameraBlacklist: number[]; public cameraBlacklist: number[];
/**
* Rectangle container of this object.
* @type {Rectangle}
*/
public bounds: Rectangle; public bounds: Rectangle;
/**
* Bound of world.
* @type {Quad}
*/
public worldBounds: Quad; public worldBounds: Quad;
/**
* What action will be performed when object is out of bounds.
* This will default to GameObject.OUT_OF_BOUNDS_STOP.
* @type {number}
*/
public outOfBoundsAction: number = 0; public outOfBoundsAction: number = 0;
/**
* At which point the graphic of this object will align to.
* Align of the object will default to GameObject.ALIGN_TOP_LEFT.
* @type {number}
*/
public align: number; public align: number;
/**
* Oorientation of the object.
* @type {number}
*/
public facing: number; public facing: number;
/**
* Set alpha to a number between 0 and 1 to change the opacity.
* @type {number}
*/
public alpha: number; public alpha: number;
/**
* Scale factor of the object.
* @type {MicroPoint}
*/
public scale: MicroPoint; public scale: MicroPoint;
/**
* Origin is the anchor point that the object will rotate by.
* The origin will default to its center.
* @type {MicroPoint}
*/
public origin: MicroPoint; public origin: MicroPoint;
/**
* Z-order value of the object.
*/
public z: number = 0; public z: number = 0;
// This value is added to the angle of the GameObject. /**
// For example if you had a sprite drawn facing straight up then you could set * This value is added to the angle of the GameObject.
// rotationOffset to 90 and it would correspond correctly with Phasers rotation system * For example if you had a sprite drawn facing straight up then you could set
* rotationOffset to 90 and it would correspond correctly with Phasers rotation system
* @type {number}
*/
public rotationOffset: number = 0; public rotationOffset: number = 0;
/**
* Render graphic based on its angle?
* @type {boolean}
*/
public renderRotation: bool = true; public renderRotation: bool = true;
// Physics properties // Physics properties
/**
* Whether this object will be moved or not.
* @type {boolean}
*/
public immovable: bool; public immovable: bool;
// Velocity is given in pixels per second. Therefore a velocity of /**
// 100 will move at a rate of 100 pixels every 1000 ms (1sec). It's not balls-on * Basic speed of this object.
// accurate due to the way timers work, but it's pretty close. Expect tolerance *
// of +- 10 px. Also that speed assumes no drag * Velocity is given in pixels per second. Therefore a velocity of
* 100 will move at a rate of 100 pixels every 1000 ms (1sec). It's not balls-on
* accurate due to the way timers work, but it's pretty close. Expect tolerance
* of +- 10 px. Also that speed assumes no drag.
*
* @type {MicroPoint}
*/
public velocity: MicroPoint; public velocity: MicroPoint;
/**
* The virtual mass of the object.
* @type {number}
*/
public mass: number; public mass: number;
/**
* The bounciness of the object.
* @type {number}
*/
public elasticity: number; public elasticity: number;
/**
* How fast the speed of this object is changing.
* @type {number}
*/
public acceleration: MicroPoint; public acceleration: MicroPoint;
/**
* This isn't drag exactly, more like deceleration that is only applied
* when acceleration is not affecting the sprite.
* @type {MicroPoint}
*/
public drag: MicroPoint; public drag: MicroPoint;
/**
* It will cap the speed automatically if you use the acceleration
* to change its velocity.
* @type {MicroPoint}
*/
public maxVelocity: MicroPoint; public maxVelocity: MicroPoint;
/**
* How fast this object is rotating.
* @type {number}
*/
public angularVelocity: number; public angularVelocity: number;
/**
* How fast angularVelocity of this object is changing.
* @type {number}
*/
public angularAcceleration: number; public angularAcceleration: number;
/**
* Deacceleration of angularVelocity will be applied when it's rotating.
* @type {number}
*/
public angularDrag: number; public angularDrag: number;
/**
* It will cap the rotate speed automatically if you use the angularAcceleration
* to change its angularVelocity.
* @type {number}
*/
public maxAngular: number; public maxAngular: number;
/**
* A point that can store numbers from 0 to 1 (for X and Y independently)
* which governs how much this object is affected by the camera .
* @type {MicroPoint}
*/
public scrollFactor: MicroPoint; public scrollFactor: MicroPoint;
/**
* Handy for storing health percentage or armor points or whatever.
* @type {number}
*/
public health: number; public health: number;
/**
* Set this to false if you want to skip the automatic motion/movement stuff
* (see updateMotion()).
* @type {boolean}
*/
public moves: bool = true; public moves: bool = true;
/**
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating surface contacts.
* @type {number}
*/
public touching: number; public touching: number;
/**
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating surface contacts from the previous game loop step.
* @type {number}
*/
public wasTouching: number; public wasTouching: number;
/**
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating collision directions.
* @type {number}
*/
public allowCollisions: number; public allowCollisions: number;
/**
* Important variable for collision processing.
* @type {MicroPoint}
*/
public last: MicroPoint; public last: MicroPoint;
// Input // Input
@ -127,6 +314,9 @@ module Phaser {
public onInputDown: Phaser.Signal; public onInputDown: Phaser.Signal;
public onInputUp: Phaser.Signal; public onInputUp: Phaser.Signal;
/**
* Pre-update is called right before update() on each object in the game loop.
*/
public preUpdate() { public preUpdate() {
// flicker time // flicker time
@ -136,9 +326,15 @@ module Phaser {
} }
/**
* Override this function to update your class's position and appearance.
*/
public update() { public update() {
} }
/**
* Automatically called after update() by the game loop.
*/
public postUpdate() { public postUpdate() {
if (this.moves) if (this.moves)
@ -187,9 +383,15 @@ module Phaser {
} }
/**
* Update input.
*/
private updateInput() { private updateInput() {
} }
/**
* Internal function for updating the position and speed of this object.
*/
private updateMotion() { private updateMotion() {
var delta: number; var delta: number;
@ -218,12 +420,12 @@ module Phaser {
* Checks to see if some <code>GameObject</code> overlaps this <code>GameObject</code> or <code>Group</code>. * Checks to see if some <code>GameObject</code> overlaps this <code>GameObject</code> or <code>Group</code>.
* If the group has a LOT of things in it, it might be faster to use <code>Collision.overlaps()</code>. * If the group has a LOT of things in it, it might be faster to use <code>Collision.overlaps()</code>.
* WARNING: Currently tilemaps do NOT support screen space overlap checks! * WARNING: Currently tilemaps do NOT support screen space overlap checks!
* *
* @param ObjectOrGroup The object or group being tested. * @param ObjectOrGroup The object or group being tested.
* @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space." * @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera. * @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* *
* @return Whether or not the two objects overlap. * @return Whether or not the two objects overlap.
*/ */
public overlaps(ObjectOrGroup, InScreenSpace: bool = false, Camera: Camera = null): bool { public overlaps(ObjectOrGroup, InScreenSpace: bool = false, Camera: Camera = null): bool {
@ -268,14 +470,14 @@ module Phaser {
* Checks to see if this <code>GameObject</code> were located at the given position, would it overlap the <code>GameObject</code> or <code>Group</code>? * Checks to see if this <code>GameObject</code> were located at the given position, would it overlap the <code>GameObject</code> or <code>Group</code>?
* This is distinct from overlapsPoint(), which just checks that point, rather than taking the object's size numbero account. * This is distinct from overlapsPoint(), which just checks that point, rather than taking the object's size numbero account.
* WARNING: Currently tilemaps do NOT support screen space overlap checks! * WARNING: Currently tilemaps do NOT support screen space overlap checks!
* *
* @param X The X position you want to check. Pretends this object (the caller, not the parameter) is located here. * @param X The X position you want to check. Pretends this object (the caller, not the parameter) is located here.
* @param Y The Y position you want to check. Pretends this object (the caller, not the parameter) is located here. * @param Y The Y position you want to check. Pretends this object (the caller, not the parameter) is located here.
* @param ObjectOrGroup The object or group being tested. * @param ObjectOrGroup The object or group being tested.
* @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space." * @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera. * @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* *
* @return Whether or not the two objects overlap. * @return Whether or not the two objects overlap.
*/ */
public overlapsAt(X: number, Y: number, ObjectOrGroup, InScreenSpace: bool = false, Camera: Camera = null): bool { public overlapsAt(X: number, Y: number, ObjectOrGroup, InScreenSpace: bool = false, Camera: Camera = null): bool {
@ -321,12 +523,12 @@ module Phaser {
/** /**
* Checks to see if a point in 2D world space overlaps this <code>GameObject</code>. * Checks to see if a point in 2D world space overlaps this <code>GameObject</code>.
* *
* @param Point The point in world space you want to check. * @param Point The point in world space you want to check.
* @param InScreenSpace Whether to take scroll factors into account when checking for overlap. * @param InScreenSpace Whether to take scroll factors into account when checking for overlap.
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera. * @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* *
* @return Whether or not the point overlaps this object. * @return Whether or not the point overlaps this object.
*/ */
public overlapsPoint(point: Point, InScreenSpace: bool = false, Camera: Camera = null): bool { public overlapsPoint(point: Point, InScreenSpace: bool = false, Camera: Camera = null): bool {
@ -351,10 +553,10 @@ module Phaser {
/** /**
* Check and see if this object is currently on screen. * Check and see if this object is currently on screen.
* *
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera. * @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* *
* @return Whether the object is on screen or not. * @return Whether the object is on screen or not.
*/ */
public onScreen(Camera: Camera = null): bool { public onScreen(Camera: Camera = null): bool {
@ -371,11 +573,11 @@ module Phaser {
/** /**
* Call this to figure out the on-screen position of the object. * Call this to figure out the on-screen position of the object.
* *
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera. * @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* @param Point Takes a <code>MicroPoint</code> object and assigns the post-scrolled X and Y values of this object to it. * @param Point Takes a <code>MicroPoint</code> object and assigns the post-scrolled X and Y values of this object to it.
* *
* @return The <code>MicroPoint</code> you passed in, or a new <code>Point</code> if you didn't pass one, containing the screen X and Y position of this object. * @return The <code>MicroPoint</code> you passed in, or a new <code>Point</code> if you didn't pass one, containing the screen X and Y position of this object.
*/ */
public getScreenXY(point: MicroPoint = null, Camera: Camera = null): MicroPoint { public getScreenXY(point: MicroPoint = null, Camera: Camera = null): MicroPoint {
@ -407,9 +609,6 @@ module Phaser {
return (this.allowCollisions & Collision.ANY) > Collision.NONE; return (this.allowCollisions & Collision.ANY) > Collision.NONE;
} }
/**
* @private
*/
public set solid(Solid: bool) { public set solid(Solid: bool) {
if (Solid) if (Solid)
@ -425,10 +624,10 @@ module Phaser {
/** /**
* Retrieve the midpoint of this object in world coordinates. * Retrieve the midpoint of this object in world coordinates.
* *
* @Point Allows you to pass in an existing <code>Point</code> object if you're so inclined. Otherwise a new one is created. * @Point Allows you to pass in an existing <code>Point</code> object if you're so inclined. Otherwise a new one is created.
* *
* @return A <code>Point</code> object containing the midpoint of this object in world coordinates. * @return A <code>Point</code> object containing the midpoint of this object in world coordinates.
*/ */
public getMidpoint(point: MicroPoint = null): MicroPoint { public getMidpoint(point: MicroPoint = null): MicroPoint {
@ -446,9 +645,9 @@ module Phaser {
/** /**
* Handy for reviving game objects. * Handy for reviving game objects.
* Resets their existence flags and position. * Resets their existence flags and position.
* *
* @param X The new X position of this object. * @param X The new X position of this object.
* @param Y The new Y position of this object. * @param Y The new Y position of this object.
*/ */
public reset(X: number, Y: number) { public reset(X: number, Y: number) {
@ -468,10 +667,10 @@ module Phaser {
* Handy for checking if this object is touching a particular surface. * Handy for checking if this object is touching a particular surface.
* For slightly better performance you can just &amp; the value directly numbero <code>touching</code>. * For slightly better performance you can just &amp; the value directly numbero <code>touching</code>.
* However, this method is good for readability and accessibility. * However, this method is good for readability and accessibility.
* *
* @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc). * @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc).
* *
* @return Whether the object is touching an object in (any of) the specified direction(s) this frame. * @return Whether the object is touching an object in (any of) the specified direction(s) this frame.
*/ */
public isTouching(Direction: number): bool { public isTouching(Direction: number): bool {
return (this.touching & Direction) > Collision.NONE; return (this.touching & Direction) > Collision.NONE;
@ -479,10 +678,18 @@ module Phaser {
/** /**
* Handy for checking if this object is just landed on a particular surface. * Handy for checking if this object is just landed on a particular surface.
* *
* @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc). * @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc).
* *
* @return Whether the object just landed on (any of) the specified surface(s) this frame. * @return Whether the object just landed on (any of) the specified surface(s) this frame.
*/
/**
* Handy function for checking if this object is just landed on a particular surface.
*
* @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc).
*
* @returns bool Whether the object just landed on any specicied surfaces.
*/ */
public justTouched(Direction: number): bool { public justTouched(Direction: number): bool {
return ((this.touching & Direction) > Collision.NONE) && ((this.wasTouching & Direction) <= Collision.NONE); return ((this.touching & Direction) > Collision.NONE) && ((this.wasTouching & Direction) <= Collision.NONE);
@ -491,8 +698,8 @@ module Phaser {
/** /**
* Reduces the "health" variable of this sprite by the amount specified in Damage. * Reduces the "health" variable of this sprite by the amount specified in Damage.
* Calls kill() if health drops to or below zero. * Calls kill() if health drops to or below zero.
* *
* @param Damage How much health to take away (use a negative number to give a health bonus). * @param Damage How much health to take away (use a negative number to give a health bonus).
*/ */
public hurt(Damage: number) { public hurt(Damage: number) {
@ -509,6 +716,11 @@ module Phaser {
* Set the world bounds that this GameObject can exist within. By default a GameObject can exist anywhere * Set the world bounds that this GameObject can exist within. By default a GameObject can exist anywhere
* in the world. But by setting the bounds (which are given in world dimensions, not screen dimensions) * in the world. But by setting the bounds (which are given in world dimensions, not screen dimensions)
* it can be stopped from leaving the world, or a section of it. * it can be stopped from leaving the world, or a section of it.
*
* @param x x position of the bound
* @param y y position of the bound
* @param width width of its bound
* @param height height of its bound
*/ */
public setBounds(x: number, y: number, width: number, height: number) { public setBounds(x: number, y: number, width: number, height: number) {
@ -518,6 +730,8 @@ module Phaser {
/** /**
* If you do not wish this object to be visible to a specific camera, pass the camera here. * If you do not wish this object to be visible to a specific camera, pass the camera here.
* @param camera The specific camera.
*/ */
public hideFromCamera(camera: Camera) { public hideFromCamera(camera: Camera) {
@ -528,6 +742,12 @@ module Phaser {
} }
/**
* Make this object only visible to a specific camera.
*
* @param camera The camera you wish it to be visible.
*/
public showToCamera(camera: Camera) { public showToCamera(camera: Camera) {
if (this.cameraBlacklist.indexOf(camera.ID) !== -1) if (this.cameraBlacklist.indexOf(camera.ID) !== -1)
@ -537,12 +757,18 @@ module Phaser {
} }
/**
* This will make the object not visible to any cameras.
*/
public clearCameraList() { public clearCameraList() {
this.cameraBlacklist.length = 0; this.cameraBlacklist.length = 0;
} }
/**
* Clean up memory.
*/
public destroy() { public destroy() {
} }

View file

@ -11,6 +11,14 @@ module Phaser {
export class Sound { export class Sound {
/**
* Sound constructor
* @param context The AudioContext instance.
* @param gainNode Gain node instance.
* @param data Sound data.
* @param volume Optional, volume of this sound when playing.
* @param loop Optional, loop this sound when playing? (Default to false)
*/
constructor(context, gainNode, data, volume?: number = 1, loop?: bool = false) { constructor(context, gainNode, data, volume?: number = 1, loop?: bool = false) {
this._context = context; this._context = context;
@ -38,11 +46,29 @@ module Phaser {
} }
/**
* Local private reference to AudioContext.
*/
private _context; private _context;
/**
* Reference to gain node of SoundManager.
*/
private _gainNode; private _gainNode;
/**
* GainNode of this sound.
*/
private _localGainNode; private _localGainNode;
/**
* Decoded data buffer.
*/
private _buffer; private _buffer;
/**
* Volume of this sound.
*/
private _volume: number; private _volume: number;
/**
* The real sound object (buffer source).
*/
private _sound; private _sound;
loop: bool = false; loop: bool = false;
@ -58,6 +84,9 @@ module Phaser {
} }
/**
* Play this sound.
*/
public play() { public play() {
if (this._buffer === null || this.isDecoding === true) if (this._buffer === null || this.isDecoding === true)
@ -81,6 +110,9 @@ module Phaser {
} }
/**
* Stop playing this sound.
*/
public stop() { public stop() {
if (this.isPlaying === true) if (this.isPlaying === true)
@ -92,12 +124,18 @@ module Phaser {
} }
/**
* Mute the sound.
*/
public mute() { public mute() {
this._localGainNode.gain.value = 0; this._localGainNode.gain.value = 0;
} }
/**
* Enable the sound.
*/
public unmute() { public unmute() {
this._localGainNode.gain.value = this._volume; this._localGainNode.gain.value = this._volume;

View file

@ -12,6 +12,9 @@ module Phaser {
export class StageScaleMode { export class StageScaleMode {
/**
* StageScaleMode constructor
*/
constructor(game: Game) { constructor(game: Game) {
this._game = game; this._game = game;
@ -22,23 +25,51 @@ module Phaser {
} }
/**
* Local private reference to game.
*/
private _game: Game; private _game: Game;
/**
* Stage height when start the game.
* @type {number}
*/
private _startHeight: number = 0; private _startHeight: number = 0;
private _iterations: number; private _iterations: number;
private _check; private _check;
// Specifies that the game be visible in the specified area without trying to preserve the original aspect ratio. /**
* Specifies that the game be visible in the specified area without trying to preserve the original aspect ratio.
* @type {number}
*/
public static EXACT_FIT: number = 0; public static EXACT_FIT: number = 0;
// Specifies that the size of the game be fixed, so that it remains unchanged even if the size of the window changes. /**
* Specifies that the size of the game be fixed, so that it remains unchanged even if the size of the window changes.
* @type {number}
*/
public static NO_SCALE: number = 1; public static NO_SCALE: number = 1;
// Specifies that the entire game be visible in the specified area without distortion while maintaining the original aspect ratio. /**
* Specifies that the entire game be visible in the specified area without distortion while maintaining the original aspect ratio.
* @type {number}
*/
public static SHOW_ALL: number = 2; public static SHOW_ALL: number = 2;
/**
* Width of the stage after calculation.
* @type {number}
*/
public width: number = 0; public width: number = 0;
/**
* Height of the stage after calculation.
* @type {number}
*/
public height: number = 0; public height: number = 0;
/**
* Game orientation angel.
* @type {number}
*/
public orientation; public orientation;
public update() { public update() {
@ -54,6 +85,9 @@ module Phaser {
return window['orientation'] === 90 || window['orientation'] === -90; return window['orientation'] === 90 || window['orientation'] === -90;
} }
/**
* Check whether game orientation the same as window's. Update orientation if not equal.
*/
private checkOrientation(event) { private checkOrientation(event) {
if (window['orientation'] !== this.orientation) if (window['orientation'] !== this.orientation)
@ -64,6 +98,9 @@ module Phaser {
} }
/**
* Re-calculate scale mode and update screen size.
*/
private refresh() { private refresh() {
// We can't do anything about the status bars in iPads, web apps or desktops // We can't do anything about the status bars in iPads, web apps or desktops
@ -91,6 +128,9 @@ module Phaser {
} }
/**
* Set screen size automatically based on stage's scaleMode.
*/
private setScreenSize() { private setScreenSize() {
if (this._game.device.iPad == false && this._game.device.webApp == false && this._game.device.desktop == false) if (this._game.device.iPad == false && this._game.device.webApp == false && this._game.device.desktop == false)

View file

@ -10,6 +10,15 @@ module Phaser {
export class Tile { export class Tile {
/**
* Tile constructor
* Create a new <code>Tile</code>.
*
* @param tilemap the tilemap this tile belongs to.
* @param index The index of this tile type in the core map data.
* @param width Width of the tile.
* @param height Height of the tile.
*/
constructor(game: Game, tilemap: Tilemap, index: number, width: number, height: number) { constructor(game: Game, tilemap: Tilemap, index: number, width: number, height: number) {
this._game = game; this._game = game;
@ -22,27 +31,74 @@ module Phaser {
} }
/**
* Local private reference to game.
*/
private _game: Game; private _game: Game;
// You can give this Tile a friendly name to help with debugging. Never used internally. /**
* You can give this Tile a friendly name to help with debugging. Never used internally.
* @type {string}
*/
public name: string; public name: string;
/**
* The virtual mass of the tile.
* @type {number}
*/
public mass: number = 1.0; public mass: number = 1.0;
/**
* Tile width.
* @type {number}
*/
public width: number; public width: number;
/**
* Tile height.
* @type {number}
*/
public height: number; public height: number;
/**
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating collision directions.
* @type {number}
*/
public allowCollisions: number; public allowCollisions: number;
/**
* Indicating collide with any object on the left.
* @type {boolean}
*/
public collideLeft: bool = false; public collideLeft: bool = false;
/**
* Indicating collide with any object on the right.
* @type {boolean}
*/
public collideRight: bool = false; public collideRight: bool = false;
/**
* Indicating collide with any object on the top.
* @type {boolean}
*/
public collideUp: bool = false; public collideUp: bool = false;
/**
* Indicating collide with any object on the bottom.
* @type {boolean}
*/
public collideDown: bool = false; public collideDown: bool = false;
/**
* Enable separation at x-axis.
* @type {boolean}
*/
public separateX: bool = true; public separateX: bool = true;
/**
* Enable separation at y-axis.
* @type {boolean}
*/
public separateY: bool = true; public separateY: bool = true;
/** /**
* A reference to the tilemap this tile object belongs to. * A reference to the tilemap this tile object belongs to.
* @type {Tilemap}
*/ */
public tilemap: Tilemap; public tilemap: Tilemap;
@ -50,6 +106,7 @@ module Phaser {
* The index of this tile type in the core map data. * The index of this tile type in the core map data.
* For example, if your map only has 16 kinds of tiles in it, * For example, if your map only has 16 kinds of tiles in it,
* this number is usually between 0 and 15. * this number is usually between 0 and 15.
* @type {number}
*/ */
public index: number; public index: number;
@ -62,6 +119,13 @@ module Phaser {
} }
/**
* Set collision configs.
* @param collision Bit field of flags. (see Tile.allowCollision)
* @param resetCollisions Reset collision flags before set.
* @param separateX Enable seprate at x-axis.
* @param separateY Enable seprate at y-axis.
*/
public setCollision(collision: number, resetCollisions: bool, separateX: bool, separateY: bool) { public setCollision(collision: number, resetCollisions: bool, separateX: bool, separateY: bool) {
if (resetCollisions) if (resetCollisions)
@ -105,6 +169,9 @@ module Phaser {
} }
/**
* Reset collision status flags.
*/
public resetCollision() { public resetCollision() {
this.allowCollisions = Collision.NONE; this.allowCollisions = Collision.NONE;

View file

@ -10,6 +10,13 @@ module Phaser {
export class Animation { export class Animation {
/**
* Animation constructor
* Create a new <code>Animation</code>.
*
* @param width Width of the world bound.
* @param height Height of the world bound.
*/
constructor(game: Game, parent: Sprite, frameData: FrameData, name: string, frames, delay: number, looped: bool) { constructor(game: Game, parent: Sprite, frameData: FrameData, name: string, frames, delay: number, looped: bool) {
this._game = game; this._game = game;

View file

@ -10,6 +10,14 @@ module Phaser {
export class AnimationLoader { export class AnimationLoader {
/**
* Parse a sprite sheet from asset data.
* @param key Asset key for the sprite sheet data.
* @param frameWidth Width of animation frame.
* @param frameHeight Height of animation frame.
* @param frameMax Number of animation frames.
* @return {FrameData=} Generated FrameData object.
*/
public static parseSpriteSheet(game: Game, key: string, frameWidth: number, frameHeight: number, frameMax: number): FrameData { public static parseSpriteSheet(game: Game, key: string, frameWidth: number, frameHeight: number, frameMax: number): FrameData {
// How big is our image? // How big is our image?
@ -63,6 +71,11 @@ module Phaser {
} }
/**
* Parse frame datas from json.
* @param json Json data you want to parse.
* @return {FrameData=} Generated FrameData object.
*/
public static parseJSONData(game: Game, json): FrameData { public static parseJSONData(game: Game, json): FrameData {
// Let's create some frames then // Let's create some frames then

View file

@ -10,6 +10,16 @@ module Phaser {
export class Frame { export class Frame {
/**
* Frame constructor
* Create a new <code>Frame</code> with specific position, size and name.
*
* @param x X position within the image to cut from.
* @param y Y position within the image to cut from.
* @param width Width of the frame.
* @param height Height of the frame.
* @param name Name of this frame.
*/
constructor(x: number, y: number, width: number, height: number, name: string) { constructor(x: number, y: number, width: number, height: number, name: string) {
this.x = x; this.x = x;
@ -23,39 +33,103 @@ module Phaser {
} }
// Position within the image to cut from /**
* X position within the image to cut from.
* @type {number}
*/
public x: number; public x: number;
/**
* Y position within the image to cut from.
* @type {number}
*/
public y: number; public y: number;
/**
* Width of the frame.
* @type {number}
*/
public width: number; public width: number;
/**
* Height of the frame.
* @type {number}
*/
public height: number; public height: number;
// Useful for Sprite Sheets /**
* Useful for Sprite Sheets.
* @type {number}
*/
public index: number; public index: number;
// Useful for Texture Atlas files (is set to the filename value) /**
* Useful for Texture Atlas files. (is set to the filename value)
*/
public name: string = ''; public name: string = '';
// Rotated? (not yet implemented) /**
* Rotated? (not yet implemented)
*/
public rotated: bool = false; public rotated: bool = false;
// Either cw or ccw, rotation is always 90 degrees /**
* Either cw or ccw, rotation is always 90 degrees.
*/
public rotationDirection: string = 'cw'; public rotationDirection: string = 'cw';
// Was it trimmed when packed? /**
* Was it trimmed when packed?
* @type {boolean}
*/
public trimmed: bool; public trimmed: bool;
// The coordinates of the trimmed sprite inside the original sprite // The coordinates of the trimmed sprite inside the original sprite
/**
* Width of the original sprite.
* @type {number}
*/
public sourceSizeW: number; public sourceSizeW: number;
/**
* Height of the original sprite.
* @type {number}
*/
public sourceSizeH: number; public sourceSizeH: number;
/**
* X position of the trimmed sprite inside original sprite.
* @type {number}
*/
public spriteSourceSizeX: number; public spriteSourceSizeX: number;
/**
* Y position of the trimmed sprite inside original sprite.
* @type {number}
*/
public spriteSourceSizeY: number; public spriteSourceSizeY: number;
/**
* Width of the trimmed sprite.
* @type {number}
*/
public spriteSourceSizeW: number; public spriteSourceSizeW: number;
/**
* Height of the trimmed sprite.
* @type {number}
*/
public spriteSourceSizeH: number; public spriteSourceSizeH: number;
/**
* Set rotation of this frame. (Not yet supported!)
*/
public setRotation(rotated: bool, rotationDirection: string) { public setRotation(rotated: bool, rotationDirection: string) {
// Not yet supported // Not yet supported
} }
/**
* Set trim of the frame.
* @param trimmed Whether this frame trimmed or not.
* @param actualWidth Actual width of this frame.
* @param actualHeight Actual height of this frame.
* @param destX Destiny x position.
* @param destY Destiny y position.
* @param destWidth Destiny draw width.
* @param destHeight Destiny draw height.
*/
public setTrim(trimmed: bool, actualWidth, actualHeight, destX, destY, destWidth, destHeight, ) { public setTrim(trimmed: bool, actualWidth, actualHeight, destX, destY, destWidth, destHeight, ) {
this.trimmed = trimmed; this.trimmed = trimmed;

View file

@ -10,6 +10,9 @@ module Phaser {
export class FrameData { export class FrameData {
/**
* FrameData constructor
*/
constructor() { constructor() {
this._frames = []; this._frames = [];
@ -17,13 +20,24 @@ module Phaser {
} }
/**
* Local frame container.
*/
private _frames: Frame[]; private _frames: Frame[];
/**
* Local frameName<->index container.
*/
private _frameNames; private _frameNames;
public get total(): number { public get total(): number {
return this._frames.length; return this._frames.length;
} }
/**
* Add a new frame.
* @param frame The frame you want to add.
* @return {Frame=} The frame you just added.
*/
public addFrame(frame: Frame): Frame { public addFrame(frame: Frame): Frame {
frame.index = this._frames.length; frame.index = this._frames.length;
@ -39,6 +53,11 @@ module Phaser {
} }
/**
* Get a frame by its index.
* @param index Index of the frame you want to get.
* @return {Frame=} The frame you want.
*/
public getFrame(index: number): Frame { public getFrame(index: number): Frame {
if (this._frames[index]) if (this._frames[index])
@ -50,6 +69,11 @@ module Phaser {
} }
/**
* Get a frame by its name.
* @param name Name of the frame you want to get.
* @return {Frame=} The frame you want.
*/
public getFrameByName(name: string): Frame { public getFrameByName(name: string): Frame {
if (this._frameNames[name] >= 0) if (this._frameNames[name] >= 0)
@ -61,6 +85,11 @@ module Phaser {
} }
/**
* Check whether there's a frame with given name.
* @param name Name of the frame you want to check.
* @return {boolean} True if frame with given name found, otherwise return false.
*/
public checkFrameName(name: string): bool { public checkFrameName(name: string): bool {
if (this._frameNames[name] >= 0) if (this._frameNames[name] >= 0)
@ -72,6 +101,13 @@ module Phaser {
} }
/**
* Get ranges of frames in an array.
* @param start Start index of frames you want.
* @param end End index of frames you want.
* @param output Optional, result will be added into this array.
* @return {array} Ranges of specific frames in an array.
*/
public getFrameRange(start: number, end: number, output?: Frame[] = []): Frame[] { public getFrameRange(start: number, end: number, output?: Frame[] = []): Frame[] {
for (var i = start; i <= end; i++) for (var i = start; i <= end; i++)
@ -83,6 +119,11 @@ module Phaser {
} }
/**
* Get all indexes of frames by giving their name.
* @param output Optional, result will be added into this array.
* @return {array} Indexes of specific frames in an array.
*/
public getFrameIndexes(output?: number[] = []): number[] { public getFrameIndexes(output?: number[] = []): number[] {
output.length = 0; output.length = 0;
@ -96,6 +137,11 @@ module Phaser {
} }
/**
* Get all names of frames by giving their indexes.
* @param output Optional, result will be added into this array.
* @return {array} Names of specific frames in an array.
*/
public getFrameIndexesByName(input: string[]): number[] { public getFrameIndexesByName(input: string[]): number[] {
var output: number[] = []; var output: number[] = [];
@ -112,10 +158,19 @@ module Phaser {
} }
/**
* Get all frames in this frame data.
* @return {array} All the frames in an array.
*/
public getAllFrames(): Frame[] { public getAllFrames(): Frame[] {
return this._frames; return this._frames;
} }
/**
* Get All frames with specific ranges.
* @param range Ranges in an array.
* @return All frames in an array.
*/
public getFrames(range: number[]) { public getFrames(range: number[]) {
var output: Frame[] = []; var output: Frame[] = [];

View file

@ -10,6 +10,14 @@ module Phaser {
export class BootScreen { export class BootScreen {
/**
* BootScreen constructor
*
* Create a new <code>BootScreen</code> with specific width and height.
*
* @param width Screen canvas width.
* @param height Screen canvas height.
*/
constructor(game:Game) { constructor(game:Game) {
this._game = game; this._game = game;
@ -19,13 +27,35 @@ module Phaser {
} }
/**
* Local private reference to game.
*/
private _game: Game; private _game: Game;
/**
* Engine logo.
*/
private _logo; private _logo;
/**
* Engine logo image data.
*/
private _logoData: string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGgAAAAZCAYAAADdYmvFAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAstJREFUeNrsWlFuwjAMbavdZGcAcRm4AXzvCPuGG8BlEJxhZ+l4TJ48z3actGGthqUI1MaO/V6cmIT2/fW10eTt46NvKshtvDZlG31yfOL9a/ldU6x4IZ0GQs0gS217enMkJYr5ixXkYrFoVqtV1kDn8/n+KfXw/Hq9Nin7h8MhScB2u3Xtav2ivsNWrh7XLcWMYqA4eUZ1kj0MAifHJEeKFojWzyIH+rL/0Cwif2AX9nN1oQOgrTg8XcTFx+ScdEOJ4WBxXQ1EjRyrn0cOzzQLzFyQSQcgw/5Qkkr0JVEQpNIdhL4vm4DL5fLulNTHcy6Uxl4/6iMLiePx2KzX6/v30+n0aynUlrnSeNq2/VN9bgM4dFPdNPmsJnIg/PuQbJmLdFN3UNu0SzbyJ0GOWJVWZE/QMkY+owrqXxGEdZA37BVyX6lJTipT6J1lf7fbqc+xh8nYeIvikatP+PGW0nEJ4jOydHYOIcfKnmgWoZDQSIIeio4Sf1IthYWskCO4vqQ6lFYjl8tl9L1H67PZbMz3VO3t93uVXHofmUjReLyMwHi5eCb3ICwJj5ZU9nCg+SzUgPYyif+2epTk4pkkyDp+eXTlZu2BkUybEkklePZfK9lPuTnc07vbmt1bYulHBeNQgx18SsH4ni/cV2rSLtqNDNUH2JQ2SsXS57Y9PHlfumkwCdICt5rnkNdPjpMiIEWgRlAJSdF4SvCQMWj+VyfI0h8D/EgWSYKiJKXi8VrOhJUxaFiFCOKKUJAtR78k9eX4USLHXqLGXOIiWUT4Vj9JiP4W0io3VDz8AJXblNWQrOimLjIGy/9uLICH6mrVmFbxEFHauzmc0fGJJmPg/v+6D0oB7N2bj0FsNHtSWTQniWTR931QlHXvasDTHXLjqY0/1/8hSDxACD+lAGH8dKQbQk5N3TFtzDmLWutvV0+pL5FVoHvCNG35FGAAayS4KUoKC9QAAAAASUVORK5CYII="; private _logoData: string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGgAAAAZCAYAAADdYmvFAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAstJREFUeNrsWlFuwjAMbavdZGcAcRm4AXzvCPuGG8BlEJxhZ+l4TJ48z3actGGthqUI1MaO/V6cmIT2/fW10eTt46NvKshtvDZlG31yfOL9a/ldU6x4IZ0GQs0gS217enMkJYr5ixXkYrFoVqtV1kDn8/n+KfXw/Hq9Nin7h8MhScB2u3Xtav2ivsNWrh7XLcWMYqA4eUZ1kj0MAifHJEeKFojWzyIH+rL/0Cwif2AX9nN1oQOgrTg8XcTFx+ScdEOJ4WBxXQ1EjRyrn0cOzzQLzFyQSQcgw/5Qkkr0JVEQpNIdhL4vm4DL5fLulNTHcy6Uxl4/6iMLiePx2KzX6/v30+n0aynUlrnSeNq2/VN9bgM4dFPdNPmsJnIg/PuQbJmLdFN3UNu0SzbyJ0GOWJVWZE/QMkY+owrqXxGEdZA37BVyX6lJTipT6J1lf7fbqc+xh8nYeIvikatP+PGW0nEJ4jOydHYOIcfKnmgWoZDQSIIeio4Sf1IthYWskCO4vqQ6lFYjl8tl9L1H67PZbMz3VO3t93uVXHofmUjReLyMwHi5eCb3ICwJj5ZU9nCg+SzUgPYyif+2epTk4pkkyDp+eXTlZu2BkUybEkklePZfK9lPuTnc07vbmt1bYulHBeNQgx18SsH4ni/cV2rSLtqNDNUH2JQ2SsXS57Y9PHlfumkwCdICt5rnkNdPjpMiIEWgRlAJSdF4SvCQMWj+VyfI0h8D/EgWSYKiJKXi8VrOhJUxaFiFCOKKUJAtR78k9eX4USLHXqLGXOIiWUT4Vj9JiP4W0io3VDz8AJXblNWQrOimLjIGy/9uLICH6mrVmFbxEFHauzmc0fGJJmPg/v+6D0oB7N2bj0FsNHtSWTQniWTR931QlHXvasDTHXLjqY0/1/8hSDxACD+lAGH8dKQbQk5N3TFtzDmLWutvV0+pL5FVoHvCNG35FGAAayS4KUoKC9QAAAAASUVORK5CYII=";
/**
* Background gradient effect color 1.
*/
private _color1 = { r: 20, g: 20, b: 20 }; private _color1 = { r: 20, g: 20, b: 20 };
/**
* Background gradient effect color 2.
*/
private _color2 = { r: 200, g: 200, b: 200 }; private _color2 = { r: 200, g: 200, b: 200 };
/**
* Fade effect tween.
* @type {Phaser.Tween}
*/
private _fade: Phaser.Tween = null; private _fade: Phaser.Tween = null;
/**
* Update color and fade.
*/
public update() { public update() {
if (this._fade == null) if (this._fade == null)
@ -42,6 +72,9 @@ module Phaser {
} }
/**
* Render BootScreen.
*/
public render() { public render() {
var grd = this._game.stage.context.createLinearGradient(0, 0, 0, this._game.stage.height); var grd = this._game.stage.context.createLinearGradient(0, 0, 0, this._game.stage.height);
@ -75,6 +108,9 @@ module Phaser {
} }
/**
* Start color fading cycle.
*/
private colorCycle() { private colorCycle() {
this._fade = this._game.createTween(this._color2); this._fade = this._game.createTween(this._color2);

View file

@ -10,6 +10,14 @@ module Phaser {
export class PauseScreen { export class PauseScreen {
/**
* PauseScreen constructor
*
* Create a new <code>PauseScreen</code> with specific width and height.
*
* @param width Screen canvas width.
* @param height Screen canvas height.
*/
constructor(game: Game, width: number, height: number) { constructor(game: Game, width: number, height: number) {
this._game = game; this._game = game;
@ -20,14 +28,34 @@ module Phaser {
} }
/**
* Local private reference to game.
*/
private _game: Game; private _game: Game;
/**
* Canvas element used by engine.
* @type {HTMLCanvasElement}
*/
private _canvas: HTMLCanvasElement; private _canvas: HTMLCanvasElement;
/**
* Render context of stage's canvas.
* @type {CanvasRenderingContext2D}
*/
private _context: CanvasRenderingContext2D; private _context: CanvasRenderingContext2D;
/**
* Background color.
*/
private _color; private _color;
/**
* Fade effect tween.
* @type {Phaser.Tween}
*/
private _fade: Phaser.Tween; private _fade: Phaser.Tween;
// Called when the game enters pause mode /**
* Called when the game enters pause mode.
*/
public onPaused() { public onPaused() {
// Take a grab of the current canvas to our temporary one // Take a grab of the current canvas to our temporary one
@ -38,17 +66,26 @@ module Phaser {
} }
/**
* Called when the game resume from pause mode.
*/
public onResume() { public onResume() {
this._fade.stop(); this._fade.stop();
this._game.tweens.remove(this._fade); this._game.tweens.remove(this._fade);
} }
/**
* Update background color.
*/
public update() { public update() {
this._color.r = Math.round(this._color.r); this._color.r = Math.round(this._color.r);
this._color.g = Math.round(this._color.g); this._color.g = Math.round(this._color.g);
this._color.b = Math.round(this._color.b); this._color.b = Math.round(this._color.b);
} }
/**
* Render PauseScreen.
*/
public render() { public render() {
this._game.stage.context.drawImage(this._canvas, 0, 0); this._game.stage.context.drawImage(this._canvas, 0, 0);
@ -73,6 +110,9 @@ module Phaser {
} }
/**
* Start fadeOut effect.
*/
private fadeOut() { private fadeOut() {
this._fade = this._game.createTween(this._color); this._fade = this._game.createTween(this._color);
@ -83,6 +123,9 @@ module Phaser {
} }
/**
* Start fadeIn effect.
*/
private fadeIn() { private fadeIn() {
this._fade = this._game.createTween(this._color); this._fade = this._game.createTween(this._color);