phaser/src/tilemaps/Tilemap.js

2349 lines
99 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2018-02-12 16:01:20 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2018-02-07 17:10:01 +00:00
var Class = require('../utils/Class');
2018-02-07 22:46:07 +00:00
var DegToRad = require('../math/DegToRad');
var DynamicTilemapLayer = require('./dynamiclayer/DynamicTilemapLayer.js');
2018-02-07 17:10:01 +00:00
var Extend = require('../utils/object/Extend');
2018-02-07 22:46:07 +00:00
var Formats = require('./Formats');
var LayerData = require('./mapdata/LayerData');
2018-02-07 22:46:07 +00:00
var Rotate = require('../math/Rotate');
var StaticTilemapLayer = require('./staticlayer/StaticTilemapLayer.js');
var Tile = require('./Tile');
2018-02-07 22:46:07 +00:00
var TilemapComponents = require('./components');
var Tileset = require('./Tileset');
2018-03-20 11:36:35 +00:00
/**
* @callback TilemapFilterCallback
*
* @param {Phaser.GameObjects.GameObject} value - An object found in the filtered area.
* @param {number} index - The index of the object within the array.
* @param {Phaser.GameObjects.GameObject[]} array - An array of all the objects found.
2018-03-20 11:36:35 +00:00
*
* @return {Phaser.GameObjects.GameObject} The object.
2018-03-20 11:36:35 +00:00
*/
/**
* @callback TilemapFindCallback
*
* @param {Phaser.GameObjects.GameObject} value - An object found.
* @param {number} index - The index of the object within the array.
* @param {Phaser.GameObjects.GameObject[]} array - An array of all the objects found.
2018-03-20 11:36:35 +00:00
*
* @return {boolean} `true` if the callback should be invoked, otherwise `false`.
2018-03-20 11:36:35 +00:00
*/
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* A Tilemap is a container for Tilemap data. This isn't a display object, rather, it holds data
* about the map and allows you to add tilesets and tilemap layers to it. A map can have one or
* more tilemap layers (StaticTilemapLayer or DynamicTilemapLayer), which are the display
* objects that actually render tiles.
*
* The Tilemap data be parsed from a Tiled JSON file, a CSV file or a 2D array. Tiled is a free
* software package specifically for creating tile maps, and is available from:
* http://www.mapeditor.org
*
* A Tilemap has handy methods for getting & manipulating the tiles within a layer. You can only
* use the methods that change tiles (e.g. removeTileAt) on a DynamicTilemapLayer.
*
* Note that all Tilemaps use a base tile size to calculate dimensions from, but that a
* StaticTilemapLayer or DynamicTilemapLayer may have its own unique tile size that overrides
* it.
*
* @class Tilemap
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Tilemaps
2018-02-07 15:27:21 +00:00
* @constructor
2018-02-07 22:46:07 +00:00
* @since 3.0.0
2018-02-07 15:27:21 +00:00
*
2018-02-07 22:46:07 +00:00
* @param {Phaser.Scene} scene - The Scene to which this Tilemap belongs.
* @param {Phaser.Tilemaps.MapData} mapData - A MapData instance containing Tilemap data.
2018-02-07 15:27:21 +00:00
*/
var Tilemap = new Class({
initialize:
function Tilemap (scene, mapData)
{
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#scene
* @type {Phaser.Scene}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.scene = scene;
2017-11-30 00:54:29 +00:00
/**
* The base width of a tile in pixels. Note that individual layers may have a different tile
* width.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#tileWidth
* @type {integer}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.tileWidth = mapData.tileWidth;
2017-11-30 00:54:29 +00:00
/**
* The base height of a tile in pixels. Note that individual layers may have a different
* tile height.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#tileHeight
* @type {integer}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.tileHeight = mapData.tileHeight;
2017-11-30 00:54:29 +00:00
/**
* The width of the map (in tiles).
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#width
* @type {number}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.width = mapData.width;
2017-11-30 00:54:29 +00:00
/**
* The height of the map (in tiles).
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#height
* @type {number}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.height = mapData.height;
2017-11-30 00:54:29 +00:00
/**
* The orientation of the map data (as specified in Tiled), usually 'orthogonal'.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#orientation
* @type {string}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.orientation = mapData.orientation;
2017-11-30 00:54:29 +00:00
/**
* The render (draw) order of the map data (as specified in Tiled), usually 'right-down'.
*
* The draw orders are:
*
* right-down
* left-down
* right-up
* left-up
*
* This can be changed via the `setRenderOrder` method.
*
* @name Phaser.Tilemaps.Tilemap#renderOrder
* @type {string}
* @since 3.12.0
*/
this.renderOrder = mapData.renderOrder;
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* The format of the map data.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#format
* @type {number}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.format = mapData.format;
2017-11-30 00:54:29 +00:00
/**
* The version of the map data (as specified in Tiled, usually 1).
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#version
* @type {number}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.version = mapData.version;
2017-11-30 00:54:29 +00:00
/**
* Map specific properties as specified in Tiled.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#properties
* @type {object}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.properties = mapData.properties;
2017-11-30 00:54:29 +00:00
/**
* The width of the map in pixels based on width * tileWidth.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#widthInPixels
* @type {number}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.widthInPixels = mapData.widthInPixels;
2017-11-30 00:54:29 +00:00
/**
* The height of the map in pixels based on height * tileHeight.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#heightInPixels
* @type {number}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.heightInPixels = mapData.heightInPixels;
2017-11-30 00:54:29 +00:00
/**
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#imageCollections
* @type {Phaser.Tilemaps.ImageCollection[]}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
2017-11-24 14:21:09 +00:00
this.imageCollections = mapData.imageCollections;
2017-11-30 00:54:29 +00:00
/**
* An array of Tiled Image Layers.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#images
* @type {array}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
2017-11-24 15:15:02 +00:00
this.images = mapData.images;
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* An array of Tilemap layer data.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#layers
* @type {Phaser.Tilemaps.LayerData[]}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.layers = mapData.layers;
2017-11-30 00:54:29 +00:00
/**
* An array of Tilesets used in the map.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#tilesets
* @type {Phaser.Tilemaps.Tileset[]}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.tilesets = mapData.tilesets;
2017-11-30 00:54:29 +00:00
/**
* An array of ObjectLayer instances parsed from Tiled object layers.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#objects
* @type {Phaser.Tilemaps.ObjectLayer[]}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.objects = mapData.objects;
2017-11-30 00:54:29 +00:00
/**
* The index of the currently selected LayerData object.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#currentLayerIndex
* @type {integer}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.currentLayerIndex = 0;
},
/**
* Sets the rendering (draw) order of the tiles in this map.
*
* The default is 'right-down', meaning it will order the tiles starting from the top-left,
* drawing to the right and then moving down to the next row.
*
* The draw orders are:
*
* 0 = right-down
* 1 = left-down
* 2 = right-up
* 3 = left-up
*
* Setting the render order does not change the tiles or how they are stored in the layer,
* it purely impacts the order in which they are rendered.
*
* You can provide either an integer (0 to 3), or the string version of the order.
*
* Calling this method _after_ creating Static or Dynamic Tilemap Layers will **not** automatically
* update them to use the new render order. If you call this method after creating layers, use their
* own `setRenderOrder` methods to change them as needed.
*
* @method Phaser.Tilemaps.Tilemap#setRenderOrder
* @since 3.12.0
*
* @param {(integer|string)} renderOrder - The render (draw) order value. Either an integer between 0 and 3, or a string: 'right-down', 'left-down', 'right-up' or 'left-up'.
*
* @return {this} This Tilemap object.
*/
setRenderOrder: function (renderOrder)
{
var orders = [ 'right-down', 'left-down', 'right-up', 'left-up' ];
if (typeof renderOrder === 'number')
{
renderOrder = orders[renderOrder];
}
if (orders.indexOf(renderOrder) > -1)
{
this.renderOrder = renderOrder;
}
return this;
},
2017-11-30 00:54:29 +00:00
/**
* Adds an image to the map to be used as a tileset. A single map may use multiple tilesets.
* Note that the tileset name can be found in the JSON file exported from Tiled, or in the Tiled
* editor.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#addTilesetImage
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @param {string} tilesetName - The name of the tileset as specified in the map data.
* @param {string} [key] - The key of the Phaser.Cache image used for this tileset. If
2018-04-03 16:30:15 +00:00
* `undefined` or `null` it will look for an image with a key matching the tilesetName parameter.
2017-11-30 00:54:29 +00:00
* @param {integer} [tileWidth] - The width of the tile (in pixels) in the Tileset Image. If not
* given it will default to the map's tileWidth value, or the tileWidth specified in the Tiled
* JSON file.
* @param {integer} [tileHeight] - The height of the tiles (in pixels) in the Tileset Image. If
* not given it will default to the map's tileHeight value, or the tileHeight specified in the
* Tiled JSON file.
* @param {integer} [tileMargin] - The margin around the tiles in the sheet (in pixels). If not
* specified, it will default to 0 or the value specified in the Tiled JSON file.
* @param {integer} [tileSpacing] - The spacing between each the tile in the sheet (in pixels).
* If not specified, it will default to 0 or the value specified in the Tiled JSON file.
* @param {integer} [gid=0] - If adding multiple tilesets to a blank map, specify the starting
* GID this set will use here.
2018-03-20 11:36:35 +00:00
*
* @return {?Phaser.Tilemaps.Tileset} Returns the Tileset object that was created or updated, or null if it
2017-11-30 00:54:29 +00:00
* failed.
*/
addTilesetImage: function (tilesetName, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid)
{
if (tilesetName === undefined) { return null; }
if (key === undefined || key === null) { key = tilesetName; }
if (!this.scene.sys.textures.exists(key))
{
2018-09-27 10:33:58 +00:00
console.warn('Invalid Tileset Image: ' + key);
return null;
}
var texture = this.scene.sys.textures.get(key);
var index = this.getTilesetIndex(tilesetName);
2018-01-18 00:30:22 +00:00
if (index === null && this.format === Formats.TILED_JSON)
{
2018-09-27 10:33:58 +00:00
console.warn('No data found for Tileset: ' + tilesetName);
return null;
}
2018-09-27 10:33:58 +00:00
var tileset = this.tilesets[index];
if (tileset)
{
2018-09-27 10:33:58 +00:00
tileset.setTileSize(tileWidth, tileHeight);
tileset.setSpacing(tileMargin, tileSpacing);
tileset.setImage(texture);
return tileset;
}
if (tileWidth === undefined) { tileWidth = this.tileWidth; }
if (tileHeight === undefined) { tileHeight = this.tileHeight; }
if (tileMargin === undefined) { tileMargin = 0; }
if (tileSpacing === undefined) { tileSpacing = 0; }
if (gid === undefined) { gid = 0; }
2018-09-27 10:33:58 +00:00
tileset = new Tileset(tilesetName, gid, tileWidth, tileHeight, tileMargin, tileSpacing);
tileset.setImage(texture);
2018-09-27 10:33:58 +00:00
this.tilesets.push(tileset);
2018-02-07 22:46:07 +00:00
return tileset;
},
2017-11-30 00:54:29 +00:00
/**
* Turns the DynamicTilemapLayer associated with the given layer into a StaticTilemapLayer. If
2017-11-30 00:54:29 +00:00
* no layer specified, the map's current layer is used. This is useful if you want to manipulate
* a map at the start of a scene, but then make it non-manipulable and optimize it for speed.
2018-01-26 21:34:03 +00:00
* Note: the DynamicTilemapLayer passed in is destroyed, so make sure to store the value
* returned from this method if you want to manipulate the new StaticTilemapLayer.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#convertLayerToStatic
* @since 3.0.0
*
2018-03-20 15:11:33 +00:00
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer)} [layer] - The name of the layer from Tiled, the
2018-02-07 22:46:07 +00:00
* index of the layer in the map, or a DynamicTilemapLayer.
2018-03-20 11:36:35 +00:00
*
* @return {?Phaser.Tilemaps.StaticTilemapLayer} Returns the new layer that was created, or null if it
2017-11-30 00:54:29 +00:00
* failed.
*/
convertLayerToStatic: function (layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
2017-11-30 00:54:29 +00:00
if (layer === null) { return null; }
var dynamicLayer = layer.tilemapLayer;
if (!dynamicLayer || !(dynamicLayer instanceof DynamicTilemapLayer))
{
return null;
}
2018-02-07 22:46:07 +00:00
var staticLayer = new StaticTilemapLayer(
dynamicLayer.scene,
dynamicLayer.tilemap,
dynamicLayer.layerIndex,
dynamicLayer.tileset,
dynamicLayer.x,
dynamicLayer.y
);
2017-11-30 00:54:29 +00:00
this.scene.sys.displayList.add(staticLayer);
dynamicLayer.destroy();
return staticLayer;
},
/**
* Copies the tiles in the source rectangular area to a new destination (all specified in tile
* coordinates) within the layer. This copies all tile properties & recalculates collision
* information in the destination region.
*
* If no layer specified, the map's current layer is used. This cannot be applied to StaticTilemapLayers.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#copy
* @since 3.0.0
*
* @param {integer} srcTileX - The x coordinate of the area to copy from, in tiles, not pixels.
* @param {integer} srcTileY - The y coordinate of the area to copy from, in tiles, not pixels.
* @param {integer} width - The width of the area to copy, in tiles, not pixels.
* @param {integer} height - The height of the area to copy, in tiles, not pixels.
* @param {integer} destTileX - The x coordinate of the area to copy to, in tiles, not pixels.
* @param {integer} destTileY - The y coordinate of the area to copy to, in tiles, not pixels.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
2017-11-30 00:54:29 +00:00
if (this._isStaticCall(layer, 'copy')) { return this; }
2018-02-07 22:46:07 +00:00
2017-11-30 00:54:29 +00:00
if (layer !== null)
{
2018-02-07 22:46:07 +00:00
TilemapComponents.Copy(
srcTileX, srcTileY,
width, height,
destTileX, destTileY,
recalculateFaces, layer
);
2017-11-30 00:54:29 +00:00
}
2018-02-07 22:46:07 +00:00
2017-11-30 00:54:29 +00:00
return this;
},
/**
* Creates a new and empty DynamicTilemapLayer. The currently selected layer in the map is set to this new layer.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#createBlankDynamicLayer
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @param {string} name - The name of this layer. Must be unique within the map.
* @param {(string|string[]|Phaser.Tilemaps.Tileset|Phaser.Tilemaps.Tileset[])} tileset - The tileset, or an array of tilesets, used to render this layer. Can be a string or a Tileset object.
* @param {number} [x=0] - The world x position where the top left of this layer will be placed.
* @param {number} [y=0] - The world y position where the top left of this layer will be placed.
* @param {integer} [width] - The width of the layer in tiles. If not specified, it will default to the map's width.
* @param {integer} [height] - The height of the layer in tiles. If not specified, it will default to the map's height.
* @param {integer} [tileWidth] - The width of the tiles the layer uses for calculations. If not specified, it will default to the map's tileWidth.
* @param {integer} [tileHeight] - The height of the tiles the layer uses for calculations. If not specified, it will default to the map's tileHeight.
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.DynamicTilemapLayer} Returns the new layer was created, or null if it failed.
2017-11-30 00:54:29 +00:00
*/
createBlankDynamicLayer: function (name, tileset, x, y, width, height, tileWidth, tileHeight)
{
if (tileWidth === undefined) { tileWidth = tileset.tileWidth; }
if (tileHeight === undefined) { tileHeight = tileset.tileHeight; }
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
var index = this.getLayerIndex(name);
if (index !== null)
{
console.warn('Invalid Tilemap Layer ID: ' + name);
return null;
}
var layerData = new LayerData({
name: name,
tileWidth: tileWidth,
tileHeight: tileHeight,
width: width,
height: height
});
var row;
2018-02-07 22:46:07 +00:00
for (var tileY = 0; tileY < height; tileY++)
{
row = [];
2018-02-07 22:46:07 +00:00
for (var tileX = 0; tileX < width; tileX++)
{
2018-02-07 22:46:07 +00:00
row.push(new Tile(layerData, -1, tileX, tileY, tileWidth, tileHeight, this.tileWidth, this.tileHeight));
}
2018-02-07 22:46:07 +00:00
layerData.data.push(row);
}
this.layers.push(layerData);
this.currentLayerIndex = this.layers.length - 1;
var dynamicLayer = new DynamicTilemapLayer(this.scene, this, this.currentLayerIndex, tileset, x, y);
dynamicLayer.setRenderOrder(this.renderOrder);
this.scene.sys.displayList.add(dynamicLayer);
return dynamicLayer;
},
2017-11-30 00:54:29 +00:00
/**
* Creates a new DynamicTilemapLayer that renders the LayerData associated with the given
* `layerID`. The currently selected layer in the map is set to this new layer.
*
* The `layerID` is important. If you've created your map in Tiled then you can get this by
* looking in Tiled and looking at the layer name. Or you can open the JSON file it exports and
* look at the layers[].name value. Either way it must match.
*
* Unlike a static layer, a dynamic layer can be modified. See DynamicTilemapLayer for more
* information.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#createDynamicLayer
* @since 3.0.0
*
* @param {(integer|string)} layerID - The layer array index value, or if a string is given, the layer name from Tiled.
* @param {(string|string[]|Phaser.Tilemaps.Tileset|Phaser.Tilemaps.Tileset[])} tileset - The tileset, or an array of tilesets, used to render this layer. Can be a string or a Tileset object.
* @param {number} x - The x position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.
* @param {number} y - The y position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.
2018-03-20 11:36:35 +00:00
*
* @return {?Phaser.Tilemaps.DynamicTilemapLayer} Returns the new layer was created, or null if it failed.
2017-11-30 00:54:29 +00:00
*/
createDynamicLayer: function (layerID, tileset, x, y)
{
var index = this.getLayerIndex(layerID);
if (index === null)
{
console.warn('Invalid Tilemap Layer ID: ' + layerID);
2017-11-30 00:54:29 +00:00
return null;
}
var layerData = this.layers[index];
// Check for an associated static or dynamic tilemap layer
if (layerData.tilemapLayer)
{
console.warn('Tilemap Layer ID already exists:' + layerID);
2017-11-30 00:54:29 +00:00
return null;
}
this.currentLayerIndex = index;
// Default the x/y position to match Tiled layer offset, if it exists.
2017-11-30 00:54:29 +00:00
if (x === undefined && this.layers[index].x) { x = this.layers[index].x; }
if (y === undefined && this.layers[index].y) { y = this.layers[index].y; }
var layer = new DynamicTilemapLayer(this.scene, this, index, tileset, x, y);
layer.setRenderOrder(this.renderOrder);
2017-11-30 00:54:29 +00:00
this.scene.sys.displayList.add(layer);
return layer;
},
/**
* Creates a Sprite for every object matching the given gid in the map data. All properties from
* the map data objectgroup are copied into the `spriteConfig`, so you can use this as an easy
* way to configure Sprite properties from within the map editor. For example giving an object a
* property of alpha: 0.5 in the map editor will duplicate that when the Sprite is created.
*
* Custom object properties not sharing names with the Sprite's own properties are copied to the
* Sprite's {@link Phaser.GameObjects.Sprite#data data store}.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#createFromObjects
* @since 3.0.0
*
* @param {string} name - The name of the object layer (from Tiled) to create Sprites from.
2018-03-20 15:11:33 +00:00
* @param {(integer|string)} id - Either the id (object), gid (tile object) or name (object or
* tile object) from Tiled. Ids are unique in Tiled, but a gid is shared by all tile objects
* with the same graphic. The same name can be used on multiple objects.
* @param {SpriteConfig} spriteConfig - The config object to pass into the Sprite creator (i.e.
* scene.make.sprite).
2018-02-07 22:46:07 +00:00
* @param {Phaser.Scene} [scene=the scene the map is within] - The Scene to create the Sprites within.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @return {Phaser.GameObjects.Sprite[]} An array of the Sprites that were created.
*/
createFromObjects: function (name, id, spriteConfig, scene)
{
if (spriteConfig === undefined) { spriteConfig = {}; }
if (scene === undefined) { scene = this.scene; }
var objectLayer = this.getObjectLayer(name);
if (!objectLayer)
{
console.warn('Cannot create from object. Invalid objectgroup name given: ' + name);
return;
}
var objects = objectLayer.objects;
var sprites = [];
for (var i = 0; i < objects.length; i++)
{
var found = false;
var obj = objects[i];
if (obj.gid !== undefined && typeof id === 'number' && obj.gid === id ||
obj.id !== undefined && typeof id === 'number' && obj.id === id ||
obj.name !== undefined && typeof id === 'string' && obj.name === id)
{
found = true;
}
if (found)
{
2017-11-30 18:26:07 +00:00
var config = Extend({}, spriteConfig, obj.properties);
2017-11-30 18:26:07 +00:00
config.x = obj.x;
config.y = obj.y;
2017-11-30 18:26:07 +00:00
var sprite = this.scene.make.sprite(config);
sprite.name = obj.name;
if (obj.width) { sprite.displayWidth = obj.width; }
if (obj.height) { sprite.displayHeight = obj.height; }
// Origin is (0, 1) in Tiled, so find the offset that matches the Sprite's origin.
var offset = {
x: sprite.originX * sprite.displayWidth,
y: (sprite.originY - 1) * sprite.displayHeight
};
// If the object is rotated, then the origin offset also needs to be rotated.
if (obj.rotation)
{
var angle = DegToRad(obj.rotation);
Rotate(offset, angle);
sprite.rotation = angle;
}
sprite.x += offset.x;
sprite.y += offset.y;
if (obj.flippedHorizontal !== undefined || obj.flippedVertical !== undefined)
{
sprite.setFlip(obj.flippedHorizontal, obj.flippedVertical);
}
if (!obj.visible) { sprite.visible = false; }
for (var key in obj.properties)
{
if (sprite.hasOwnProperty(key))
{
continue;
}
sprite.setData(key, obj.properties[key]);
}
sprites.push(sprite);
}
}
return sprites;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Creates a Sprite for every object matching the given tile indexes in the layer. You can
* optionally specify if each tile will be replaced with a new tile after the Sprite has been
* created. This is useful if you want to lay down special tiles in a level that are converted to
* Sprites, but want to replace the tile itself with a floor tile or similar once converted.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#createFromTiles
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-03-20 15:11:33 +00:00
* @param {(integer|array)} indexes - The tile index, or array of indexes, to create Sprites from.
* @param {(integer|array)} replacements - The tile index, or array of indexes, to change a converted
2018-02-07 22:46:07 +00:00
* tile to. Set to `null` to leave the tiles unchanged. If an array is given, it is assumed to be a
* one-to-one mapping with the indexes array.
2018-09-28 13:02:12 +00:00
* @param {SpriteConfig} spriteConfig - The config object to pass into the Sprite creator (i.e. scene.make.sprite).
2018-02-07 22:46:07 +00:00
* @param {Phaser.Scene} [scene=scene the map is within] - The Scene to create the Sprites within.
2018-09-28 13:02:12 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.GameObjects.Sprite[]} Returns an array of Tiles, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
createFromTiles: function (indexes, replacements, spriteConfig, scene, camera, layer)
{
2017-11-30 00:54:29 +00:00
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
2017-11-30 00:54:29 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
2017-11-30 00:54:29 +00:00
return TilemapComponents.CreateFromTiles(indexes, replacements, spriteConfig, scene, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
* Creates a new StaticTilemapLayer that renders the LayerData associated with the given
* `layerID`. The currently selected layer in the map is set to this new layer.
*
* The `layerID` is important. If you've created your map in Tiled then you can get this by
* looking in Tiled and looking at the layer name. Or you can open the JSON file it exports and
* look at the layers[].name value. Either way it must match.
*
* It's important to remember that a static layer cannot be modified. See StaticTilemapLayer for
* more information.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#createStaticLayer
* @since 3.0.0
*
* @param {(integer|string)} layerID - The layer array index value, or if a string is given, the layer name from Tiled.
* @param {(string|string[]|Phaser.Tilemaps.Tileset|Phaser.Tilemaps.Tileset[])} tileset - The tileset, or an array of tilesets, used to render this layer. Can be a string or a Tileset object.
2019-01-23 19:30:08 +00:00
* @param {number} [x=0] - The x position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.
* @param {number} [y=0] - The y position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.
2018-03-20 11:36:35 +00:00
*
* @return {?Phaser.Tilemaps.StaticTilemapLayer} Returns the new layer was created, or null if it failed.
2017-11-30 00:54:29 +00:00
*/
createStaticLayer: function (layerID, tileset, x, y)
{
var index = this.getLayerIndex(layerID);
if (index === null)
{
2018-09-27 10:33:58 +00:00
console.warn('Invalid Tilemap Layer ID: ' + layerID);
return null;
}
var layerData = this.layers[index];
2018-09-27 10:33:58 +00:00
// Check for an associated static or dynamic tilemap layer
if (layerData.tilemapLayer)
{
2018-09-27 10:33:58 +00:00
console.warn('Tilemap Layer ID already exists:' + layerID);
return null;
}
this.currentLayerIndex = index;
2018-09-27 10:33:58 +00:00
// Default the x/y position to match Tiled layer offset, if it exists.
if (x === undefined && this.layers[index].x) { x = this.layers[index].x; }
if (y === undefined && this.layers[index].y) { y = this.layers[index].y; }
2017-11-30 00:54:29 +00:00
var layer = new StaticTilemapLayer(this.scene, this, index, tileset, x, y);
layer.setRenderOrder(this.renderOrder);
this.scene.sys.displayList.add(layer);
return layer;
},
2017-11-30 00:54:29 +00:00
/**
* Removes all layer data from this Tilemap and nulls the scene reference. This will destroy any
* StaticTilemapLayers or DynamicTilemapLayers that have been linked to LayerData.
2018-02-07 22:46:07 +00:00
*
* @method Phaser.Tilemaps.Tilemap#destroy
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
destroy: function ()
{
2017-11-30 00:54:29 +00:00
this.removeAllLayers();
this.tilesets.length = 0;
this.objects.length = 0;
this.scene = undefined;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Sets the tiles in the given rectangular area (in tile coordinates) of the layer with the
* specified index. Tiles will be set to collide if the given index is a colliding index.
* Collision information in the region will be recalculated.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the map's current layer is used.
* This cannot be applied to StaticTilemapLayers.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#fill
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} index - The tile index to fill the area with.
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
fill: function (index, tileX, tileY, width, height, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'fill')) { return this; }
2018-02-07 22:46:07 +00:00
if (layer !== null)
{
TilemapComponents.Fill(index, tileX, tileY, width, height, recalculateFaces, layer);
}
2018-02-07 22:46:07 +00:00
return this;
},
/**
* For each object in the given object layer, run the given filter callback function. Any
* objects that pass the filter test (i.e. where the callback returns true) will returned as a
* new array. Similar to Array.prototype.Filter in vanilla JS.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#filterObjects
* @since 3.0.0
*
2018-04-16 15:02:27 +00:00
* @param {(Phaser.Tilemaps.ObjectLayer|string)} objectLayer - The name of an object layer (from Tiled) or an ObjectLayer instance.
* @param {TilemapFilterCallback} callback - The callback. Each object in the given area will be passed to this callback as the first and only parameter.
* @param {object} [context] - The context under which the callback should be run.
2018-03-20 11:36:35 +00:00
*
2018-04-16 15:02:27 +00:00
* @return {?Phaser.GameObjects.GameObject[]} An array of object that match the search, or null if the objectLayer given was invalid.
*/
filterObjects: function (objectLayer, callback, context)
{
if (typeof objectLayer === 'string')
{
var name = objectLayer;
objectLayer = this.getObjectLayer(objectLayer);
if (!objectLayer)
{
console.warn('No object layer found with the name: ' + name);
return null;
}
}
return objectLayer.objects.filter(callback, context);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* For each tile in the given rectangular area (in tile coordinates) of the layer, run the given
* filter callback function. Any tiles that pass the filter test (i.e. where the callback returns
* true) will returned as a new array. Similar to Array.prototype.Filter in vanilla JS.
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#filterTiles
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @param {function} callback - The callback. Each tile in the given area will be passed to this
* callback as the first and only parameter. The callback should return true for tiles that pass the
* filter.
* @param {object} [context] - The context under which the callback should be run.
2018-09-28 13:02:12 +00:00
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area to filter.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area to filter.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile[]} Returns an array of Tiles, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
filterTiles: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.FilterTiles(callback, context, tileX, tileY, width, height, filteringOptions, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Searches the entire map layer for the first tile matching the given index, then returns that Tile
* object. If no match is found, it returns null. The search starts from the top-left tile and
* continues horizontally until it hits the end of the row, then it drops down to the next column.
* If the reverse boolean is true, it scans starting from the bottom-right corner traveling up to
* the top-left.
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#findByIndex
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @param {integer} index - The tile index value to search for.
* @param {integer} [skip=0] - The number of times to skip a matching tile before returning.
2018-09-28 13:02:12 +00:00
* @param {boolean} [reverse=false] - If true it will scan the layer in reverse, starting at the bottom-right. Otherwise it scans from the top-left.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tiles, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
findByIndex: function (findIndex, skip, reverse, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.FindByIndex(findIndex, skip, reverse, layer);
},
/**
* Find the first object in the given object layer that satisfies the provided testing function.
* I.e. finds the first object for which `callback` returns true. Similar to
* Array.prototype.find in vanilla JS.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#findObject
* @since 3.0.0
*
2018-04-16 15:02:27 +00:00
* @param {(Phaser.Tilemaps.ObjectLayer|string)} objectLayer - The name of an object layer (from Tiled) or an ObjectLayer instance.
* @param {TilemapFindCallback} callback - The callback. Each object in the given area will be passed to this callback as the first and only parameter.
* @param {object} [context] - The context under which the callback should be run.
2018-03-20 11:36:35 +00:00
*
2018-04-16 15:02:27 +00:00
* @return {?Phaser.GameObjects.GameObject} An object that matches the search, or null if no object found.
*/
findObject: function (objectLayer, callback, context)
{
if (typeof objectLayer === 'string')
{
var name = objectLayer;
objectLayer = this.getObjectLayer(objectLayer);
if (!objectLayer)
{
console.warn('No object layer found with the name: ' + name);
return null;
}
}
return objectLayer.objects.find(callback, context) || null;
},
/**
2018-02-07 22:46:07 +00:00
* Find the first tile in the given rectangular area (in tile coordinates) of the layer that
* satisfies the provided testing function. I.e. finds the first tile for which `callback` returns
* true. Similar to Array.prototype.find in vanilla JS.
* If no layer specified, the maps current layer is used.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#findTile
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {FindTileCallback} callback - The callback. Each tile in the given area will be passed to this callback as the first and only parameter.
2018-02-07 22:46:07 +00:00
* @param {object} [context] - The context under which the callback should be run.
2018-09-28 13:02:12 +00:00
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The Tile layer to run the search on. If not provided will use the current layer.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tiles, or null if the layer given was invalid.
*/
findTile: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.FindTile(callback, context, tileX, tileY, width, height, filteringOptions, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* For each tile in the given rectangular area (in tile coordinates) of the layer, run the given
* callback. Similar to Array.prototype.forEach in vanilla JS.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the map's current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#forEachTile
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {EachTileCallback} callback - The callback. Each tile in the given area will be passed to this callback as the first and only parameter.
2018-02-07 22:46:07 +00:00
* @param {object} [context] - The context under which the callback should be run.
2018-09-28 13:02:12 +00:00
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The Tile layer to run the search on. If not provided will use the current layer.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
forEachTile: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer !== null)
{
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, filteringOptions, layer);
}
2018-02-07 22:46:07 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
* Gets the image layer index based on its name.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getImageIndex
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @param {string} name - The name of the image to get.
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @return {integer} The index of the image in this tilemap, or null if not found.
*/
2017-11-24 15:15:02 +00:00
getImageIndex: function (name)
{
return this.getIndex(this.images, name);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Internally used. Returns the index of the object in one of the Tilemaps arrays whose name
2017-11-30 00:54:29 +00:00
* property matches the given `name`.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getIndex
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @param {array} location - The Tilemap array to search.
* @param {string} name - The name of the array element to get.
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @return {number} The index of the element in the array, or null if not found.
*/
2017-11-15 20:55:26 +00:00
getIndex: function (location, name)
{
for (var i = 0; i < location.length; i++)
{
if (location[i].name === name)
{
return i;
}
}
2018-02-07 22:46:07 +00:00
2017-11-15 20:55:26 +00:00
return null;
},
2017-11-30 00:54:29 +00:00
/**
* Gets the LayerData from this.layers that is associated with `layer`, or null if an invalid
* `layer` is given.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getLayer
* @since 3.0.0
*
2018-03-20 15:11:33 +00:00
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The name of the
2017-11-30 00:54:29 +00:00
* layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
2018-02-07 22:46:07 +00:00
* StaticTilemapLayer. If not given will default to the maps current layer index.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @return {Phaser.Tilemaps.LayerData} The corresponding LayerData within this.layers.
2017-11-30 00:54:29 +00:00
*/
2017-11-15 20:55:26 +00:00
getLayer: function (layer)
{
var index = this.getLayerIndex(layer);
2018-02-07 22:46:07 +00:00
2017-11-15 20:55:26 +00:00
return index !== null ? this.layers[index] : null;
},
/**
* Gets the ObjectLayer from this.objects that has the given `name`, or null if no ObjectLayer
* is found with that name.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getObjectLayer
* @since 3.0.0
*
* @param {string} [name] - The name of the object layer from Tiled.
2018-03-20 11:36:35 +00:00
*
* @return {?Phaser.Tilemaps.ObjectLayer} The corresponding ObjectLayer within this.objects or null.
*/
getObjectLayer: function (name)
{
var index = this.getIndex(this.objects, name);
2018-02-07 22:46:07 +00:00
return index !== null ? this.objects[index] : null;
},
2017-11-30 00:54:29 +00:00
/**
* Gets the LayerData index of the given `layer` within this.layers, or null if an invalid
* `layer` is given.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getLayerIndex
* @since 3.0.0
*
2018-03-20 15:11:33 +00:00
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The name of the
2017-11-30 00:54:29 +00:00
* layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
* StaticTilemapLayer. If not given will default to the map's current layer index.
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @return {integer} The LayerData index within this.layers.
*/
2017-11-15 20:55:26 +00:00
getLayerIndex: function (layer)
{
if (layer === undefined)
{
return this.currentLayerIndex;
2017-11-15 20:55:26 +00:00
}
else if (typeof layer === 'string')
{
return this.getLayerIndexByName(layer);
}
else if (typeof layer === 'number' && layer < this.layers.length)
{
return layer;
}
else if (layer instanceof StaticTilemapLayer || layer instanceof DynamicTilemapLayer)
{
return layer.layerIndex;
}
else
{
return null;
}
},
2017-11-30 00:54:29 +00:00
/**
* Gets the index of the LayerData within this.layers that has the given `name`, or null if an
* invalid `name` is given.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getLayerIndexByName
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @param {string} name - The name of the layer to get.
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @return {integer} The LayerData index within this.layers.
*/
2017-11-15 20:55:26 +00:00
getLayerIndexByName: function (name)
{
return this.getIndex(this.layers, name);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Gets a tile at the given tile coordinates from the given layer.
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#getTileAt
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @param {integer} tileX - X position to get the tile from (given in tile units, not pixels).
* @param {integer} tileY - Y position to get the tile from (given in tile units, not pixels).
2018-09-28 13:02:12 +00:00
* @param {boolean} [nonNull=false] - If true getTile won't return null for empty tiles, but a Tile object with an index of -1.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tile, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
2017-11-16 19:09:07 +00:00
getTileAt: function (tileX, tileY, nonNull, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
2017-11-16 19:09:07 +00:00
return TilemapComponents.GetTileAt(tileX, tileY, nonNull, layer);
2017-11-15 20:55:26 +00:00
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Gets a tile at the given world coordinates from the given layer.
* If no layer specified, the map's current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getTileAtWorldXY
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @param {number} worldX - X position to get the tile from (given in pixels)
* @param {number} worldY - Y position to get the tile from (given in pixels)
2018-09-28 13:02:12 +00:00
* @param {boolean} [nonNull=false] - If true, function won't return null for empty tiles, but a Tile object with an index of -1.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tile, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
getTileAtWorldXY: function (worldX, worldY, nonNull, camera, layer)
2017-11-16 19:27:52 +00:00
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
2018-04-16 15:02:27 +00:00
if (layer === null)
{
return null;
}
else
{
return TilemapComponents.GetTileAtWorldXY(worldX, worldY, nonNull, camera, layer);
}
2017-11-16 19:27:52 +00:00
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Gets the tiles in the given rectangular area (in tile coordinates) of the layer.
* If no layer specified, the maps current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getTilesWithin
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile[]} Returns an array of Tiles, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
getTilesWithin: function (tileX, tileY, width, height, filteringOptions, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, filteringOptions, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Gets the tiles that overlap with the given shape in the given layer. The shape must be a Circle,
* Line, Rectangle or Triangle. The shape should be in world coordinates.
* If no layer specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#getTilesWithinShape
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-03-20 15:11:33 +00:00
* @param {(Phaser.Geom.Circle|Phaser.Geom.Line|Phaser.Geom.Rectangle|Phaser.Geom.Triangle)} shape - A shape in world (pixel) coordinates
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2018-09-28 13:02:12 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when factoring in which tiles to return.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile[]} Returns an array of Tiles, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
getTilesWithinShape: function (shape, filteringOptions, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.GetTilesWithinShape(shape, filteringOptions, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Gets the tiles in the given rectangular area (in world coordinates) of the layer.
* If no layer specified, the maps current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getTilesWithinWorldXY
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {number} worldX - The world x coordinate for the top-left of the area.
* @param {number} worldY - The world y coordinate for the top-left of the area.
* @param {number} width - The width of the area.
* @param {number} height - The height of the area.
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2018-09-28 13:02:12 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when factoring in which tiles to return.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile[]} Returns an array of Tiles, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
getTilesWithinWorldXY: function (worldX, worldY, width, height, filteringOptions, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, filteringOptions, camera, layer);
},
/**
* Gets the Tileset that has the given `name`, or null if an invalid `name` is given.
*
* @method Phaser.Tilemaps.Tilemap#getTileset
* @since 3.14.0
*
* @param {string} name - The name of the Tileset to get.
*
* @return {?Phaser.Tilemaps.Tileset} The Tileset, or `null` if no matching named tileset was found.
*/
getTileset: function (name)
{
var index = this.getIndex(this.tilesets, name);
return (index !== null) ? this.tilesets[index] : null;
},
2017-11-30 00:54:29 +00:00
/**
* Gets the index of the Tileset within this.tilesets that has the given `name`, or null if an
* invalid `name` is given.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getTilesetIndex
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @param {string} name - The name of the Tileset to get.
2018-03-20 11:36:35 +00:00
*
2017-11-30 00:54:29 +00:00
* @return {integer} The Tileset index within this.tilesets.
*/
2017-11-15 20:55:26 +00:00
getTilesetIndex: function (name)
{
return this.getIndex(this.tilesets, name);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Checks if there is a tile at the given location (in tile coordinates) in the given layer. Returns
* false if there is no tile or if the tile at that location has an index of -1.
*
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#hasTileAt
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?boolean} Returns a boolean, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
hasTileAt: function (tileX, tileY, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.HasTileAt(tileX, tileY, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Checks if there is a tile at the given location (in world coordinates) in the given layer. Returns
* false if there is no tile or if the tile at that location has an index of -1.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#hasTileAtWorldXY
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {number} worldX - The x coordinate, in pixels.
* @param {number} worldY - The y coordinate, in pixels.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when factoring in which tiles to return.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?boolean} Returns a boolean, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
hasTileAtWorldXY: function (worldX, worldY, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
* The LayerData object that is currently selected in the map. You can set this property using
* any type supported by setLayer.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* @name Phaser.Tilemaps.Tilemap#layer
* @type {Phaser.Tilemaps.LayerData}
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
2017-11-15 20:55:26 +00:00
layer: {
get: function ()
{
return this.layers[this.currentLayerIndex];
2017-11-15 20:55:26 +00:00
},
set: function (layer)
{
this.setLayer(layer);
}
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Puts a tile at the given tile coordinates in the specified layer. You can pass in either an index
* or a Tile object. If you pass in a Tile, all attributes will be copied over to the specified
* location. If you pass in an index, only the index at the specified location will be changed.
* Collision information will be recalculated at the specified location.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* This cannot be applied to StaticTilemapLayers.
*
* @method Phaser.Tilemaps.Tilemap#putTileAt
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-03-20 15:11:33 +00:00
* @param {(integer|Phaser.Tilemaps.Tile)} tile - The index of this tile to set or a Tile object.
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tile, or null if the layer given was invalid or the coordinates were out of bounds.
2017-11-30 00:54:29 +00:00
*/
putTileAt: function (tile, tileX, tileY, recalculateFaces, layer)
2017-11-16 19:09:07 +00:00
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'putTileAt')) { return null; }
2018-02-07 22:46:07 +00:00
2017-11-16 19:09:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.PutTileAt(tile, tileX, tileY, recalculateFaces, layer);
2017-11-16 19:09:07 +00:00
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Puts a tile at the given world coordinates (pixels) in the specified layer. You can pass in either
* an index or a Tile object. If you pass in a Tile, all attributes will be copied over to the
* specified location. If you pass in an index, only the index at the specified location will be
* changed. Collision information will be recalculated at the specified location.
*
* If no layer specified, the maps current layer is used. This
2017-11-30 00:54:29 +00:00
* cannot be applied to StaticTilemapLayers.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#putTileAtWorldXY
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-03-20 15:11:33 +00:00
* @param {(integer|Phaser.Tilemaps.Tile)} tile - The index of this tile to set or a Tile object.
2018-09-28 13:02:12 +00:00
* @param {number} worldX - The x coordinate, in pixels.
* @param {number} worldY - The y coordinate, in pixels.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tile, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
putTileAtWorldXY: function (tile, worldX, worldY, recalculateFaces, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'putTileAtWorldXY')) { return null; }
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.PutTileAtWorldXY(tile, worldX, worldY, recalculateFaces, camera, layer);
},
/**
2018-02-07 22:46:07 +00:00
* Puts an array of tiles or a 2D array of tiles at the given tile coordinates in the specified
* layer. The array can be composed of either tile indexes or Tile objects. If you pass in a Tile,
* all attributes will be copied over to the specified location. If you pass in an index, only the
* index at the specified location will be changed. Collision information will be recalculated
* within the region tiles were changed.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#putTilesAt
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {(integer[]|integer[][]|Phaser.Tilemaps.Tile[]|Phaser.Tilemaps.Tile[][])} tile - A row (array) or grid (2D array) of Tiles or tile indexes to place.
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
*/
putTilesAt: function (tilesArray, tileX, tileY, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'putTilesAt')) { return this; }
2018-02-07 22:46:07 +00:00
if (layer !== null)
{
TilemapComponents.PutTilesAt(tilesArray, tileX, tileY, recalculateFaces, layer);
}
2018-02-07 22:46:07 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the
* specified layer. Each tile will recieve a new index. If an array of indexes is passed in, then
* those will be used for randomly assigning new tile indexes. If an array is not provided, the
* indexes found within the region (excluding -1) will be used for randomly assigning new tile
* indexes. This method only modifies tile indexes and does not change collision information.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
*
* @method Phaser.Tilemaps.Tilemap#randomize
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
2018-02-07 22:46:07 +00:00
* @param {integer[]} [indexes] - An array of indexes to randomly draw from during randomization.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
randomize: function (tileX, tileY, width, height, indexes, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'randomize')) { return this; }
2018-02-07 22:46:07 +00:00
if (layer !== null)
{
TilemapComponents.Randomize(tileX, tileY, width, height, indexes, layer);
}
2018-02-07 22:46:07 +00:00
return this;
},
/**
2018-02-07 22:46:07 +00:00
* Calculates interesting faces at the given tile coordinates of the specified layer. Interesting
* faces are used internally for optimizing collisions against tiles. This method is mostly used
* internally to optimize recalculating faces when only one tile has been changed.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#calculateFacesAt
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
*/
calculateFacesAt: function (tileX, tileY, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return this; }
2018-02-07 22:46:07 +00:00
TilemapComponents.CalculateFacesAt(tileX, tileY, layer);
2018-02-07 22:46:07 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Calculates interesting faces within the rectangular area specified (in tile coordinates) of the
* layer. Interesting faces are used internally for optimizing collisions against tiles. This method
* is mostly used internally.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the map's current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#calculateFacesWithin
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
calculateFacesWithin: function (tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return this; }
2018-02-07 22:46:07 +00:00
TilemapComponents.CalculateFacesWithin(tileX, tileY, width, height, layer);
2018-02-07 22:46:07 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
* Removes all layers from this Tilemap and destroys any associated StaticTilemapLayers or
* DynamicTilemapLayers.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#removeAllLayers
* @since 3.0.0
*
* @return {Phaser.Tilemaps.Tilemap} This Tilemap object.
2017-11-30 00:54:29 +00:00
*/
removeAllLayers: function ()
{
2017-11-30 00:54:29 +00:00
// Destroy any StaticTilemapLayers or DynamicTilemapLayers that are stored in LayerData
for (var i = 0; i < this.layers.length; i++)
{
if (this.layers[i].tilemapLayer)
{
this.layers[i].tilemapLayer.destroy();
}
}
2018-02-07 22:46:07 +00:00
this.layers.length = 0;
this.currentLayerIndex = 0;
2018-02-07 22:46:07 +00:00
return this;
},
/**
* Removes the given Tile, or an array of Tiles, from the layer to which they belong,
* and optionally recalculates the collision information.
*
* This cannot be applied to Tiles that belong to Static Tilemap Layers.
*
* @method Phaser.Tilemaps.Tilemap#removeTile
2019-03-07 12:31:06 +00:00
* @since 3.17.0
*
* @param {(Phaser.Tilemaps.Tile|Phaser.Tilemaps.Tile[])} tiles - The Tile to remove, or an array of Tiles.
* @param {integer} [replaceIndex=-1] - After removing the Tile, insert a brand new Tile into its location with the given index. Leave as -1 to just remove the tile.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
*
* @return {Phaser.Tilemaps.Tile[]} Returns an array of Tiles that were removed.
*/
removeTile: function (tiles, replaceIndex, recalculateFaces)
{
if (replaceIndex === undefined) { replaceIndex = -1; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
var removed = [];
if (!Array.isArray(tiles))
{
tiles = [ tiles ];
}
for (var i = 0; i < tiles.length; i++)
{
var tile = tiles[i];
removed.push(this.removeTileAt(tile.x, tile.y, true, recalculateFaces, tile.tilemapLayer));
if (replaceIndex > -1)
{
this.putTileAt(replaceIndex, tile.x, tile.y, recalculateFaces, tile.tilemapLayer);
}
}
return removed;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Removes the tile at the given tile coordinates in the specified layer and updates the layer's
* collision information.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
*
* @method Phaser.Tilemaps.Tilemap#removeTileAt
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {boolean} [replaceWithNull=true] - If true, this will replace the tile at the specified location with null instead of a Tile with an index of -1.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 22:46:07 +00:00
*
* @return {?Phaser.Tilemaps.Tile} Returns the Tile that was removed, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
removeTileAt: function (tileX, tileY, replaceWithNull, recalculateFaces, layer)
2017-11-16 19:09:07 +00:00
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'removeTileAt')) { return null; }
2018-02-07 22:46:07 +00:00
2017-11-16 19:09:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.RemoveTileAt(tileX, tileY, replaceWithNull, recalculateFaces, layer);
2017-11-16 19:09:07 +00:00
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 22:46:07 +00:00
* Removes the tile at the given world coordinates in the specified layer and updates the layer's
* collision information.
2018-03-20 11:36:35 +00:00
*
2018-02-07 22:46:07 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
*
* @method Phaser.Tilemaps.Tilemap#removeTileAtWorldXY
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {number} worldX - The x coordinate, in pixels.
* @param {number} worldY - The y coordinate, in pixels.
* @param {boolean} [replaceWithNull=true] - If true, this will replace the tile at the specified location with null instead of a Tile with an index of -1.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tile} Returns a Tile, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
removeTileAtWorldXY: function (worldX, worldY, replaceWithNull, recalculateFaces, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (this._isStaticCall(layer, 'removeTileAtWorldXY')) { return null; }
2018-02-07 22:46:07 +00:00
if (layer === null) { return null; }
2018-02-07 22:46:07 +00:00
return TilemapComponents.RemoveTileAtWorldXY(worldX, worldY, replaceWithNull, recalculateFaces, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Draws a debug representation of the layer to the given Graphics. This is helpful when you want to
* get a quick idea of which of your tiles are colliding and which have interesting faces. The tiles
* are drawn starting at (0, 0) in the Graphics, allowing you to place the debug representation
* wherever you want on the screen.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#renderDebug
* @since 3.0.0
*
* @param {Phaser.GameObjects.Graphics} graphics - The target Graphics object to draw upon.
2019-02-13 16:06:00 +00:00
* @param {Phaser.Tilemaps.Types.StyleConfig} styleConfig - An object specifying the colors to use for the debug drawing.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
renderDebug: function (graphics, styleConfig, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
TilemapComponents.RenderDebug(graphics, styleConfig, layer);
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Scans the given rectangular area (given in tile coordinates) for tiles with an index matching
* `findIndex` and updates their index to match `newIndex`. This only modifies the index and does
* not change collision information.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
*
* @method Phaser.Tilemaps.Tilemap#replaceByIndex
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} findIndex - The index of the tile to search for.
* @param {integer} newIndex - The index of the tile to replace it with.
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
replaceByIndex: function (findIndex, newIndex, tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (this._isStaticCall(layer, 'replaceByIndex')) { return this; }
2018-02-07 23:08:37 +00:00
if (layer !== null)
{
TilemapComponents.ReplaceByIndex(findIndex, newIndex, tileX, tileY, width, height, layer);
}
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Sets collision on the given tile or tiles within a layer by index. You can pass in either a
* single numeric index or an array of indexes: [2, 3, 15, 20]. The `collides` parameter controls if
* collision will be enabled (true) or disabled (false).
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#setCollision
* @since 3.0.0
*
2018-03-20 15:11:33 +00:00
* @param {(integer|array)} indexes - Either a single tile index, or an array of tile indexes.
2018-09-28 13:02:12 +00:00
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate the tile faces after the update.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
setCollision: function (indexes, collides, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
TilemapComponents.SetCollision(indexes, collides, recalculateFaces, layer);
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Sets collision on a range of tiles in a layer whose index is between the specified `start` and
* `stop` (inclusive). Calling this with a start value of 10 and a stop value of 14 would set
* collision for tiles 10, 11, 12, 13 and 14. The `collides` parameter controls if collision will be
* enabled (true) or disabled (false).
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#setCollisionBetween
* @since 3.0.0
*
* @param {integer} start - The first index of the tile to be set for collision.
* @param {integer} stop - The last index of the tile to be set for collision.
2018-09-28 13:02:12 +00:00
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate the tile faces after the update.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
setCollisionBetween: function (start, stop, collides, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
TilemapComponents.SetCollisionBetween(start, stop, collides, recalculateFaces, layer);
2018-02-07 23:08:37 +00:00
return this;
},
/**
2018-02-07 23:08:37 +00:00
* Sets collision on the tiles within a layer by checking tile properties. If a tile has a property
* that matches the given properties object, its collision flag will be set. The `collides`
* parameter controls if collision will be enabled (true) or disabled (false). Passing in
* `{ collides: true }` would update the collision flag on any tiles with a "collides" property that
* has a value of true. Any tile that doesn't have "collides" set to true will be ignored. You can
* also use an array of values, e.g. `{ types: ["stone", "lava", "sand" ] }`. If a tile has a
* "types" property that matches any of those values, its collision flag will be updated.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#setCollisionByProperty
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {object} properties - An object with tile properties and corresponding values that should be checked.
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate the tile faces after the update.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
*/
setCollisionByProperty: function (properties, collides, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
TilemapComponents.SetCollisionByProperty(properties, collides, recalculateFaces, layer);
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Sets collision on all tiles in the given layer, except for tiles that have an index specified in
* the given array. The `collides` parameter controls if collision will be enabled (true) or
* disabled (false).
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#setCollisionByExclusion
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-02-07 23:08:37 +00:00
* @param {integer[]} indexes - An array of the tile indexes to not be counted for collision.
2018-09-28 13:02:12 +00:00
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate the tile faces after the update.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
setCollisionByExclusion: function (indexes, collides, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
TilemapComponents.SetCollisionByExclusion(indexes, collides, recalculateFaces, layer);
2018-02-07 23:08:37 +00:00
return this;
},
/**
2018-02-07 23:08:37 +00:00
* Sets collision on the tiles within a layer by checking each tile's collision group data
* (typically defined in Tiled within the tileset collision editor). If any objects are found within
* a tile's collision group, the tile's colliding information will be set. The `collides` parameter
* controls if collision will be enabled (true) or disabled (false).
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#setCollisionFromCollisionGroup
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate the tile faces after the update.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
*/
setCollisionFromCollisionGroup: function (collides, recalculateFaces, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
TilemapComponents.SetCollisionFromCollisionGroup(collides, recalculateFaces, layer);
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Sets a global collision callback for the given tile index within the layer. This will affect all
* tiles on this layer that have the same index. If a callback is already set for the tile index it
* will be replaced. Set the callback to null to remove it. If you want to set a callback for a tile
* at a specific location on the map then see setTileLocationCallback.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#setTileIndexCallback
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {(integer|array)} indexes - Either a single tile index, or an array of tile indexes to have a collision callback set for.
2018-02-07 23:08:37 +00:00
* @param {function} callback - The callback that will be invoked when the tile is collided with.
* @param {object} callbackContext - The context under which the callback is called.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
2017-11-29 13:53:04 +00:00
setTileIndexCallback: function (indexes, callback, callbackContext, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
2017-11-29 13:53:04 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
2017-11-29 13:53:04 +00:00
TilemapComponents.SetTileIndexCallback(indexes, callback, callbackContext, layer);
2018-02-07 23:08:37 +00:00
2017-11-29 13:53:04 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Sets a collision callback for the given rectangular area (in tile coordindates) within the layer.
* If a callback is already set for the tile index it will be replaced. Set the callback to null to
* remove it.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#setTileLocationCallback
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} tileY - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} width - How many tiles wide from the `tileX` index the area will be.
* @param {integer} height - How many tiles tall from the `tileY` index the area will be.
2018-02-07 23:08:37 +00:00
* @param {function} callback - The callback that will be invoked when the tile is collided with.
2018-04-16 15:02:27 +00:00
* @param {object} [callbackContext] - The context under which the callback is called.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
2017-11-29 14:20:24 +00:00
setTileLocationCallback: function (tileX, tileY, width, height, callback, callbackContext, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
2017-11-29 14:20:24 +00:00
if (layer === null) { return this; }
2018-02-07 23:08:37 +00:00
2017-11-29 14:20:24 +00:00
TilemapComponents.SetTileLocationCallback(tileX, tileY, width, height, callback, callbackContext, layer);
2018-02-07 23:08:37 +00:00
2017-11-29 14:20:24 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
* Sets the current layer to the LayerData associated with `layer`.
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#setLayer
* @since 3.0.0
*
2018-03-20 15:11:33 +00:00
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The name of the
2017-11-30 00:54:29 +00:00
* layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
* StaticTilemapLayer. If not given will default to the map's current layer index.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* @return {Phaser.Tilemaps.Tilemap} This Tilemap object.
2017-11-30 00:54:29 +00:00
*/
2017-11-15 20:55:26 +00:00
setLayer: function (layer)
{
var index = this.getLayerIndex(layer);
2018-02-07 23:08:37 +00:00
2017-11-15 20:55:26 +00:00
if (index !== null)
{
this.currentLayerIndex = index;
2017-11-15 20:55:26 +00:00
}
2018-02-07 23:08:37 +00:00
2017-11-15 20:55:26 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
* Sets the base tile size for the map. Note: this does not necessarily match the tileWidth and
* tileHeight for all layers. This also updates the base size on all tiles across all layers.
2017-11-30 00:54:29 +00:00
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#setBaseTileSize
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @param {integer} tileWidth - The width of the tiles the map uses for calculations.
* @param {integer} tileHeight - The height of the tiles the map uses for calculations.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* @return {Phaser.Tilemaps.Tilemap} This Tilemap object.
2017-11-30 00:54:29 +00:00
*/
setBaseTileSize: function (tileWidth, tileHeight)
{
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.widthInPixels = this.width * tileWidth;
this.heightInPixels = this.height * tileHeight;
2017-11-30 00:54:29 +00:00
// Update the base tile size on all layers & tiles
for (var i = 0; i < this.layers.length; i++)
{
this.layers[i].baseTileWidth = tileWidth;
this.layers[i].baseTileHeight = tileHeight;
var mapData = this.layers[i].data;
var mapWidth = this.layers[i].width;
var mapHeight = this.layers[i].height;
2018-12-13 11:07:20 +00:00
for (var row = 0; row < mapHeight; row++)
{
2018-12-13 11:07:20 +00:00
for (var col = 0; col < mapWidth; col++)
{
var tile = mapData[row][col];
2018-02-07 23:08:37 +00:00
if (tile !== null)
{
tile.setSize(undefined, undefined, tileWidth, tileHeight);
}
}
}
}
2017-11-30 00:54:29 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
* Sets the tile size for a specific `layer`. Note: this does not necessarily match the map's
* tileWidth and tileHeight for all layers. This will set the tile size for the layer and any
* tiles the layer has.
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#setLayerTileSize
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @param {integer} tileWidth - The width of the tiles (in pixels) in the layer.
* @param {integer} tileHeight - The height of the tiles (in pixels) in the layer.
2018-03-20 15:11:33 +00:00
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The name of the
2017-11-30 00:54:29 +00:00
* layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
* StaticTilemapLayer. If not given will default to the map's current layer index.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* @return {Phaser.Tilemaps.Tilemap} This Tilemap object.
2017-11-30 00:54:29 +00:00
*/
setLayerTileSize: function (tileWidth, tileHeight, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return this; }
layer.tileWidth = tileWidth;
layer.tileHeight = tileHeight;
var mapData = layer.data;
var mapWidth = layer.width;
var mapHeight = layer.height;
2018-12-13 11:07:20 +00:00
for (var row = 0; row < mapHeight; row++)
{
2018-12-13 11:07:20 +00:00
for (var col = 0; col < mapWidth; col++)
{
var tile = mapData[row][col];
2018-02-07 23:08:37 +00:00
2018-12-13 11:07:20 +00:00
if (tile !== null)
{
tile.setSize(tileWidth, tileHeight);
}
}
}
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Shuffles the tiles in a rectangular region (specified in tile coordinates) within the given
* layer. It will only randomize the tiles in that area, so if they're all the same nothing will
* appear to have changed! This method only modifies tile indexes and does not change collision
* information.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
2017-11-30 00:54:29 +00:00
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#shuffle
* @since 3.0.0
*
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
shuffle: function (tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (this._isStaticCall(layer, 'shuffle')) { return this; }
2018-02-07 23:08:37 +00:00
if (layer !== null)
{
TilemapComponents.Shuffle(tileX, tileY, width, height, layer);
}
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Scans the given rectangular area (given in tile coordinates) for tiles with an index matching
* `indexA` and swaps then with `indexB`. This only modifies the index and does not change collision
* information.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
* This cannot be applied to StaticTilemapLayers.
*
* @method Phaser.Tilemaps.Tilemap#swapByIndex
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-02-07 23:08:37 +00:00
* @param {integer} tileA - First tile index.
* @param {integer} tileB - Second tile index.
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:08:37 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
swapByIndex: function (indexA, indexB, tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (this._isStaticCall(layer, 'swapByIndex')) { return this; }
2018-02-07 23:08:37 +00:00
if (layer !== null)
{
TilemapComponents.SwapByIndex(indexA, indexB, tileX, tileY, width, height, layer);
}
2018-02-07 23:08:37 +00:00
return this;
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:08:37 +00:00
* Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the
* layers position, scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#tileToWorldX
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2017-11-30 00:54:29 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?number} Returns a number, or null if the layer given was invalid.
*/
tileToWorldX: function (tileX, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return null; }
2018-02-07 23:08:37 +00:00
return TilemapComponents.TileToWorldX(tileX, camera, layer);
},
/**
2018-02-07 23:08:37 +00:00
* Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
* layers position, scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#tileToWorldY
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer
* to use. If not given the current layer is used.
*
2018-03-20 11:36:35 +00:00
* @return {?number} Returns a number, or null if the layer given was invalid.
*/
tileToWorldY: function (tileX, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:08:37 +00:00
if (layer === null) { return null; }
2018-02-07 23:08:37 +00:00
return TilemapComponents.TileToWorldY(tileX, camera, layer);
},
/**
2018-02-07 23:12:55 +00:00
* Converts from tile XY coordinates (tile units) to world XY coordinates (pixels), factoring in the
* layers position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
2018-02-07 23:12:55 +00:00
* @method Phaser.Tilemaps.Tilemap#tileToWorldXY
2018-02-07 23:08:37 +00:00
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {Phaser.Math.Vector2} [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Math.Vector2} Returns a point, or null if the layer given was invalid.
*/
tileToWorldXY: function (tileX, tileY, point, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:12:55 +00:00
if (layer === null) { return null; }
2018-02-07 23:12:55 +00:00
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, layer);
},
2017-12-03 14:56:12 +00:00
/**
2018-02-07 23:12:55 +00:00
* Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the
* specified layer. Each tile will receive a new index. New indexes are drawn from the given
* weightedIndexes array. An example weighted array:
*
* [
* { index: 6, weight: 4 }, // Probability of index 6 is 4 / 8
* { index: 7, weight: 2 }, // Probability of index 7 would be 2 / 8
* { index: 8, weight: 1.5 }, // Probability of index 8 would be 1.5 / 8
* { index: 26, weight: 0.5 } // Probability of index 27 would be 0.5 / 8
* ]
*
* The probability of any index being choose is (the index's weight) / (sum of all weights). This
* method only modifies tile indexes and does not change collision information.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the map's current layer is used. This
2017-12-03 14:56:12 +00:00
* cannot be applied to StaticTilemapLayers.
*
2018-02-07 23:12:55 +00:00
* @method Phaser.Tilemaps.Tilemap#weightedRandomize
2018-02-07 23:08:37 +00:00
* @since 3.0.0
*
* @param {integer} [tileX=0] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [tileY=0] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {integer} [width=max width based on tileX] - How many tiles wide from the `tileX` index the area will be.
* @param {integer} [height=max height based on tileY] - How many tiles tall from the `tileY` index the area will be.
2018-02-07 23:12:55 +00:00
* @param {object[]} [weightedIndexes] - An array of objects to randomly draw from during
* randomization. They should be in the form: { index: 0, weight: 4 } or
* { index: [0, 1], weight: 4 } if you wish to draw from multiple tile indexes.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.Tilemap} Return this Tilemap object, or null if the layer given was invalid.
2017-12-03 14:56:12 +00:00
*/
weightedRandomize: function (tileX, tileY, width, height, weightedIndexes, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:12:55 +00:00
2017-12-03 21:31:05 +00:00
if (this._isStaticCall(layer, 'weightedRandomize')) { return this; }
2018-02-07 23:12:55 +00:00
2017-12-03 14:56:12 +00:00
if (layer !== null)
{
TilemapComponents.WeightedRandomize(tileX, tileY, width, height, weightedIndexes, layer);
}
2018-02-07 23:12:55 +00:00
2017-12-03 14:56:12 +00:00
return this;
},
/**
2018-02-07 23:12:55 +00:00
* Converts from world X coordinates (pixels) to tile X coordinates (tile units), factoring in the
* layers position, scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
2018-02-07 23:12:55 +00:00
* @method Phaser.Tilemaps.Tilemap#worldToTileX
2018-02-07 23:08:37 +00:00
* @since 3.0.0
*
2018-09-28 13:02:12 +00:00
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer
* to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?number} Returns a number, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
worldToTileX: function (worldX, snapToFloor, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:12:55 +00:00
if (layer === null) { return null; }
2018-02-07 23:12:55 +00:00
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:12:55 +00:00
* Converts from world Y coordinates (pixels) to tile Y coordinates (tile units), factoring in the
* layers position, scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
2018-02-07 23:12:55 +00:00
* @method Phaser.Tilemaps.Tilemap#worldToTileY
2018-02-07 23:08:37 +00:00
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?number} Returns a number, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
worldToTileY: function (worldY, snapToFloor, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:12:55 +00:00
if (layer === null) { return null; }
2018-02-07 23:12:55 +00:00
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
2018-02-07 23:12:55 +00:00
* Converts from world XY coordinates (pixels) to tile XY coordinates (tile units), factoring in the
* layers position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:08:37 +00:00
* If no layer specified, the maps current layer is used.
*
2018-02-07 23:12:55 +00:00
* @method Phaser.Tilemaps.Tilemap#worldToTileXY
2018-02-07 23:08:37 +00:00
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2018-09-28 13:02:12 +00:00
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
* @param {Phaser.Math.Vector2} [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
2018-03-20 11:36:35 +00:00
* @return {?Phaser.Math.Vector2} Returns a point, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
worldToTileXY: function (worldX, worldY, snapToFloor, point, camera, layer)
{
layer = this.getLayer(layer);
2018-02-07 23:12:55 +00:00
if (layer === null) { return null; }
2018-02-07 23:12:55 +00:00
return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
* Used internally to check if a layer is static and prints out a warning.
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#_isStaticCall
2017-11-30 00:54:29 +00:00
* @private
2018-02-07 23:08:37 +00:00
* @since 3.0.0
*
2017-11-30 00:54:29 +00:00
* @return {boolean}
*/
_isStaticCall: function (layer, functionName)
{
if (layer.tilemapLayer instanceof StaticTilemapLayer)
{
console.warn(functionName + ': You cannot change the tiles in a static tilemap layer');
return true;
}
else
{
return false;
}
}
2017-11-15 20:55:26 +00:00
});
module.exports = Tilemap;