phaser/src/tilemaps/components/CullTiles.js

85 lines
2.5 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-07-15 01:09:36 +00:00
2018-07-14 19:41:13 +00:00
var y = 0;
var x = 0;
var tile = null;
2018-07-12 00:14:34 +00:00
2018-07-15 01:09:36 +00:00
var tilemapLayer = layer.tilemapLayer;
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-15 01:09:36 +00:00
var tileW = Math.floor(layer.tileWidth * tilemapLayer.scaleX);
var tileH = Math.floor(layer.tileHeight * tilemapLayer.scaleY);
// Camera world view bounds, snapped for scaled 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-15 01:09:36 +00:00
2018-07-18 14:06:56 +00:00
var drawLeft = 0;
var drawRight = mapWidth;
var drawTop = 0;
var drawBottom = mapHeight;
if (!tilemapLayer.skipCull)
{
2018-07-16 14:20:46 +00:00
drawLeft = Math.max(0, boundsLeft / tileW);
drawRight = Math.min(mapWidth, boundsRight / tileW);
drawTop = Math.max(0, boundsTop / tileH);
drawBottom = Math.min(mapHeight, boundsBottom / tileH);
}
2018-07-15 01:09:36 +00:00
for (y = drawTop; y < drawBottom; y++)
2018-07-14 19:30:15 +00:00
{
2018-07-15 01:09:36 +00:00
for (x = drawLeft; x < drawRight; x++)
2018-07-14 19:30:15 +00:00
{
2018-07-15 01:09:36 +00:00
tile = mapData[y][x];
2018-04-23 16:45:42 +00:00
2018-07-15 01:09:36 +00:00
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
2018-07-14 19:30:15 +00:00
{
2018-07-15 01:09:36 +00:00
continue;
2018-07-14 19:30:15 +00:00
}
2018-07-15 01:09:36 +00:00
outputArray.push(tile);
2018-07-14 19:30:15 +00:00
}
}
tilemapLayer.tilesDrawn = outputArray.length;
tilemapLayer.tilesTotal = mapWidth * mapHeight;
2018-07-12 00:14:34 +00:00
return outputArray;
};
module.exports = CullTiles;