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.
|
|
|
|
*
|
|
|
|
* @param {LayerData} layer - [description]
|
2017-11-27 13:33:30 +00:00
|
|
|
* @param {Camera} [camera=main camera] - [description]
|
2017-11-26 15:05:19 +00:00
|
|
|
* @param {array} [outputArray] - [description]
|
2017-11-29 21:07:56 +00:00
|
|
|
* @returns {Tile[]}
|
2017-11-26 15:05:19 +00:00
|
|
|
*/
|
2017-11-18 14:31:59 +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-26 02:47:47 +00:00
|
|
|
var tileWidth = layer.tileWidth * tilemapLayer.scaleX;
|
|
|
|
var tileHeight = layer.tileHeight * tilemapLayer.scaleY;
|
2017-11-18 14:31:59 +00:00
|
|
|
|
|
|
|
for (var row = 0; row < mapHeight; ++row)
|
|
|
|
{
|
|
|
|
for (var col = 0; col < mapWidth; ++col)
|
|
|
|
{
|
|
|
|
var tile = mapData[row][col];
|
|
|
|
|
|
|
|
if (tile === null || (tile.index <= 0 && tilemapLayer.skipIndexZero)) { continue; }
|
|
|
|
|
2017-11-30 12:56:13 +00:00
|
|
|
var tileX = tile.worldX - left;
|
|
|
|
var tileY = tile.worldY - top;
|
|
|
|
var cullW = camera.width + tile.width;
|
|
|
|
var cullH = camera.height + tile.height;
|
2017-11-18 14:31:59 +00:00
|
|
|
|
|
|
|
if (tile.visible &&
|
2017-11-26 02:47:47 +00:00
|
|
|
tileX > -tileWidth && tileY > -tileHeight &&
|
2017-11-18 14:31:59 +00:00
|
|
|
tileX < cullW && tileY < cullH)
|
|
|
|
{
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CullTiles;
|