phaser/src/tilemaps/components/CullTiles.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-11-26 15:05:19 +00:00
/**
* Returns the tiles in the given layer that are within the camera's viewport. This is used
* internally.
*
2018-02-08 01:08:59 +00:00
* @function Phaser.Tilemaps.Components.CullTiles
* @since 3.0.0
*
2018-02-08 02:02:37 +00:00
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
2017-11-26 15:05:19 +00:00
* @param {array} [outputArray] - [description]
2018-02-08 01:08:59 +00:00
*
2018-02-08 02:02:37 +00:00
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
2017-11-26 15:05:19 +00:00
*/
var CullTiles = function (layer, camera, outputArray)
{
if (outputArray === undefined) { outputArray = []; }
outputArray.length = 0;
var tilemapLayer = layer.tilemapLayer;
var mapData = layer.data;
var mapWidth = layer.width;
var mapHeight = layer.height;
var left = (camera.scrollX * tilemapLayer.scrollFactorX) - tilemapLayer.x;
var top = (camera.scrollY * tilemapLayer.scrollFactorY) - tilemapLayer.y;
2017-11-30 17:15:35 +00:00
var sx = tilemapLayer.scaleX;
var sy = tilemapLayer.scaleY;
var tileWidth = layer.tileWidth * sx;
var tileHeight = layer.tileHeight * sy;
for (var row = 0; row < mapHeight; ++row)
{
for (var col = 0; col < mapWidth; ++col)
{
var tile = mapData[row][col];
if (tile === null || tile.index === -1) { continue; }
var tileX = tile.pixelX * sx - left;
var tileY = tile.pixelY * sy - top;
2017-11-30 17:15:35 +00:00
var cullW = camera.width + tileWidth;
var cullH = camera.height + tileHeight;
if (tile.visible &&
tileX > -tileWidth && tileY > -tileHeight &&
tileX < cullW && tileY < cullH)
{
outputArray.push(tile);
}
}
}
return outputArray;
};
module.exports = CullTiles;