2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-22 01:16:46 +00:00
|
|
|
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.
|
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.CalculateFacesAt
|
|
|
|
* @since 3.0.0
|
2020-09-02 10:54:24 +00:00
|
|
|
*
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} tileX - The x coordinate.
|
|
|
|
* @param {number} tileY - The y coordinate.
|
2018-02-08 02:02:37 +00:00
|
|
|
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
|
2017-11-27 13:33:30 +00:00
|
|
|
*/
|
2018-01-29 22:30:57 +00:00
|
|
|
var CalculateFacesAt = function (tileX, tileY, layer)
|
2017-11-22 01:16:46 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2018-03-15 11:22:22 +00:00
|
|
|
tile.faceTop = true;
|
2017-11-22 01:16:46 +00:00
|
|
|
tile.faceBottom = true;
|
|
|
|
tile.faceLeft = true;
|
|
|
|
tile.faceRight = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset edges that are shared between tile and its neighbors
|
|
|
|
if (above && above.collides)
|
|
|
|
{
|
2020-09-02 10:54:24 +00:00
|
|
|
if (tileCollides)
|
|
|
|
{
|
|
|
|
tile.faceTop = false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:16:46 +00:00
|
|
|
above.faceBottom = !tileCollides;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (below && below.collides)
|
|
|
|
{
|
2020-09-02 10:54:24 +00:00
|
|
|
if (tileCollides)
|
|
|
|
{
|
|
|
|
tile.faceBottom = false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:16:46 +00:00
|
|
|
below.faceTop = !tileCollides;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left && left.collides)
|
|
|
|
{
|
2020-09-02 10:54:24 +00:00
|
|
|
if (tileCollides)
|
|
|
|
{
|
|
|
|
tile.faceLeft = false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:16:46 +00:00
|
|
|
left.faceRight = !tileCollides;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (right && right.collides)
|
|
|
|
{
|
2020-09-02 10:54:24 +00:00
|
|
|
if (tileCollides)
|
|
|
|
{
|
|
|
|
tile.faceRight = false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:16:46 +00:00
|
|
|
right.faceLeft = !tileCollides;
|
|
|
|
}
|
|
|
|
|
2020-09-02 10:54:24 +00:00
|
|
|
if (tile && !tile.collides)
|
|
|
|
{
|
|
|
|
tile.resetFaces();
|
|
|
|
}
|
2017-11-30 23:18:27 +00:00
|
|
|
|
2017-11-22 01:16:46 +00:00
|
|
|
return tile;
|
|
|
|
};
|
|
|
|
|
2018-01-29 22:30:57 +00:00
|
|
|
module.exports = CalculateFacesAt;
|