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 14:31:25 +00:00
|
|
|
var skipCull = tilemapLayer.skipCull;
|
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:32:25 +00:00
|
|
|
var boundsLeft = SnapFloor(camera.worldView.x, tileW) - tileW;
|
|
|
|
var boundsRight = SnapCeil(camera.worldView.right, tileW) + tileW;
|
|
|
|
var boundsTop = SnapFloor(camera.worldView.y, tileH) - tileH;
|
|
|
|
var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + tileH;
|
2018-07-12 00:14:34 +00:00
|
|
|
|
|
|
|
for (var y = 0; y < mapHeight; y++)
|
2018-04-23 16:45:42 +00:00
|
|
|
{
|
2018-07-12 00:14:34 +00:00
|
|
|
for (var x = 0; x < mapWidth; x++)
|
2018-04-23 16:45:42 +00:00
|
|
|
{
|
|
|
|
var tile = mapData[y][x];
|
|
|
|
|
2018-07-12 14:31:25 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
2018-05-24 22:02:59 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-23 15:40:19 +00:00
|
|
|
|
2018-07-12 15:12:55 +00:00
|
|
|
var tilePixelX = (tile.pixelX + tilemapLayer.x) * tilemapLayer.scaleX;
|
|
|
|
var tilePixelY = (tile.pixelY + tilemapLayer.y) * tilemapLayer.scaleY;
|
|
|
|
|
|
|
|
if (skipCull || (tilePixelX >= boundsLeft && tilePixelX + tileW <= boundsRight && tilePixelY >= boundsTop && tilePixelY + tileH <= boundsBottom))
|
2018-04-23 16:45:42 +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;
|