Copy update: copy all tile props and recalculate faces within dest region

This commit is contained in:
Michael Hadley 2017-11-26 08:58:38 -06:00
parent dfa3cc243a
commit 6ca7c9ecfd
4 changed files with 20 additions and 9 deletions

View file

@ -41,7 +41,7 @@ var Tile = new Class({
this.tint = 0xFFFFFF;
},
// Copy everything except position
// Copy everything except position & interesting faces
copy: function (tile)
{
this.index = tile.index;

View file

@ -189,13 +189,13 @@ var Tilemap = new Class({
return layer;
},
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY, layer)
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, layer)
{
layer = this.getLayer(layer);
if (this._isStaticCall(layer, 'copy')) { return this; }
if (layer !== null)
{
TilemapComponents.Copy(srcTileX, srcTileY, width, height, destTileX, destTileY, layer);
TilemapComponents.Copy(srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, layer);
}
return this;
},

View file

@ -1,10 +1,12 @@
var GetTilesWithin = require('./GetTilesWithin');
var CalculateFacesWithin = require('./CalculateFacesWithin');
// Copies indices, not other properties. Does not modify collisions.
var Copy = function (srcTileX, srcTileY, width, height, destTileX, destTileY, layer)
var Copy = function (srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, layer)
{
if (srcTileX === undefined || srcTileX < 0) { srcTileX = 0; }
if (srcTileY === undefined || srcTileY < 0) { srcTileY = 0; }
if (srcTileX < 0) { srcTileX = 0; }
if (srcTileY < 0) { srcTileY = 0; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
var srcTiles = GetTilesWithin(srcTileX, srcTileY, width, height, null, layer);
@ -17,9 +19,18 @@ var Copy = function (srcTileX, srcTileY, width, height, destTileX, destTileY, la
var tileY = srcTiles[i].y + offsetY;
if (tileX >= 0 && tileX < layer.width && tileY >= 0 && tileY < layer.height)
{
layer.data[tileY][tileX].index = srcTiles[i].index;
if (layer.data[tileY][tileX])
{
layer.data[tileY][tileX].copy(srcTiles[i]);
}
}
}
if (recalculateFaces)
{
// Recalculate the faces within the destination area and neighboring tiles
CalculateFacesWithin(destTileX - 1, destTileY - 1, width + 2, height + 2, layer);
}
};
module.exports = Copy;

View file

@ -60,9 +60,9 @@ var DynamicTilemapLayer = new Class({
TilemapComponents.CullTiles(this.layer, camera, this.culledTiles);
},
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY)
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces)
{
TilemapComponents.Copy(srcTileX, srcTileY, width, height, destTileX, destTileY, this.layer);
TilemapComponents.Copy(srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, this.layer);
return this;
},