phaser/src/tilemaps/components/CullTiles.js

99 lines
3 KiB
JavaScript
Raw Normal View History

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}
*/
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
* @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-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;
2018-04-23 16:45:42 +00:00
2018-07-14 19:41:13 +00:00
var y = 0;
var x = 0;
var tile = null;
var tilemapLayer = layer.tilemapLayer;
2018-07-12 00:14:34 +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
// Camera world view bounds, snapped for tile size
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:30:15 +00:00
// If skipping cull, loop through every tile in the map.
if(tilemapLayer.skipCull)
{
for (y = 0; y < mapHeight; y++)
2018-07-14 19:30:15 +00:00
{
for (x = 0; x < mapWidth; x++)
2018-07-14 19:30:15 +00:00
{
tile = mapData[y][x];
2018-07-12 00:14:34 +00:00
2018-07-14 19:30:15 +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:30:15 +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 (y = drawTop; y < drawBottom; y++)
2018-07-14 19:30:15 +00:00
{
for (x = drawLeft; x < drawRight; x++)
2018-07-14 19:30:15 +00:00
{
tile = mapData[y][x];
2018-06-23 15:40:19 +00:00
2018-07-14 19:30:15 +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:30:15 +00:00
outputArray.push(tile);
}
}
}
tilemapLayer.tilesDrawn = outputArray.length;
tilemapLayer.tilesTotal = mapWidth * mapHeight;
2018-07-12 00:14:34 +00:00
return outputArray;
};
module.exports = CullTiles;