2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-07-12 14:07:45 +00:00
|
|
|
var SnapFloor = require('../../math/snap/SnapFloor');
|
|
|
|
var SnapCeil = require('../../math/snap/SnapCeil');
|
|
|
|
|
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
|
2018-04-16 14:25:22 +00:00
|
|
|
* @private
|
2018-02-08 01:08:59 +00:00
|
|
|
* @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-03-15 19:32:09 +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
|
|
|
*/
|
2017-11-18 14:31:59 +00:00
|
|
|
var CullTiles = function (layer, camera, outputArray)
|
|
|
|
{
|
|
|
|
if (outputArray === undefined) { outputArray = []; }
|
|
|
|
|
|
|
|
outputArray.length = 0;
|
2018-04-23 16:45:42 +00:00
|
|
|
|
2017-11-18 14:31:59 +00:00
|
|
|
var tilemapLayer = layer.tilemapLayer;
|
2018-07-12 00:14:34 +00:00
|
|
|
|
2018-07-12 14:07:45 +00:00
|
|
|
var tileW = Math.floor(layer.tileWidth * tilemapLayer.scaleX);
|
|
|
|
var tileH = Math.floor(layer.tileHeight * tilemapLayer.scaleY);
|
2018-07-12 00:14:34 +00:00
|
|
|
|
2018-04-23 16:45:42 +00:00
|
|
|
var mapData = layer.data;
|
|
|
|
var mapWidth = layer.width;
|
|
|
|
var mapHeight = layer.height;
|
2018-07-12 00:14:34 +00:00
|
|
|
|
2018-07-12 14:07:45 +00:00
|
|
|
// Camera world view bounds, snapped for tile size
|
|
|
|
|
2018-07-12 15:51:54 +00:00
|
|
|
var boundsLeft = SnapFloor(camera.worldView.x, tileW) - (tilemapLayer.cullPaddingX * tileW);
|
|
|
|
var boundsRight = SnapCeil(camera.worldView.right, tileW) + (tilemapLayer.cullPaddingX * tileW);
|
|
|
|
var boundsTop = SnapFloor(camera.worldView.y, tileH) - (tilemapLayer.cullPaddingY * tileH);
|
|
|
|
var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + (tilemapLayer.cullPaddingY * tileH);
|
2018-07-14 19:14:55 +00:00
|
|
|
|
|
|
|
// If skipping cull, loop through every tile in the map.
|
|
|
|
|
|
|
|
if(tilemapLayer.skipCull)
|
|
|
|
{
|
|
|
|
for (var y = 0; y < mapHeight; y++)
|
|
|
|
{
|
|
|
|
for (var x = 0; x < mapWidth; x++)
|
|
|
|
{
|
|
|
|
var tile = mapData[y][x];
|
2018-07-12 00:14:34 +00:00
|
|
|
|
2018-07-14 19:14:55 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-23 16:45:42 +00:00
|
|
|
|
2018-07-14 19:14:55 +00:00
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var drawLeft = Math.max(0, boundsLeft / layer.tileWidth);
|
|
|
|
var drawRight = Math.min(mapWidth, boundsRight / layer.tileWidth);
|
|
|
|
var drawTop = Math.max(0, boundsTop / layer.tileHeight);
|
|
|
|
var drawBottom = Math.min(mapHeight, boundsBottom / layer.tileHeight);
|
|
|
|
|
|
|
|
for (var y = drawTop; y < drawBottom; y++)
|
|
|
|
{
|
|
|
|
for (var x = drawLeft; x < drawRight; x++)
|
|
|
|
{
|
|
|
|
var tile = mapData[y][x];
|
2018-06-23 15:40:19 +00:00
|
|
|
|
2018-07-14 19:14:55 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-23 16:45:42 +00:00
|
|
|
|
2018-07-14 19:14:55 +00:00
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 15:12:55 +00:00
|
|
|
tilemapLayer.tilesDrawn = outputArray.length;
|
|
|
|
tilemapLayer.tilesTotal = mapWidth * mapHeight;
|
2018-07-12 00:14:34 +00:00
|
|
|
|
2017-11-18 14:31:59 +00:00
|
|
|
return outputArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CullTiles;
|