phaser/src/tilemaps/components/CalculateFacesAt.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

var GetTileAt = require('./GetTileAt');
2017-11-27 13:33:30 +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.
*
* @param {integer} tileX - [description]
* @param {integer} tileY - [description]
2017-11-27 13:33:30 +00:00
* @param {LayerData} layer - [description]
*/
var CalculateFacesAt = 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;
}
if (tile && !tile.collides) { tile.resetFaces(); }
return tile;
};
module.exports = CalculateFacesAt;