2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
|
|
|
/// <reference path="GameObject.ts" />
|
2013-04-22 00:53:24 +00:00
|
|
|
/// <reference path="../system/TilemapLayer.ts" />
|
2013-04-25 00:55:56 +00:00
|
|
|
/// <reference path="../system/Tile.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 {
|
|
|
|
|
|
|
|
export class Tilemap extends GameObject {
|
|
|
|
|
2013-04-22 00:53: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
|
|
|
|
|
|
|
super(game);
|
|
|
|
|
|
|
|
this.isGroup = false;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
this._game.world.setSize(this.currentLayer.widthInPixels, this.currentLayer.heightInPixels, true);
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:37:45 +00:00
|
|
|
private _tempCollisionData;
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public static FORMAT_CSV: number = 0;
|
|
|
|
public static FORMAT_TILED_JSON: number = 1;
|
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
public tiles : Tile[];
|
|
|
|
public layers : TilemapLayer[];
|
2013-04-22 00:53:24 +00:00
|
|
|
public currentLayer: TilemapLayer;
|
2013-04-28 11:25:02 +00:00
|
|
|
public collisionLayer: TilemapLayer;
|
2013-05-02 02:37:45 +00:00
|
|
|
public collisionCallback = null;
|
|
|
|
public collisionCallbackContext;
|
2013-04-18 13:16:18 +00:00
|
|
|
public mapFormat: number;
|
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
public update() {
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
public render(camera: Camera, cameraOffsetX: number, cameraOffsetY: number) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-24 01:48:03 +00:00
|
|
|
if (this.cameraBlacklist.indexOf(camera.ID) == -1)
|
2013-04-22 00:53:24 +00:00
|
|
|
{
|
2013-04-24 01:48:03 +00:00
|
|
|
// Loop through the layers
|
2013-04-25 00:55:56 +00:00
|
|
|
for (var i = 0; i < this.layers.length; i++)
|
2013-04-24 01:48:03 +00:00
|
|
|
{
|
2013-04-25 00:55:56 +00:00
|
|
|
this.layers[i].render(camera, cameraOffsetX, cameraOffsetY);
|
2013-04-24 01:48:03 +00:00
|
|
|
}
|
2013-04-22 00:53:24 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
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-04-25 00:55:56 +00:00
|
|
|
var layer: TilemapLayer = new TilemapLayer(this._game, this, 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-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-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-04-25 00:55:56 +00:00
|
|
|
var layer: TilemapLayer = new TilemapLayer(this._game, this, key, Tilemap.FORMAT_TILED_JSON, json.layers[i].name, json.tilewidth, json.tileheight);
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private generateTiles(qty:number) {
|
|
|
|
|
|
|
|
for (var i = 0; i < qty; i++)
|
|
|
|
{
|
|
|
|
this.tiles.push(new Tile(this._game, this, i, this.currentLayer.tileWidth, this.currentLayer.tileHeight));
|
|
|
|
}
|
|
|
|
|
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-02 02:37:45 +00:00
|
|
|
public setCollisionCallback(context, callback) {
|
|
|
|
|
|
|
|
this.collisionCallbackContext = context;
|
|
|
|
this.collisionCallback = callback;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public setCollisionRange(start: number, end: number, collision?:number = Collision.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-02 02:37:45 +00:00
|
|
|
public setCollisionByIndex(values:number[], collision?:number = Collision.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-02 02:37:45 +00:00
|
|
|
public getTileByIndex(value: number):Tile {
|
|
|
|
|
|
|
|
if (this.tiles[value])
|
|
|
|
{
|
|
|
|
return this.tiles[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-25 00:55:56 +00:00
|
|
|
public getTile(x: number, y: number, layer?: number = 0):Tile {
|
|
|
|
|
|
|
|
return this.tiles[this.layers[layer].getTileIndex(x, y)];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTileFromWorldXY(x: number, y: number, layer?: number = 0):Tile {
|
|
|
|
|
|
|
|
return this.tiles[this.layers[layer].getTileFromWorldXY(x, y)];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTileFromInputXY(layer?: number = 0):Tile {
|
|
|
|
|
|
|
|
return this.tiles[this.layers[layer].getTileFromWorldXY(this._game.input.worldX, this._game.input.worldY)];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTileOverlaps(object: GameObject) {
|
|
|
|
|
|
|
|
return this.currentLayer.getTileOverlaps(object);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// COLLIDE
|
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)
|
|
|
|
{
|
|
|
|
objectOrGroup = this._game.world.group;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-28 11:25:02 +00:00
|
|
|
public collideGameObject(object: GameObject): bool {
|
|
|
|
|
2013-05-02 02:37:45 +00:00
|
|
|
if (object !== this && object.immovable == false && object.exists == true && object.allowCollisions != Collision.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-04-22 00:53:24 +00:00
|
|
|
// Set current layer
|
|
|
|
// Set layer order?
|
|
|
|
// Get block of tiles
|
|
|
|
// Swap tiles around
|
|
|
|
// Delete tiles of certain type
|
|
|
|
// Erase tiles
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|