2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Tile
|
|
|
|
*
|
2013-04-25 00:55:56 +00:00
|
|
|
* A Tile is a single representation of a tile within a Tilemap
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
export class Tile {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
constructor(game: Game, tilemap: Tilemap, index: number, width: number, height: number) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
this._game = game;
|
|
|
|
this.tilemap = tilemap;
|
|
|
|
this.index = index;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
this.allowCollisions = Collision.NONE;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Local private reference to game.
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
private _game: Game;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* You can give this Tile a friendly name to help with debugging. Never used internally.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
public name: string;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* The virtual mass of the tile.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-28 11:25:02 +00:00
|
|
|
public mass: number = 1.0;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Tile width.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
public width: number;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Tile height.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
public height: number;
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating collision directions.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
public allowCollisions: number;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Indicating collide with any object on the left.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-28 11:25:02 +00:00
|
|
|
public collideLeft: bool = false;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Indicating collide with any object on the right.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-28 11:25:02 +00:00
|
|
|
public collideRight: bool = false;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Indicating collide with any object on the top.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-28 11:25:02 +00:00
|
|
|
public collideUp: bool = false;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Indicating collide with any object on the bottom.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-28 11:25:02 +00:00
|
|
|
public collideDown: bool = false;
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Enable separation at x-axis.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public separateX: bool = true;
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Enable separation at y-axis.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public separateY: bool = true;
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* A reference to the tilemap this tile object belongs to.
|
2013-05-03 11:32:39 +00:00
|
|
|
* @type {Tilemap}
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
public tilemap: Tilemap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The index of this tile type in the core map data.
|
|
|
|
* For example, if your map only has 16 kinds of tiles in it,
|
|
|
|
* this number is usually between 0 and 15.
|
2013-05-03 11:32:39 +00:00
|
|
|
* @type {number}
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
public index: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up memory.
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
|
|
|
|
this.tilemap = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public setCollision(collision: number, resetCollisions: bool, separateX: bool, separateY: bool) {
|
2013-04-28 11:25:02 +00:00
|
|
|
|
|
|
|
if (resetCollisions)
|
|
|
|
{
|
|
|
|
this.resetCollision();
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:37:45 +00:00
|
|
|
this.separateX = separateX;
|
|
|
|
this.separateY = separateY;
|
|
|
|
|
2013-04-28 11:25:02 +00:00
|
|
|
this.allowCollisions = collision;
|
|
|
|
|
|
|
|
if (collision & Collision.ANY)
|
|
|
|
{
|
|
|
|
this.collideLeft = true;
|
|
|
|
this.collideRight = true;
|
|
|
|
this.collideUp = true;
|
|
|
|
this.collideDown = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collision & Collision.LEFT || collision & Collision.WALL)
|
|
|
|
{
|
|
|
|
this.collideLeft = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collision & Collision.RIGHT || collision & Collision.WALL)
|
|
|
|
{
|
|
|
|
this.collideRight = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collision & Collision.UP || collision & Collision.CEILING)
|
|
|
|
{
|
|
|
|
this.collideUp = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collision & Collision.DOWN || collision & Collision.CEILING)
|
|
|
|
{
|
|
|
|
this.collideDown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:32:39 +00:00
|
|
|
/**
|
|
|
|
* Reset collision status flags.
|
|
|
|
*/
|
2013-04-28 11:25:02 +00:00
|
|
|
public resetCollision() {
|
|
|
|
|
|
|
|
this.allowCollisions = Collision.NONE;
|
|
|
|
this.collideLeft = false;
|
|
|
|
this.collideRight = false;
|
|
|
|
this.collideUp = false;
|
|
|
|
this.collideDown = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
/**
|
|
|
|
* Returns a string representation of this object.
|
|
|
|
* @method toString
|
|
|
|
* @return {string} a string representation of the object.
|
|
|
|
**/
|
|
|
|
public toString(): string {
|
|
|
|
|
|
|
|
return "[{Tiled (index=" + this.index + " collisions=" + this.allowCollisions + " width=" + this.width + " height=" + this.height + ")}]";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|