phaser/src/tilemaps/Tilemap.js

2607 lines
103 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
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 Formats = require('./Formats');
var GetFastValue = require('../utils/object/GetFastValue');
var LayerData = require('./mapdata/LayerData');
var ORIENTATION = require('./const/ORIENTATION_CONST');
2018-02-07 22:46:07 +00:00
var Rotate = require('../math/Rotate');
var SpliceOne = require('../utils/array/SpliceOne');
var Sprite = require('../gameobjects/sprite/Sprite');
var Tile = require('./Tile');
2018-02-07 22:46:07 +00:00
var TilemapComponents = require('./components');
var TilemapLayer = require('./TilemapLayer');
2018-02-07 22:46:07 +00:00
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, which are the display objects that actually render the tiles.
2018-02-07 15:27:21 +00:00
*
* The Tilemap data can be parsed from a Tiled JSON file, a CSV file or a 2D array. Tiled is a free
2018-02-07 15:27:21 +00:00
* software package specifically for creating tile maps, and is available from:
* http://www.mapeditor.org
*
* As of Phaser 3.50.0 the Tilemap API now supports the following types of map:
*
* 1) Orthogonal
* 2) Isometric
* 3) Hexagonal
* 4) Staggered
*
* Prior to this release, only orthogonal maps were supported.
*
* Another large change in 3.50 was the consolidation of Tilemap Layers. Previously, you created
* either a Static or Dynamic Tilemap Layer. However, as of 3.50 the features of both have been
* merged and the API simplified, so now there is just the single `TilemapLayer` class.
*
* A Tilemap has handy methods for getting and manipulating the tiles within a layer, allowing
* you to build or modify the tilemap data at runtime.
2018-02-07 15:27:21 +00:00
*
* Note that all Tilemaps use a base tile size to calculate dimensions from, but that a
* TilemapLayer may have its own unique tile size that overrides this.
2018-02-07 15:27:21 +00:00
*
* As of Phaser 3.21.0, if your tilemap includes layer groups (a feature of Tiled 1.2.0+) these
* will be traversed and the following properties will impact children:
*
* - Opacity (blended with parent) and visibility (parent overrides child)
* - Vertical and horizontal offset
*
* The grouping hierarchy is not preserved and all layers will be flattened into a single array.
*
* Group layers are parsed during Tilemap construction but are discarded after parsing so dynamic
* layers will NOT continue to be affected by a parent.
*
* To avoid duplicate layer names, a layer that is a child of a group layer will have its parent
* group name prepended with a '/'. For example, consider a group called 'ParentGroup' with a
* child called 'Layer 1'. In the Tilemap object, 'Layer 1' will have the name
* 'ParentGroup/Layer 1'.
*
2018-02-07 15:27:21 +00:00
* @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
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 22:46:07 +00:00
* @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
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 22:46:07 +00:00
* @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
/**
* A collection of Images, as parsed from Tiled map data.
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
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 22:46:07 +00:00
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
this.currentLayerIndex = 0;
2020-09-19 08:56:05 +00:00
/**
* The length of the horizontal sides of the hexagon.
2020-10-02 10:57:37 +00:00
* Only used for hexagonal orientation Tilemaps.
*
* @name Phaser.Tilemaps.Tilemap#hexSideLength
2020-11-23 10:22:13 +00:00
* @type {number}
* @since 3.50.0
2020-09-19 08:56:05 +00:00
*/
this.hexSideLength = mapData.hexSideLength;
var orientation = this.orientation;
2020-09-19 08:56:05 +00:00
/**
* Functions used to handle world to tile, and tile to world, conversion.
* Cached here for internal use by public methods such as `worldToTileXY`, etc.
*
* @name Phaser.Tilemaps.Tilemap#_convert
* @private
* @type {object}
* @since 3.50.0
2020-09-19 08:56:05 +00:00
*/
this._convert = {
WorldToTileXY: TilemapComponents.GetWorldToTileXYFunction(orientation),
WorldToTileX: TilemapComponents.GetWorldToTileXFunction(orientation),
WorldToTileY: TilemapComponents.GetWorldToTileYFunction(orientation),
TileToWorldXY: TilemapComponents.GetTileToWorldXYFunction(orientation),
TileToWorldX: TilemapComponents.GetTileToWorldXFunction(orientation),
TileToWorldY: TilemapComponents.GetTileToWorldYFunction(orientation)
};
},
/**
* @ignore
*/
2020-10-12 10:50:13 +00:00
createBlankDynamicLayer: function (name, tileset, x, y, width, height, tileWidth, tileHeight)
{
console.warn('createBlankDynamicLayer is deprecated. Use createBlankLayer');
2020-10-12 10:50:13 +00:00
return this.createBlankLayer(name, tileset, x, y, width, height, tileWidth, tileHeight);
},
/**
* @ignore
*/
2020-10-12 10:50:13 +00:00
createDynamicLayer: function (layerID, tileset, x, y)
{
console.warn('createDynamicLayer is deprecated. Use createLayer');
2020-10-12 10:50:13 +00:00
return this.createLayer(layerID, tileset, x, y);
},
/**
* @ignore
*/
2020-10-12 10:50:13 +00:00
createStaticLayer: function (layerID, tileset, x, y)
{
console.warn('createStaticLayer is deprecated. Use createLayer');
2020-10-12 10:50:13 +00:00
return this.createLayer(layerID, tileset, x, y);
},
/**
* 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 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
*
2020-11-23 10:32:00 +00:00
* @param {(number|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.
2020-11-23 10:22:13 +00:00
* @param {number} [tileWidth] - The width of the tile (in pixels) in the Tileset Image. If not
2017-11-30 00:54:29 +00:00
* given it will default to the map's tileWidth value, or the tileWidth specified in the Tiled
* JSON file.
2020-11-23 10:22:13 +00:00
* @param {number} [tileHeight] - The height of the tiles (in pixels) in the Tileset Image. If
2017-11-30 00:54:29 +00:00
* not given it will default to the map's tileHeight value, or the tileHeight specified in the
* Tiled JSON file.
2020-11-23 10:22:13 +00:00
* @param {number} [tileMargin] - The margin around the tiles in the sheet (in pixels). If not
2017-11-30 00:54:29 +00:00
* specified, it will default to 0 or the value specified in the Tiled JSON file.
2020-11-23 10:22:13 +00:00
* @param {number} [tileSpacing] - The spacing between each the tile in the sheet (in pixels).
2017-11-30 00:54:29 +00:00
* If not specified, it will default to 0 or the value specified in the Tiled JSON file.
2020-11-23 10:22:13 +00:00
* @param {number} [gid=0] - If adding multiple tilesets to a blank map, specify the starting
2017-11-30 00:54:29 +00:00
* 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
/**
* 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
*
2020-11-23 10:22:13 +00:00
* @param {number} srcTileX - The x coordinate of the area to copy from, in tiles, not pixels.
* @param {number} srcTileY - The y coordinate of the area to copy from, in tiles, not pixels.
* @param {number} width - The width of the area to copy, in tiles, not pixels.
* @param {number} height - The height of the area to copy, in tiles, not pixels.
* @param {number} destTileX - The x coordinate of the area to copy to, in tiles, not pixels.
* @param {number} 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.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 (layer !== null)
{
2018-02-07 22:46:07 +00:00
TilemapComponents.Copy(
srcTileX, srcTileY,
width, height,
destTileX, destTileY,
recalculateFaces, layer
);
return this;
}
else
{
return null;
}
2017-11-30 00:54:29 +00:00
},
/**
* Creates a new and empty Tilemap Layer. The currently selected layer in the map is set to this new layer.
*
* Prior to v3.50.0 this method was called `createBlankDynamicLayer`.
2017-11-30 00:54:29 +00:00
*
* @method Phaser.Tilemaps.Tilemap#createBlankLayer
2018-02-07 22:46:07 +00:00
* @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.
2020-11-23 10:22:13 +00:00
* @param {number} [width] - The width of the layer in tiles. If not specified, it will default to the map's width.
* @param {number} [height] - The height of the layer in tiles. If not specified, it will default to the map's height.
* @param {number} [tileWidth] - The width of the tiles the layer uses for calculations. If not specified, it will default to the map's tileWidth.
* @param {number} [tileHeight] - The height of the tiles the layer uses for calculations. If not specified, it will default to the map's tileHeight.
*
* @return {?Phaser.Tilemaps.TilemapLayer} Returns the new layer that was created, or `null` if it failed.
2017-11-30 00:54:29 +00:00
*/
createBlankLayer: function (name, tileset, x, y, width, height, tileWidth, tileHeight)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
if (tileWidth === undefined) { tileWidth = this.tileWidth; }
if (tileHeight === undefined) { tileHeight = this.tileHeight; }
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,
2020-09-19 08:56:05 +00:00
height: height,
orientation: this.orientation
});
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 layer = new TilemapLayer(this.scene, this, this.currentLayerIndex, tileset, x, y);
layer.setRenderOrder(this.renderOrder);
this.scene.sys.displayList.add(layer);
return layer;
},
2017-11-30 00:54:29 +00:00
/**
* Creates a new Tilemap Layer that renders the LayerData associated with the given
2017-11-30 00:54:29 +00:00
* `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.
*
* Prior to v3.50.0 this method was called `createDynamicLayer`.
2017-11-30 00:54:29 +00:00
*
* @method Phaser.Tilemaps.Tilemap#createLayer
2018-02-07 22:46:07 +00:00
* @since 3.0.0
*
2020-11-23 10:32:00 +00:00
* @param {(number|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=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.TilemapLayer} Returns the new layer was created, or null if it failed.
2017-11-30 00:54:29 +00:00
*/
createLayer: function (layerID, tileset, x, y)
2017-11-30 00:54:29 +00:00
{
var index = this.getLayerIndex(layerID);
if (index === null)
{
console.warn('Invalid Tilemap Layer ID: ' + layerID);
2019-11-18 16:08:36 +00:00
if (typeof layerID === 'string')
{
console.warn('Valid tilelayer names:\n\t' + this.getTileLayerNames().join(',\n\t'));
}
2019-11-18 16:08:36 +00:00
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.
if (x === undefined)
2017-11-30 00:54:29 +00:00
{
x = layerData.x;
2017-11-30 00:54:29 +00:00
}
if (y === undefined)
{
y = layerData.y;
}
2017-11-30 00:54:29 +00:00
var layer = new TilemapLayer(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;
},
/**
* This method will iterate through all of the objects defined in a Tiled Object Layer and then
* convert the matching results into Phaser Game Objects (by default, Sprites)
*
* Objects are matched on one of 3 criteria: The Object ID, the Object GID or the Object Name.
*
* Within Tiled, Object IDs are unique per Object. Object GIDs, however, are shared by all objects
* using the same image. Finally, Object Names are strings and the same name can be used on multiple
* Objects in Tiled, they do not have to be unique.
*
* You set the configuration parameter accordingly, based on which type of criteria you wish
* to match against. For example, to convert all items on an Object Layer with a `gid` of 26:
*
* ```javascript
* createFromObjects(layerName, {
* gid: 26
* });
* ```
*
* Or, to convert objects with the name 'bonus':
*
* ```javascript
* createFromObjects(layerName, {
* name: 'bonus'
* });
* ```
*
* Or, to convert an object with a specific id:
*
* ```javascript
* createFromObjects(layerName, {
* id: 9
* });
* ```
*
* You should only specify either `id`, `gid`, `name`, or none of them. Do not add more than
* one criteria to your config. If you do not specify any criteria, then _all_ objects in the
* Object Layer will be converted.
*
* By default this method will convert objects into `Sprite` instances, but you can override
* this by providing your own class type:
*
* ```javascript
* createFromObjects(layerName, {
* gid: 26,
* classType: Coin
* });
* ```
*
* This will convert all Objects with a gid of 26 into your custom `Coin` class. You can pass
* any class type here, but it _must_ extend `Phaser.GameObjects.GameObject` as its base class.
* Your class will always be passed 1 parameter: `scene`, which is a reference to either the Scene
* specified in the config object or, if not given, the Scene to which this Tilemap belongs.
*
* All properties from object are copied into the Game Object, so you can use this as an easy
* way to configure properties from within the map editor. For example giving an object a
* property of `alpha: 0.5` in Tiled will be reflected in the Game Object that is created.
*
* Custom object properties that do not exist as a Game Object property are set in the
* Game Objects {@link Phaser.GameObjects.GameObject#data data store}.
*
* You can use set a `container` property in the config. If given, the class will be added to
* the Container instance instead of the Scene.
*
* Finally, you can provide an array of config objects, to convert multiple types of object in
* a single call:
*
* ```javascript
* createFromObjects(layerName, [
* {
* gid: 26,
* classType: Coin
* },
* {
* id: 9,
* classType: BossMonster
* },
* {
* name: 'lava',
* classType: LavaTile
* }
* ]);
* ```
*
* The signature of this method changed significantly in v3.50.0. Prior to this, it did not take config objects.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#createFromObjects
* @since 3.0.0
*
* @param {string} objectLayerName - The name of the Tiled object layer to create the Game Objects from.
* @param {Phaser.Types.Tilemaps.CreateFromObjectLayerConfig|Phaser.Types.Tilemaps.CreateFromObjectLayerConfig[]} config - A CreateFromObjects configuration object, or an array of them.
2018-03-20 11:36:35 +00:00
*
* @return {Phaser.GameObjects.GameObject[]} An array containing the Game Objects that were created. Empty if invalid object layer, or no matching id/gid/name was found.
*/
createFromObjects: function (objectLayerName, config)
{
var results = [];
var objectLayer = this.getObjectLayer(objectLayerName);
if (!objectLayer)
{
console.warn('createFromObjects: Invalid objectLayerName given: ' + objectLayerName);
2019-11-18 16:08:36 +00:00
return results;
}
2019-11-18 16:08:36 +00:00
if (!Array.isArray(config))
{
config = [ config ];
}
var objects = objectLayer.objects;
for (var c = 0; c < config.length; c++)
{
var singleConfig = config[c];
var id = GetFastValue(singleConfig, 'id', null);
var gid = GetFastValue(singleConfig, 'gid', null);
var name = GetFastValue(singleConfig, 'name', null);
var obj;
var toConvert = [];
// Sweep to get all the objects we want to convert in this pass
for (var s = 0; s < objects.length; s++)
{
obj = objects[s];
if (
(id === null && gid === null && name === null) ||
(id !== null && obj.id === id) ||
(gid !== null && obj.gid === gid) ||
(name !== null && obj.name === name)
)
{
toConvert.push(obj);
}
}
// Now let's convert them ...
var classType = GetFastValue(singleConfig, 'classType', Sprite);
var scene = GetFastValue(singleConfig, 'scene', this.scene);
var container = GetFastValue(singleConfig, 'container', null);
var texture = GetFastValue(singleConfig, 'key', null);
var frame = GetFastValue(singleConfig, 'frame', null);
for (var i = 0; i < toConvert.length; i++)
{
obj = toConvert[i];
var sprite = new classType(scene);
sprite.setName(obj.name);
sprite.setPosition(obj.x, obj.y);
sprite.setTexture(texture, frame);
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 Sprites origin.
// Do not offset objects with zero dimensions (e.g. points).
var offset = {
x: sprite.originX * obj.width,
y: (sprite.originY - 1) * obj.height
};
// 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;
}
// Set properties the class may have, or setData those it doesn't
for (var key in obj.properties)
{
if (sprite[key] !== undefined)
{
sprite[key] = obj.properties[key];
}
else
{
sprite.setData(key, obj.properties[key]);
}
}
if (container)
{
container.add(sprite);
}
else
{
scene.add.existing(sprite);
}
results.push(sprite);
}
}
return results;
},
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
*
2020-11-23 10:32:00 +00:00
* @param {(number|array)} indexes - The tile index, or array of indexes, to create Sprites from.
* @param {(number|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.
2019-05-09 14:32:53 +00:00
* @param {Phaser.Types.GameObjects.Sprite.SpriteConfig} spriteConfig - The config object to pass into the Sprite creator (i.e. scene.make.sprite).
2020-11-26 12:55:40 +00:00
* @param {Phaser.Scene} [scene] - The Scene to create the Sprites within.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
/**
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
*
2020-11-23 10:22:13 +00:00
* @param {number} index - The tile index to fill the area with.
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2018-09-28 13:02:12 +00:00
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (recalculateFaces === undefined) { recalculateFaces = true; }
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return 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
*
2020-09-23 11:59:03 +00:00
* @return {?Phaser.Types.Tilemaps.TiledObject[]} 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.
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area to filter.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area to filter.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2019-05-09 11:39:19 +00:00
* @param {Phaser.Types.Tilemaps.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
*
2020-11-23 10:22:13 +00:00
* @param {number} index - The tile index value to search for.
* @param {number} [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.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
*
2020-09-23 11:59:03 +00:00
* @return {?Phaser.Types.Tilemaps.TiledObject} 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.
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2019-05-09 11:39:19 +00:00
* @param {Phaser.Types.Tilemaps.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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.
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area to search.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2019-05-09 11:39:19 +00:00
* @param {Phaser.Types.Tilemaps.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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) { return 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
*
2020-11-23 10:22:13 +00:00
* @return {number} The index of the image in this tilemap, or null if not found.
2017-11-30 00:54:29 +00:00
*/
2017-11-24 15:15:02 +00:00
getImageIndex: function (name)
{
return this.getIndex(this.images, name);
},
/**
* Return a list of all valid imagelayer names loaded in this Tilemap.
*
* @method Phaser.Tilemaps.Tilemap#getImageLayerNames
* @since 3.21.0
*
2019-11-18 16:08:36 +00:00
* @return {string[]} Array of valid imagelayer names / IDs loaded into this Tilemap.
*/
getImageLayerNames: function ()
{
2019-11-18 16:08:36 +00:00
if (!this.images || !Array.isArray(this.images))
{
return [];
}
return this.images.map(function (image)
{
return image.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 the given `layer`, or null if the layer is invalid.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#getLayer
* @since 3.0.0
*
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The name of the layer from Tiled, the index of the layer in the map or Tilemap Layer. 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
return (index !== null) ? this.layers[index] : null;
2017-11-15 20:55:26 +00:00
},
/**
* 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;
},
/**
* Return a list of all valid objectgroup names loaded in this Tilemap.
*
* @method Phaser.Tilemaps.Tilemap#getObjectLayerNames
* @since 3.21.0
*
2019-11-18 16:08:36 +00:00
* @return {string[]} Array of valid objectgroup names / IDs loaded into this Tilemap.
*/
getObjectLayerNames: function ()
{
2019-11-18 16:08:36 +00:00
if (!this.objects || !Array.isArray(this.objects))
{
return [];
}
return this.objects.map(function (object)
{
return object.name;
});
},
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
*
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The name of the layer from Tiled, the index of the layer in the map or a Tilemap Layer. If not given will default to the map's current layer index.
2018-03-20 11:36:35 +00:00
*
2020-11-23 10:22:13 +00:00
* @return {number} The LayerData index within this.layers.
2017-11-30 00:54:29 +00:00
*/
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 TilemapLayer)
2017-11-15 20:55:26 +00:00
{
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
*
2020-11-23 10:22:13 +00:00
* @return {number} The LayerData index within this.layers.
2017-11-30 00:54:29 +00:00
*/
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 is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @method Phaser.Tilemaps.Tilemap#getTileAt
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - X position to get the tile from (given in tile units, not pixels).
* @param {number} tileY - Y position to get the tile from (given in tile units, not pixels).
2020-11-26 12:55:40 +00:00
* @param {boolean} [nonNull] - If true getTile won't return null for empty tiles, but a Tile object with an index of -1.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 is 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#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)
2020-11-26 12:55:40 +00:00
* @param {boolean} [nonNull] - 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] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
if (layer === null) { return null; }
2019-06-29 19:13:15 +00:00
return TilemapComponents.GetTileAtWorldXY(worldX, worldY, nonNull, camera, layer);
2017-11-16 19:27:52 +00:00
},
/**
* Return a list of all valid tilelayer names loaded in this Tilemap.
*
* @method Phaser.Tilemaps.Tilemap#getTileLayerNames
* @since 3.21.0
*
2019-11-22 17:46:26 +00:00
* @return {string[]} Array of valid tilelayer names / IDs loaded into this Tilemap.
*/
getTileLayerNames: function ()
{
2019-11-18 16:08:36 +00:00
if (!this.layers || !Array.isArray(this.layers))
{
return [];
}
return this.layers.map(function (layer)
{
return layer.name;
});
},
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 is 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
*
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2019-05-09 11:39:19 +00:00
* @param {Phaser.Types.Tilemaps.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @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-05-09 11:39:19 +00:00
* @param {Phaser.Types.Tilemaps.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when factoring in which tiles to return.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 is 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-05-09 11:39:19 +00:00
* @param {Phaser.Types.Tilemaps.FilteringOptions} [filteringOptions] - Optional filters to apply when getting the tiles.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when factoring in which tiles to return.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
*
2020-11-23 10:22:13 +00:00
* @return {number} The Tileset index within this.tilesets.
2017-11-30 00:54:29 +00:00
*/
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 is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @method Phaser.Tilemaps.Tilemap#hasTileAt
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
* @param {number} tileY - The y coordinate, in tiles, not pixels.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @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.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when factoring in which tiles to return.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @method Phaser.Tilemaps.Tilemap#putTileAt
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2020-11-23 10:32:00 +00:00
* @param {(number|Phaser.Tilemaps.Tile)} tile - The index of this tile to set or a Tile object.
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
* @param {number} tileY - The y coordinate, in tiles, not pixels.
2020-11-26 12:55:40 +00:00
* @param {boolean} [recalculateFaces] - `true` if the faces data should be recalculated.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
{
2020-11-26 12:55:40 +00:00
if (recalculateFaces === undefined) { recalculateFaces = true; }
2017-11-16 19:09:07 +00:00
layer = this.getLayer(layer);
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 is 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#putTileAtWorldXY
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-11-23 10:32:00 +00:00
* @param {(number|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.
2020-11-26 12:55:40 +00:00
* @param {boolean} [recalculateFaces] - `true` if the faces data should be recalculated.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (recalculateFaces === undefined) { recalculateFaces = true; }
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.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
*
* If no layer is specified, the maps current layer is used.
*
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
*
2020-11-23 10:32:00 +00:00
* @param {(number[]|number[][]|Phaser.Tilemaps.Tile[]|Phaser.Tilemaps.Tile[][])} tile - A row (array) or grid (2D array) of Tiles or tile indexes to place.
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
* @param {number} tileY - The y coordinate, in tiles, not pixels.
2020-11-26 12:55:40 +00:00
* @param {boolean} [recalculateFaces] - `true` if the faces data should be recalculated.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (recalculateFaces === undefined) { recalculateFaces = true; }
layer = this.getLayer(layer);
2018-02-07 22:46:07 +00:00
if (layer === null) { return 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 receive a new index. If an array of indexes is passed in, then
2018-02-07 22:46:07 +00:00
* 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @method Phaser.Tilemaps.Tilemap#randomize
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2020-11-23 10:23:10 +00:00
* @param {number[]} [indexes] - An array of indexes to randomly draw from during randomization.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 (layer === null) { return 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
*
* If no layer is specified, the maps current layer is used.
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#calculateFacesAt
* @since 3.0.0
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
* @param {number} tileY - The y coordinate, in tiles, not pixels.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 null; }
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
*
* If no layer is 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#calculateFacesWithin
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 null; }
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;
},
/**
* Removes the given TilemapLayer from this Tilemap without destroying it.
2019-06-29 19:13:15 +00:00
*
* If no layer is specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#removeLayer
* @since 3.17.0
*
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The tile layer to be removed.
*
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
*/
removeLayer: function (layer)
{
var index = this.getLayerIndex(layer);
if (index !== null)
{
SpliceOne(this.layers, index);
for (var i = index; i < this.layers.length; i++)
{
if (this.layers[i].tilemapLayer)
{
this.layers[i].tilemapLayer.layerIndex--;
}
}
if (this.currentLayerIndex === index)
{
this.currentLayerIndex = 0;
}
return this;
}
else
{
return null;
}
},
/**
* Destroys the given TilemapLayer and removes it from this Tilemap.
2019-06-29 19:13:15 +00:00
*
* If no layer is specified, the maps current layer is used.
*
* @method Phaser.Tilemaps.Tilemap#destroyLayer
* @since 3.17.0
*
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The tile layer to be destroyed.
*
* @return {?Phaser.Tilemaps.Tilemap} Returns this, or null if the layer given was invalid.
*/
destroyLayer: function (layer)
{
var index = this.getLayerIndex(layer);
if (index !== null)
{
layer = this.layers[index];
layer.destroy();
SpliceOne(this.layers, index);
if (this.currentLayerIndex === index)
{
this.currentLayerIndex = 0;
}
return this;
}
else
{
return null;
}
},
2017-11-30 00:54:29 +00:00
/**
* Removes all Tilemap Layers from this Tilemap and calls `destroy` on each of them.
2017-11-30 00:54:29 +00:00
*
2018-02-07 22:46:07 +00:00
* @method Phaser.Tilemaps.Tilemap#removeAllLayers
* @since 3.0.0
*
* @return {this} This Tilemap object.
2017-11-30 00:54:29 +00:00
*/
removeAllLayers: function ()
{
var layers = this.layers;
for (var i = 0; i < layers.length; i++)
2017-11-30 00:54:29 +00:00
{
if (layers[i].tilemapLayer)
2017-11-30 00:54:29 +00:00
{
layers[i].tilemapLayer.destroy(false);
2017-11-30 00:54:29 +00:00
}
}
2018-02-07 22:46:07 +00:00
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.
2019-06-29 19:13:15 +00:00
*
* @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.
2020-11-23 10:22:13 +00:00
* @param {number} [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
/**
* Removes the tile at the given tile coordinates in the specified layer and updates the layers collision information.
2018-03-20 11:36:35 +00:00
*
* If no layer is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @method Phaser.Tilemaps.Tilemap#removeTileAt
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
* @param {number} tileY - The y coordinate, in tiles, not pixels.
2020-11-26 12:55:40 +00:00
* @param {boolean} [replaceWithNull] - If `true` (the default), this will replace the tile at the specified location with null instead of a Tile with an index of -1.
* @param {boolean} [recalculateFaces] - If `true` (the default), the faces data will be recalculated.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
{
2020-11-26 12:55:40 +00:00
if (replaceWithNull === undefined) { replaceWithNull = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
2017-11-16 19:09:07 +00:00
layer = this.getLayer(layer);
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
/**
* Removes the tile at the given world coordinates in the specified layer and updates the layers collision information.
2018-03-20 11:36:35 +00:00
*
* If no layer is specified, the maps current layer is used.
2018-02-07 22:46:07 +00:00
*
* @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.
2020-11-26 12:55:40 +00:00
* @param {boolean} [replaceWithNull] - If `true` (the default), this will replace the tile at the specified location with null instead of a Tile with an index of -1.
* @param {boolean} [recalculateFaces] - If `true` (the default), the faces data will be recalculated.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (replaceWithNull === undefined) { replaceWithNull = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
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.RemoveTileAtWorldXY(worldX, worldY, replaceWithNull, recalculateFaces, camera, layer);
},
2017-11-30 00:54:29 +00:00
/**
* Draws a debug representation of the layer to the given Graphics object. This is helpful when you want to
2018-02-07 23:08:37 +00:00
* 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* **Note:** This method currently only works with orthogonal tilemap layers.
*
2018-02-07 23:08:37 +00:00
* @method Phaser.Tilemaps.Tilemap#renderDebug
* @since 3.0.0
*
* @param {Phaser.GameObjects.Graphics} graphics - The target Graphics object to draw upon.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Types.Tilemaps.StyleConfig} [styleConfig] - An object specifying the colors to use for the debug drawing.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 null; }
2018-02-07 23:08:37 +00:00
if (this.orientation === ORIENTATION.ORTHOGONAL)
{
TilemapComponents.RenderDebug(graphics, styleConfig, layer);
}
2018-02-07 23:08:37 +00:00
return this;
},
2019-04-08 15:05:12 +00:00
/**
* Draws a debug representation of all layers within this Tilemap to the given Graphics object.
2019-06-29 19:13:15 +00:00
*
2019-04-08 15:05:12 +00:00
* 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.
*
* @method Phaser.Tilemaps.Tilemap#renderDebugFull
* @since 3.17.0
*
* @param {Phaser.GameObjects.Graphics} graphics - The target Graphics object to draw upon.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Types.Tilemaps.StyleConfig} [styleConfig] - An object specifying the colors to use for the debug drawing.
2019-04-08 15:05:12 +00:00
*
2020-11-26 12:55:40 +00:00
* @return {this} This Tilemap instance.
2019-04-08 15:05:12 +00:00
*/
renderDebugFull: function (graphics, styleConfig)
{
var layers = this.layers;
for (var i = 0; i < layers.length; i++)
{
TilemapComponents.RenderDebug(graphics, styleConfig, layers[i]);
}
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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#replaceByIndex
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} findIndex - The index of the tile to search for.
* @param {number} newIndex - The index of the tile to replace it with.
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 (layer === null) { return 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#setCollision
* @since 3.0.0
*
2020-11-23 10:32:00 +00:00
* @param {(number|array)} indexes - Either a single tile index, or an array of tile indexes.
2020-11-26 12:55:40 +00:00
* @param {boolean} [collides] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces] - Whether or not to recalculate the tile faces after the update.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
* @param {boolean} [updateLayer=true] - If true, updates the current tiles on the layer. Set to false if no tiles have been placed for significant performance boost.
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, updateLayer)
{
2020-11-26 12:55:40 +00:00
if (collides === undefined) { collides = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
if (updateLayer === undefined) { updateLayer = true; }
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
TilemapComponents.SetCollision(indexes, collides, recalculateFaces, layer, updateLayer);
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
*
* If no layer is specified, the maps 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
*
2020-11-23 10:22:13 +00:00
* @param {number} start - The first index of the tile to be set for collision.
* @param {number} stop - The last index of the tile to be set for collision.
2020-11-26 12:55:40 +00:00
* @param {boolean} [collides] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces] - Whether or not to recalculate the tile faces after the update.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (collides === undefined) { collides = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
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
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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @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.
2020-11-26 12:55:40 +00:00
* @param {boolean} [collides] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces] - Whether or not to recalculate the tile faces after the update.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (collides === undefined) { collides = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
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
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). Tile indexes not currently in the layer are not affected.
2018-03-20 11:36:35 +00:00
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#setCollisionByExclusion
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2020-11-23 10:23:10 +00:00
* @param {number[]} indexes - An array of the tile indexes to not be counted for collision.
2020-11-26 12:55:40 +00:00
* @param {boolean} [collides] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces] - Whether or not to recalculate the tile faces after the update.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (collides === undefined) { collides = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
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
TilemapComponents.SetCollisionByExclusion(indexes, collides, recalculateFaces, layer);
2018-02-07 23:08:37 +00:00
return this;
},
/**
* Sets collision on the tiles within a layer by checking each tiles collision group data
2018-02-07 23:08:37 +00:00
* (typically defined in Tiled within the tileset collision editor). If any objects are found within
* a tiles collision group, the tiles colliding information will be set. The `collides` parameter
2018-02-07 23:08:37 +00:00
* controls if collision will be enabled (true) or disabled (false).
2018-03-20 11:36:35 +00:00
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#setCollisionFromCollisionGroup
* @since 3.0.0
*
2020-11-26 12:55:40 +00:00
* @param {boolean} [collides] - If true it will enable collision. If false it will clear collision.
* @param {boolean} [recalculateFaces] - Whether or not to recalculate the tile faces after the update.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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)
{
2020-11-26 12:55:40 +00:00
if (collides === undefined) { collides = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
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
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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#setTileIndexCallback
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
* @param {(number|number[])} indexes - Either a single tile index, or an array of tile indexes to have a collision callback set for. All values should be integers.
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.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
if (layer === null) { return null; }
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
/**
2019-10-29 05:50:33 +00:00
* Sets a collision callback for the given rectangular area (in tile coordinates) within the layer.
2018-02-07 23:08:37 +00:00
* 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#setTileLocationCallback
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} tileY - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} width - How many tiles wide from the `tileX` index the area will be.
* @param {number} 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.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
if (layer === null) { return null; }
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
*
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The name of the layer from Tiled, the index of the layer in the map or a TilemapLayer. If not given will default to the maps current layer index.
2018-03-20 11:36:35 +00:00
*
* @return {this} 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
*
2020-11-23 10:22:13 +00:00
* @param {number} tileWidth - The width of the tiles the map uses for calculations.
* @param {number} tileHeight - The height of the tiles the map uses for calculations.
2018-03-20 11:36:35 +00:00
*
* @return {this} 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 maps
2017-11-30 00:54:29 +00:00
* 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
*
2020-11-23 10:22:13 +00:00
* @param {number} tileWidth - The width of the tiles (in pixels) in the layer.
* @param {number} tileHeight - The height of the tiles (in pixels) in the layer.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The name of the layer from Tiled, the index of the layer in the map or a TilemapLayer. If not given will default to the maps current layer index.
2018-03-20 11:36:35 +00:00
*
* @return {this} 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
*
* If no layer is specified, the maps current layer is used.
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
*
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 (layer === null) { return 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#swapByIndex
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} tileA - First tile index.
* @param {number} tileB - Second tile index.
2020-11-26 12:55:40 +00:00
* @param {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 (layer === null) { return 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#tileToWorldX
* @since 3.0.0
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 this._convert.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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
* @method Phaser.Tilemaps.Tilemap#tileToWorldY
* @since 3.0.0
*
2020-11-23 10:22:13 +00:00
* @param {number} tileY - The y coordinate, in tiles, not pixels.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 this._convert.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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
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
*
2020-11-23 10:22:13 +00:00
* @param {number} tileX - The x coordinate, in tiles, not pixels.
* @param {number} tileY - The y coordinate, in tiles, not pixels.
* @param {Phaser.Math.Vector2} [vec2] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
* @return {?Phaser.Math.Vector2} Returns a Vector2, or null if the layer given was invalid.
*/
tileToWorldXY: function (tileX, tileY, vec2, 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 this._convert.TileToWorldXY(tileX, tileY, vec2, 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 picked is (the indexs weight) / (sum of all weights). This
2018-02-07 23:12:55 +00:00
* method only modifies tile indexes and does not change collision information.
2018-03-20 11:36:35 +00:00
*
* If no layer is specified, the maps current layer is used.
2017-12-03 14:56:12 +00:00
*
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
*
2020-11-26 12:55:40 +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 {number} [tileX] - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [tileY] - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} [width] - How many tiles wide from the `tileX` index the area will be.
* @param {number} [height] - How many tiles tall from the `tileY` index the area will be.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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
*/
2020-11-26 12:55:40 +00:00
weightedRandomize: function (weightedIndexes, tileX, tileY, width, height, layer)
2017-12-03 14:56:12 +00:00
{
layer = this.getLayer(layer);
2018-02-07 23:12:55 +00:00
if (layer === null) { return 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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
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.
2020-11-26 12:55:40 +00:00
* @param {boolean} [snapToFloor] - Whether or not to round the tile coordinate down to the nearest integer.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 this._convert.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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
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.
2020-11-26 12:55:40 +00:00
* @param {boolean} [snapToFloor] - Whether or not to round the tile coordinate down to the nearest integer.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [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 this._convert.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
*
* If no layer is specified, the maps current layer is used.
2018-02-07 23:08:37 +00:00
*
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.
2020-11-26 12:55:40 +00:00
* @param {boolean} [snapToFloor] - Whether or not to round the tile coordinate down to the nearest integer.
* @param {Phaser.Math.Vector2} [vec2] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
2020-11-26 12:55:40 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use when calculating the tile index from the world values.
2020-11-23 10:32:00 +00:00
* @param {(string|number|Phaser.Tilemaps.TilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
2018-02-07 23:12:55 +00:00
*
* @return {?Phaser.Math.Vector2} Returns a vec2, or null if the layer given was invalid.
2017-11-30 00:54:29 +00:00
*/
worldToTileXY: function (worldX, worldY, snapToFloor, vec2, 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 this._convert.WorldToTileXY(worldX, worldY, snapToFloor, vec2, camera, 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
* TilemapLayers that have been created.
2017-11-30 00:54:29 +00:00
*
* @method Phaser.Tilemaps.Tilemap#destroy
2018-02-07 23:08:37 +00:00
* @since 3.0.0
2017-11-30 00:54:29 +00:00
*/
destroy: function ()
{
this.removeAllLayers();
this.tilesets.length = 0;
this.objects.length = 0;
this.scene = null;
}
2017-11-15 20:55:26 +00:00
});
module.exports = Tilemap;