phaser/src/tilemaps/components/CalculateFacesWithin.js

54 lines
1.8 KiB
JavaScript
Raw Normal View History

var GetTileAt = require('./GetTileAt');
var GetTilesWithin = require('./GetTilesWithin');
2017-11-26 15:05:19 +00:00
/**
2017-11-26 15:33:19 +00:00
* Calculates interesting faces within the rectangular area specified (in tile coordinates) of the
* layer. Interesting faces are used internally for optimizing collisions against tiles. This method
* is mostly used internally.
2017-11-26 15:05:19 +00:00
*
2018-02-08 01:08:59 +00:00
* @function Phaser.Tilemaps.Components.CalculateFacesWithin
* @since 3.0.0
*
* @param {integer} [tileX=0] - [description]
* @param {integer} [tileY=0] - [description]
* @param {integer} [width=max width based on tileX] - [description]
* @param {integer} [height=max height based on tileY] - [description]
2018-02-08 02:02:37 +00:00
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
2017-11-26 15:05:19 +00:00
*/
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, null, layer);
for (var i = 0; i < tiles.length; i++)
{
var tile = tiles[i];
if (tile)
{
if (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;
}
else
{
tile.resetFaces();
}
}
}
};
module.exports = CalculateFacesWithin;