Better optimized calc/recalc faces methods for tiles

These eliminate the need for the preventRecalc method from v2. If an individual tile is changed with putTileAt, only the min number of faces will be recalculated (vs all faces being recalculated in v2)
This commit is contained in:
Michael Hadley 2017-11-21 19:16:46 -06:00
parent dd8bbfa625
commit bdea565586
4 changed files with 85 additions and 7 deletions

View file

@ -0,0 +1,32 @@
var GetTileAt = require('./GetTileAt');
var GetTilesWithin = require('./GetTilesWithin');
var CalculateFacesWithin = function (tileX, tileY, width, height, layer)
{
var above = null;
var below = null;
var left = null;
var right = null;
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
for (var i = 0; i < tiles.length; i++)
{
var tile = tiles[i];
if (tile && tile.collides)
{
above = GetTileAt(tile.x, tile.y - 1, true, layer);
below = GetTileAt(tile.x, tile.y + 1, true, layer);
left = GetTileAt(tile.x - 1, tile.y, true, layer);
right = GetTileAt(tile.x + 1, tile.y, true, layer);
tile.faceTop = (above && above.collides) ? false : true;
tile.faceBottom = (below && below.collides) ? false : true;
tile.faceLeft = (left && left.collides) ? false : true;
tile.faceRight = (right && right.collides) ? false : true;
}
}
};
module.exports = CalculateFacesWithin;

View file

@ -1,6 +0,0 @@
var RecalculateFaces = function (layer)
{
return;
};
module.exports = RecalculateFaces;

View file

@ -0,0 +1,52 @@
var GetTileAt = require('./GetTileAt');
// Recalculate the faces, assuming only one tile location has been changed
// Used internally to update faces quickly for PutTileAt/RemoveTileAt/etc. Alternate approach to
// 951.
var RecalculateFacesAt = function (tileX, tileY, layer)
{
var tile = GetTileAt(tileX, tileY, true, layer);
var above = GetTileAt(tileX, tileY - 1, true, layer);
var below = GetTileAt(tileX, tileY + 1, true, layer);
var left = GetTileAt(tileX - 1, tileY, true, layer);
var right = GetTileAt(tileX + 1, tileY, true, layer);
var tileCollides = tile && tile.collides;
// Assume the changed tile has all interesting edges
if (tileCollides)
{
tile.faceBottom = true;
tile.faceBottom = true;
tile.faceLeft = true;
tile.faceRight = true;
}
// Reset edges that are shared between tile and its neighbors
if (above && above.collides)
{
if (tileCollides) { tile.faceTop = false; }
above.faceBottom = !tileCollides;
}
if (below && below.collides)
{
if (tileCollides) { tile.faceBottom = false; }
below.faceTop = !tileCollides;
}
if (left && left.collides)
{
if (tileCollides) { tile.faceLeft = false; }
left.faceRight = !tileCollides;
}
if (right && right.collides)
{
if (tileCollides) { tile.faceRight = false; }
right.faceLeft = !tileCollides;
}
return tile;
};
module.exports = RecalculateFacesAt;

View file

@ -14,7 +14,7 @@ module.exports = {
PutTileAt: require('./PutTileAt'),
PutTileAtWorldXY: require('./PutTileAtWorldXY'),
Randomize: require('./Randomize'),
RecalculateFaces: require('./RecalculateFaces'),
CalculateFacesWithin: require('./CalculateFacesWithin'),
RemoveTileAt: require('./RemoveTileAt'),
RemoveTileAtWorldXY: require('./RemoveTileAtWorldXY'),
ReplaceByIndex: require('./ReplaceByIndex'),