2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Tilemap
|
|
|
|
*
|
|
|
|
* This GameObject allows for the display of a tilemap within the game world. Tile maps consist of an image, tile data and a size.
|
2013-04-22 00:53:24 +00:00
|
|
|
* Internally it creates a TilemapLayer for each layer in the tilemap.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
export class Tilemap {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Tilemap constructor
|
|
|
|
* Create a new <code>Tilemap</code>.
|
|
|
|
*
|
|
|
|
* @param game {Phaser.Game} Current game instance.
|
|
|
|
* @param key {string} Asset key for this map.
|
|
|
|
* @param mapData {string} Data of this map. (a big 2d array, normally in csv)
|
|
|
|
* @param format {number} Format of this map data, available: Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @param resizeWorld {bool} Resize the world bound automatically based on this tilemap?
|
2013-05-04 11:59:59 +00:00
|
|
|
* @param tileWidth {number} Width of tiles in this map.
|
|
|
|
* @param tileHeight {number} Height of tiles in this map.
|
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
constructor(game: Game, key: string, mapData: string, format: number, resizeWorld: bool = true, tileWidth: number = 0, tileHeight: number = 0) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
this.game = game;
|
|
|
|
this.type = Phaser.Types.TILEMAP;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
this.exists = true;
|
|
|
|
this.active = true;
|
|
|
|
this.visible = true;
|
|
|
|
this.alive = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-07-02 22:41:25 +00:00
|
|
|
this.z = -1;
|
|
|
|
this.group = null;
|
|
|
|
this.name = '';
|
|
|
|
|
2013-08-06 02:14:48 +00:00
|
|
|
this.texture = new Phaser.Display.Texture(this);
|
|
|
|
this.transform = new Phaser.Components.TransformManager(this);
|
2013-07-02 22:41:25 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
this.tiles = [];
|
|
|
|
this.layers = [];
|
2013-04-22 00:53:24 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.mapFormat = format;
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case Tilemap.FORMAT_CSV:
|
2013-04-22 00:53:24 +00:00
|
|
|
this.parseCSV(game.cache.getText(mapData), key, tileWidth, tileHeight);
|
2013-04-18 13:16:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Tilemap.FORMAT_TILED_JSON:
|
2013-04-22 00:53:24 +00:00
|
|
|
this.parseTiledJSON(game.cache.getText(mapData), key);
|
2013-04-18 13:16:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
if (this.currentLayer && resizeWorld)
|
|
|
|
{
|
2013-05-31 17:21:32 +00:00
|
|
|
this.game.world.setSize(this.currentLayer.widthInPixels, this.currentLayer.heightInPixels, true);
|
2013-04-22 00:53:24 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:37:45 +00:00
|
|
|
private _tempCollisionData;
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Reference to the main game object
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public game: Phaser.Game;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of game object.
|
|
|
|
*/
|
|
|
|
public type: number;
|
|
|
|
|
2013-07-02 22:41:25 +00:00
|
|
|
/**
|
|
|
|
* The name of game object.
|
|
|
|
*/
|
|
|
|
public name: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Group this Sprite belongs to.
|
|
|
|
*/
|
|
|
|
public group: Group;
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Controls if both <code>update</code> and render are called by the core game loop.
|
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public exists: bool;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls if <code>update()</code> is automatically called by the core game loop.
|
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public active: bool;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls if this Sprite is rendered or skipped during the core game loop.
|
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public visible: bool;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
/**
|
2013-07-02 22:41:25 +00:00
|
|
|
* A useful state for many game objects. Kill and revive both flip this switch.
|
2013-05-31 17:21:32 +00:00
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public alive: bool;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-07-02 22:41:25 +00:00
|
|
|
/**
|
|
|
|
* The texture used to render the Sprite.
|
|
|
|
*/
|
2013-08-06 02:14:48 +00:00
|
|
|
public texture: Phaser.Display.Texture;
|
2013-07-02 22:41:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sprite transform component.
|
|
|
|
*/
|
2013-08-06 02:14:48 +00:00
|
|
|
public transform: Phaser.Components.TransformManager;
|
2013-07-02 22:41:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* z order value of the object.
|
|
|
|
*/
|
|
|
|
public z: number = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render iteration counter
|
|
|
|
*/
|
|
|
|
public renderOrderID: number = 0;
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Tilemap data format enum: CSV.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public static FORMAT_CSV: number = 0;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Tilemap data format enum: Tiled JSON.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public static FORMAT_TILED_JSON: number = 1;
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Array contains tile objects of this map.
|
|
|
|
* @type {Tile[]}
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public tiles: Phaser.Tile[];
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Array contains tilemap layer objects of this map.
|
|
|
|
* @type {TilemapLayer[]}
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public layers : Phaser.TilemapLayer[];
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Current tilemap layer.
|
|
|
|
* @type {TilemapLayer}
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public currentLayer: Phaser.TilemapLayer;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* The tilemap layer for collision.
|
|
|
|
* @type {TilemapLayer}
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public collisionLayer: Phaser.TilemapLayer;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Tilemap collision callback.
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public collisionCallback = null;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Context for the collision callback called with.
|
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public collisionCallbackContext;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Format of this tilemap data. Available values: Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public mapFormat: number;
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Parset csv map data and generate tiles.
|
|
|
|
* @param data {string} CSV map data.
|
|
|
|
* @param key {string} Asset key for tileset image.
|
|
|
|
* @param tileWidth {number} Width of its tile.
|
|
|
|
* @param tileHeight {number} Height of its tile.
|
|
|
|
*/
|
2013-04-22 00:53:24 +00:00
|
|
|
private parseCSV(data: string, key: string, tileWidth: number, tileHeight: number) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-08-08 05:29:21 +00:00
|
|
|
var layer: TilemapLayer = new TilemapLayer(this, 0, key, Tilemap.FORMAT_CSV, 'TileLayerCSV' + this.layers.length.toString(), tileWidth, tileHeight);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
// Trim any rogue whitespace from the data
|
|
|
|
data = data.trim();
|
|
|
|
|
|
|
|
var rows = data.split("\n");
|
|
|
|
|
|
|
|
for (var i = 0; i < rows.length; i++)
|
|
|
|
{
|
|
|
|
var column = rows[i].split(",");
|
|
|
|
|
|
|
|
if (column.length > 0)
|
|
|
|
{
|
2013-04-22 00:53:24 +00:00
|
|
|
layer.addColumn(column);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
layer.updateBounds();
|
2013-07-02 22:41:25 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
var tileQuantity = layer.parseTileOffsets();
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
this.currentLayer = layer;
|
2013-04-28 11:25:02 +00:00
|
|
|
this.collisionLayer = layer;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
this.layers.push(layer);
|
|
|
|
|
|
|
|
this.generateTiles(tileQuantity);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
2013-06-01 18:59:20 +00:00
|
|
|
* Parse JSON map data and generate tiles.
|
2013-05-04 11:59:59 +00:00
|
|
|
* @param data {string} JSON map data.
|
|
|
|
* @param key {string} Asset key for tileset image.
|
|
|
|
*/
|
2013-04-22 00:53:24 +00:00
|
|
|
private parseTiledJSON(data: string, key: string) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
// Trim any rogue whitespace from the data
|
|
|
|
data = data.trim();
|
|
|
|
|
|
|
|
var json = JSON.parse(data);
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
for (var i = 0; i < json.layers.length; i++)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-08-08 05:29:21 +00:00
|
|
|
var layer: TilemapLayer = new TilemapLayer(this, i, key, Tilemap.FORMAT_TILED_JSON, json.layers[i].name, json.tilewidth, json.tileheight);
|
2013-05-04 11:59:59 +00:00
|
|
|
|
2013-06-25 14:35:45 +00:00
|
|
|
// Check it's a data layer
|
|
|
|
if (!json.layers[i].data)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
layer.alpha = json.layers[i].opacity;
|
|
|
|
layer.visible = json.layers[i].visible;
|
2013-04-25 00:55:56 +00:00
|
|
|
layer.tileMargin = json.tilesets[0].margin;
|
|
|
|
layer.tileSpacing = json.tilesets[0].spacing;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
var c = 0;
|
|
|
|
var row;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
for (var t = 0; t < json.layers[i].data.length; t++)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-04-22 00:53:24 +00:00
|
|
|
if (c == 0)
|
|
|
|
{
|
|
|
|
row = [];
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
row.push(json.layers[i].data[t]);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
c++;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
if (c == json.layers[i].width)
|
|
|
|
{
|
|
|
|
layer.addColumn(row);
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
layer.updateBounds();
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
var tileQuantity = layer.parseTileOffsets();
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
this.currentLayer = layer;
|
2013-04-28 11:25:02 +00:00
|
|
|
this.collisionLayer = layer;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
this.layers.push(layer);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
this.generateTiles(tileQuantity);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Create tiles of given quantity.
|
|
|
|
* @param qty {number} Quentity of tiles to be generated.
|
|
|
|
*/
|
2013-04-25 00:55:56 +00:00
|
|
|
private generateTiles(qty:number) {
|
|
|
|
|
|
|
|
for (var i = 0; i < qty; i++)
|
|
|
|
{
|
2013-08-08 18:16:47 +00:00
|
|
|
this.tiles.push(new Phaser.Tile(this.game, this, i, this.currentLayer.tileWidth, this.currentLayer.tileHeight));
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
public get widthInPixels(): number {
|
|
|
|
return this.currentLayer.widthInPixels;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
public get heightInPixels(): number {
|
|
|
|
return this.currentLayer.heightInPixels;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
// Tile Collision
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Set callback to be called when this tilemap collides.
|
|
|
|
* @param context {object} Callback will be called with this context.
|
|
|
|
* @param callback {function} Callback function.
|
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public setCollisionCallback(context, callback) {
|
|
|
|
|
|
|
|
this.collisionCallbackContext = context;
|
|
|
|
this.collisionCallback = callback;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Set collision configs of tiles in a range index.
|
|
|
|
* @param start {number} First index of tiles.
|
|
|
|
* @param end {number} Last index of tiles.
|
|
|
|
* @param collision {number} Bit field of flags. (see Tile.allowCollision)
|
2013-08-13 03:22:24 +00:00
|
|
|
* @param resetCollisions {bool} Reset collision flags before set.
|
|
|
|
* @param separateX {bool} Enable seprate at x-axis.
|
|
|
|
* @param separateY {bool} Enable seprate at y-axis.
|
2013-05-04 11:59:59 +00:00
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public setCollisionRange(start: number, end: number, collision:number = Types.ANY, resetCollisions: bool = false, separateX: bool = true, separateY: bool = true) {
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
for (var i = start; i < end; i++)
|
|
|
|
{
|
2013-05-02 02:37:45 +00:00
|
|
|
this.tiles[i].setCollision(collision, resetCollisions, separateX, separateY);
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Set collision configs of tiles with given index.
|
|
|
|
* @param values {number[]} Index array which contains all tile indexes. The tiles with those indexes will be setup with rest parameters.
|
|
|
|
* @param collision {number} Bit field of flags. (see Tile.allowCollision)
|
2013-08-13 03:22:24 +00:00
|
|
|
* @param resetCollisions {bool} Reset collision flags before set.
|
|
|
|
* @param separateX {bool} Enable seprate at x-axis.
|
|
|
|
* @param separateY {bool} Enable seprate at y-axis.
|
2013-05-04 11:59:59 +00:00
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public setCollisionByIndex(values:number[], collision:number = Types.ANY, resetCollisions: bool = false, separateX: bool = true, separateY: bool = true) {
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < values.length; i++)
|
|
|
|
{
|
2013-05-02 02:37:45 +00:00
|
|
|
this.tiles[values[i]].setCollision(collision, resetCollisions, separateX, separateY);
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tile Management
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Get the tile by its index.
|
|
|
|
* @param value {number} Index of the tile you want to get.
|
|
|
|
* @return {Tile} The tile with given index.
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public getTileByIndex(value: number): Phaser.Tile {
|
2013-05-02 02:37:45 +00:00
|
|
|
|
|
|
|
if (this.tiles[value])
|
|
|
|
{
|
|
|
|
return this.tiles[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Get the tile located at specific position and layer.
|
|
|
|
* @param x {number} X position of this tile located.
|
|
|
|
* @param y {number} Y position of this tile located.
|
2013-05-04 16:18:45 +00:00
|
|
|
* @param [layer] {number} layer of this tile located.
|
2013-05-04 11:59:59 +00:00
|
|
|
* @return {Tile} The tile with specific properties.
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public getTile(x: number, y: number, layer: number = this.currentLayer.ID): Phaser.Tile {
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
return this.tiles[this.layers[layer].getTileIndex(x, y)];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Get the tile located at specific position (in world coordinate) and layer. (thus you give a position of a point which is within the tile)
|
|
|
|
* @param x {number} X position of the point in target tile.
|
|
|
|
* @param x {number} Y position of the point in target tile.
|
2013-05-04 16:18:45 +00:00
|
|
|
* @param [layer] {number} layer of this tile located.
|
2013-05-04 11:59:59 +00:00
|
|
|
* @return {Tile} The tile with specific properties.
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public getTileFromWorldXY(x: number, y: number, layer: number = this.currentLayer.ID): Phaser.Tile {
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
return this.tiles[this.layers[layer].getTileFromWorldXY(x, y)];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* Gets the tile underneath the Input.x/y position
|
|
|
|
* @param layer The layer to check, defaults to 0
|
|
|
|
* @returns {Tile}
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public getTileFromInputXY(layer: number = this.currentLayer.ID): Phaser.Tile {
|
2013-04-25 00:55:56 +00:00
|
|
|
|
2013-08-08 02:05:59 +00:00
|
|
|
return this.tiles[this.layers[layer].getTileFromWorldXY(this.game.input.worldX, this.game.input.worldY)];
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Get tiles overlaps the given object.
|
|
|
|
* @param object {GameObject} Tiles you want to get that overlaps this.
|
2013-06-01 18:59:20 +00:00
|
|
|
* @return {array} Array with tiles information. (Each contains x, y and the tile.)
|
2013-05-04 11:59:59 +00:00
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public getTileOverlaps(object: Phaser.Sprite) {
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
return this.currentLayer.getTileOverlaps(object);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// COLLIDE
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Check whether this tilemap collides with the given game object or group of objects.
|
|
|
|
* @param objectOrGroup {function} Target object of group you want to check.
|
|
|
|
* @param callback {function} This is called if objectOrGroup collides the tilemap.
|
|
|
|
* @param context {object} Callback will be called with this context.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool} Return true if this collides with given object, otherwise return false.
|
2013-05-04 11:59:59 +00:00
|
|
|
*/
|
2013-05-02 02:37:45 +00:00
|
|
|
public collide(objectOrGroup = null, callback = null, context = null) {
|
|
|
|
|
|
|
|
if (callback !== null && context !== null)
|
|
|
|
{
|
|
|
|
this.collisionCallback = callback;
|
|
|
|
this.collisionCallbackContext = context;
|
|
|
|
}
|
2013-04-25 00:55:56 +00:00
|
|
|
|
|
|
|
if (objectOrGroup == null)
|
|
|
|
{
|
2013-05-31 17:21:32 +00:00
|
|
|
objectOrGroup = this.game.world.group;
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Group?
|
|
|
|
if (objectOrGroup.isGroup == false)
|
|
|
|
{
|
2013-05-02 02:37:45 +00:00
|
|
|
this.collideGameObject(objectOrGroup);
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-28 11:25:02 +00:00
|
|
|
objectOrGroup.forEachAlive(this, this.collideGameObject, true);
|
2013-04-25 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Check whether this tilemap collides with the given game object.
|
|
|
|
* @param object {GameObject} Target object you want to check.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool} Return true if this collides with given object, otherwise return false.
|
2013-05-04 11:59:59 +00:00
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public collideGameObject(object: Phaser.Sprite): bool {
|
2013-04-28 11:25:02 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
if (object.body.type == Types.BODY_DYNAMIC && object.exists == true && object.body.allowCollisions != Types.NONE)
|
2013-04-28 11:25:02 +00:00
|
|
|
{
|
2013-05-02 02:37:45 +00:00
|
|
|
this._tempCollisionData = this.collisionLayer.getTileOverlaps(object);
|
|
|
|
|
|
|
|
if (this.collisionCallback !== null && this._tempCollisionData.length > 0)
|
|
|
|
{
|
|
|
|
this.collisionCallback.call(this.collisionCallbackContext, object, this._tempCollisionData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-04-28 11:25:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:59:59 +00:00
|
|
|
/**
|
|
|
|
* Set a tile to a specific layer.
|
|
|
|
* @param x {number} X position of this tile.
|
|
|
|
* @param y {number} Y position of this tile.
|
|
|
|
* @param index {number} The index of this tile type in the core map data.
|
2013-05-04 16:18:45 +00:00
|
|
|
* @param [layer] {number} which layer you want to set the tile to.
|
2013-05-04 11:59:59 +00:00
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public putTile(x: number, y: number, index: number, layer: number = this.currentLayer.ID) {
|
2013-05-02 04:01:34 +00:00
|
|
|
|
|
|
|
this.layers[layer].putTile(x, y, index);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-25 09:35:58 +00:00
|
|
|
/**
|
|
|
|
* Can be over-ridden if required
|
|
|
|
*/
|
|
|
|
public preUpdate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be over-ridden if required
|
|
|
|
*/
|
|
|
|
public update() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be over-ridden if required
|
|
|
|
*/
|
|
|
|
public postUpdate() {
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:29:21 +00:00
|
|
|
public destroy() {
|
|
|
|
|
|
|
|
this.texture = null;
|
|
|
|
this.transform = null;
|
|
|
|
|
|
|
|
this.tiles.length = 0;
|
|
|
|
this.layers.length = 0;
|
|
|
|
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|