2017-11-16 19:09:07 +00:00
|
|
|
var Tile = require('../Tile');
|
|
|
|
var IsInLayerBounds = require('./IsInLayerBounds');
|
2017-11-22 01:18:34 +00:00
|
|
|
var RecalculateFacesAt = require('./RecalculateFacesAt');
|
2017-11-16 19:09:07 +00:00
|
|
|
|
|
|
|
// Put Phaser.Tile|number. Note: does not place a reference to tile, it copies the tile or creates a
|
|
|
|
// new one.
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
layer.data[tileY][tileX].copy(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
if (layer.collideIndexes.indexOf(newTile.index) !== -1)
|
|
|
|
{
|
|
|
|
newTile.setCollision(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newTile.resetCollision();
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
RecalculateFacesAt(tileX, tileY, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|