2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-11-16 19:09:07 +00:00
|
|
|
var Tile = require('../Tile');
|
|
|
|
var IsInLayerBounds = require('./IsInLayerBounds');
|
2018-01-29 22:30:57 +00:00
|
|
|
var CalculateFacesAt = require('./CalculateFacesAt');
|
2018-01-29 22:51:08 +00:00
|
|
|
var SetTileCollision = require('./SetTileCollision');
|
2017-11-16 19:09:07 +00:00
|
|
|
|
2017-11-27 13:33:30 +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-02-08 01:08:59 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.PutTileAt
|
2018-04-16 14:25:22 +00:00
|
|
|
* @private
|
2018-02-08 01:08:59 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-20 15:11:33 +00:00
|
|
|
* @param {(integer|Phaser.Tilemaps.Tile)} tile - The index of this tile to set or a Tile object.
|
2018-09-28 13:32:36 +00:00
|
|
|
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
|
|
|
|
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
|
|
|
|
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
|
2018-02-08 02:02:37 +00:00
|
|
|
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
|
2018-03-20 15:11:33 +00:00
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @return {Phaser.Tilemaps.Tile} The Tile object that was created or added to this map.
|
2017-11-27 13:33:30 +00:00
|
|
|
*/
|
2017-11-22 01:18:34 +00:00
|
|
|
var PutTileAt = function (tile, tileX, tileY, recalculateFaces, layer)
|
2017-11-16 19:09:07 +00:00
|
|
|
{
|
|
|
|
if (!IsInLayerBounds(tileX, tileY, layer)) { return null; }
|
2017-11-22 01:18:34 +00:00
|
|
|
if (recalculateFaces === undefined) { recalculateFaces = true; }
|
|
|
|
|
|
|
|
var oldTile = layer.data[tileY][tileX];
|
|
|
|
var oldTileCollides = oldTile && oldTile.collides;
|
2017-11-16 19:09:07 +00:00
|
|
|
|
|
|
|
if (tile instanceof Tile)
|
|
|
|
{
|
|
|
|
if (layer.data[tileY][tileX] === null)
|
|
|
|
{
|
|
|
|
layer.data[tileY][tileX] = new Tile(layer, tile.index, tileX, tileY, tile.width, tile.height);
|
|
|
|
}
|
2017-11-27 13:19:20 +00:00
|
|
|
layer.data[tileY][tileX].copy(tile);
|
2017-11-16 19:09:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var index = tile;
|
|
|
|
if (layer.data[tileY][tileX] === null)
|
|
|
|
{
|
|
|
|
layer.data[tileY][tileX] = new Tile(layer, index, tileX, tileY, layer.tileWidth, layer.tileHeight);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
layer.data[tileY][tileX].index = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:13:43 +00:00
|
|
|
// Updating colliding flag on the new tile
|
|
|
|
var newTile = layer.data[tileY][tileX];
|
2018-01-29 22:51:08 +00:00
|
|
|
var collides = layer.collideIndexes.indexOf(newTile.index) !== -1;
|
|
|
|
SetTileCollision(newTile, collides);
|
2017-11-16 19:09:07 +00:00
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
// Recalculate faces only if the colliding flag at (tileX, tileY) has changed
|
|
|
|
if (recalculateFaces && (oldTileCollides !== newTile.collides))
|
|
|
|
{
|
2018-01-29 22:30:57 +00:00
|
|
|
CalculateFacesAt(tileX, tileY, layer);
|
2017-11-22 01:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newTile;
|
2017-11-16 19:09:07 +00:00
|
|
|
};
|
|
|
|
|
2017-11-18 22:11:51 +00:00
|
|
|
module.exports = PutTileAt;
|
2017-11-16 19:09:07 +00:00
|
|
|
|