2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
var CONST = require('../../const.js');
|
2018-07-12 14:07:45 +00:00
|
|
|
var SnapFloor = require('../../math/snap/SnapFloor');
|
|
|
|
var SnapCeil = require('../../math/snap/SnapCeil');
|
2020-09-19 08:56:05 +00:00
|
|
|
var CheckIsoBounds = require('./CheckIsoBounds');
|
|
|
|
var GetCullBounds = require('./GetCullBounds');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Orientation-Decorated function that returns the tiles in the given layer that are within the camera's viewport. This is used internally.
|
|
|
|
*
|
|
|
|
* @function Phaser.Tilemaps.Components.GetStandardCullTiles
|
|
|
|
* @since 3.0.0
|
|
|
|
* --- decorator:
|
|
|
|
* @param {function} getCullBounds
|
|
|
|
* --- inner function :
|
|
|
|
* @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.
|
|
|
|
* @param {array} [outputArray] - An optional array to store the Tile objects within.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
|
|
|
|
*/
|
|
|
|
var GetStandardCullTiles = function (getCullBounds)
|
|
|
|
{
|
|
|
|
console.log('getting standard cull tiles function');
|
|
|
|
|
|
|
|
var StandardCullTiles = function (layer, camera, outputArray, renderOrder)
|
|
|
|
{
|
|
|
|
if (outputArray === undefined) { outputArray = []; }
|
|
|
|
if (renderOrder === undefined) { renderOrder = 0; }
|
|
|
|
console.log('culling tiles');
|
|
|
|
outputArray.length = 0;
|
|
|
|
|
|
|
|
var tilemapLayer = layer.tilemapLayer;
|
|
|
|
|
|
|
|
var mapData = layer.data;
|
|
|
|
var mapWidth = layer.width;
|
|
|
|
var mapHeight = layer.height;
|
|
|
|
|
|
|
|
var drawLeft = 0;
|
|
|
|
var drawRight = mapWidth;
|
|
|
|
var drawTop = 0;
|
|
|
|
var drawBottom = mapHeight;
|
|
|
|
|
|
|
|
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
|
|
|
|
{
|
|
|
|
// Camera world view bounds, snapped for scaled tile size
|
|
|
|
// Cull Padding values are given in tiles, not pixels
|
|
|
|
var bounds = getCullBounds(layer,camera);
|
|
|
|
|
|
|
|
drawLeft = Math.max(0, bounds.left);
|
|
|
|
drawRight = Math.min(mapWidth, bounds.right);
|
|
|
|
drawTop = Math.max(0, bounds.top);
|
|
|
|
drawBottom = Math.min(mapHeight, bounds.bottom);
|
|
|
|
}
|
|
|
|
var x;
|
|
|
|
var y;
|
|
|
|
var tile;
|
|
|
|
|
|
|
|
|
|
|
|
if (renderOrder === 0)
|
|
|
|
{
|
|
|
|
// right-down
|
|
|
|
|
|
|
|
for (y = drawTop; y < drawBottom; y++)
|
|
|
|
{
|
|
|
|
for (x = drawLeft; mapData[y] && x < drawRight; x++)
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 1)
|
|
|
|
{
|
|
|
|
// left-down
|
|
|
|
|
|
|
|
for (y = drawTop; y < drawBottom; y++)
|
|
|
|
{
|
|
|
|
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 2)
|
|
|
|
{
|
|
|
|
// right-up
|
|
|
|
|
|
|
|
for (y = drawBottom; y >= drawTop; y--)
|
|
|
|
{
|
|
|
|
for (x = drawLeft; mapData[y] && x < drawRight; x++)
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 3)
|
|
|
|
{
|
|
|
|
// left-up
|
|
|
|
|
|
|
|
for (y = drawBottom; y >= drawTop; y--)
|
|
|
|
{
|
|
|
|
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tilemapLayer.tilesDrawn = outputArray.length;
|
|
|
|
tilemapLayer.tilesTotal = mapWidth * mapHeight;
|
|
|
|
|
|
|
|
return outputArray;
|
|
|
|
};
|
|
|
|
return StandardCullTiles;
|
|
|
|
};
|
|
|
|
|
2018-07-12 14:07:45 +00:00
|
|
|
|
2017-11-26 15:05:19 +00:00
|
|
|
/**
|
2018-08-21 17:02:00 +00:00
|
|
|
* Returns the tiles in the given layer that are within the camera's viewport. This is used internally.
|
2017-11-26 15:05:19 +00:00
|
|
|
*
|
2020-09-19 08:56:05 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.IsoCullTiles
|
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.
|
2018-09-28 13:32:36 +00:00
|
|
|
* @param {array} [outputArray] - An optional array to store the Tile objects within.
|
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
|
|
|
*/
|
2020-09-19 08:56:05 +00:00
|
|
|
var IsoCullTiles = function (layer, camera, outputArray, renderOrder)
|
2017-11-18 14:31:59 +00:00
|
|
|
{
|
|
|
|
if (outputArray === undefined) { outputArray = []; }
|
2018-08-21 17:02:00 +00:00
|
|
|
if (renderOrder === undefined) { renderOrder = 0; }
|
2017-11-18 14:31:59 +00:00
|
|
|
|
|
|
|
outputArray.length = 0;
|
2018-07-15 01:09:36 +00:00
|
|
|
|
2018-08-06 12:31:27 +00:00
|
|
|
var tilemap = layer.tilemapLayer.tilemap;
|
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-08-06 12:31:27 +00:00
|
|
|
// We need to use the tile sizes defined for the map as a whole, not the layer,
|
|
|
|
// in order to calculate the bounds correctly. As different sized tiles may be
|
|
|
|
// placed on the grid and we cannot trust layer.baseTileWidth to give us the true size.
|
|
|
|
var tileW = Math.floor(tilemap.tileWidth * tilemapLayer.scaleX);
|
|
|
|
var tileH = Math.floor(tilemap.tileHeight * tilemapLayer.scaleY);
|
2018-07-15 01:09:36 +00:00
|
|
|
|
2018-07-18 14:06:56 +00:00
|
|
|
var drawLeft = 0;
|
2018-07-16 13:55:51 +00:00
|
|
|
var drawRight = mapWidth;
|
|
|
|
var drawTop = 0;
|
|
|
|
var drawBottom = mapHeight;
|
|
|
|
|
2018-08-06 21:22:32 +00:00
|
|
|
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
|
2018-07-16 13:55:51 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
if (layer.orientation === CONST.ORTHOGONAL || layer.orientation === CONST.STAGGERED || layer.orientation === CONST.HEXAGONAL)
|
|
|
|
{
|
|
|
|
// Camera world view bounds, snapped for scaled tile size
|
|
|
|
// Cull Padding values are given in tiles, not pixels
|
|
|
|
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
|
|
|
|
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
|
2018-08-02 23:40:56 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
var boundsTop;
|
|
|
|
var boundsBottom;
|
2018-08-02 23:40:56 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (layer.orientation === CONST.ORTHOGONAL)
|
|
|
|
{
|
|
|
|
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
|
|
|
|
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
|
|
|
|
}
|
|
|
|
else if (layer.orientation === CONST.STAGGERED)
|
|
|
|
{
|
|
|
|
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH / 2, 0, true) - tilemapLayer.cullPaddingY;
|
|
|
|
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH / 2, 0, true) + tilemapLayer.cullPaddingY;
|
|
|
|
}
|
|
|
|
else if (layer.orientation === CONST.HEXAGONAL)
|
|
|
|
{
|
|
|
|
var sidel = layer.hexSideLength;
|
|
|
|
var rowH = ((tileH - sidel) / 2 + sidel);
|
2018-07-16 13:55:51 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, rowH, 0, true) - tilemapLayer.cullPaddingY;
|
|
|
|
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, rowH, 0, true) + tilemapLayer.cullPaddingY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
drawLeft = Math.max(0, boundsLeft);
|
|
|
|
drawRight = Math.min(mapWidth, boundsRight);
|
|
|
|
drawTop = Math.max(0, boundsTop);
|
|
|
|
|
|
|
|
drawBottom = Math.min(mapHeight, boundsBottom);
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 17:02:00 +00:00
|
|
|
var x;
|
|
|
|
var y;
|
|
|
|
var tile;
|
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
|
|
|
|
if (layer.orientation === CONST.ORTHOGONAL || layer.orientation === CONST.STAGGERED || layer.orientation === CONST.HEXAGONAL)
|
2018-07-14 19:30:15 +00:00
|
|
|
{
|
2018-08-21 17:02:00 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (renderOrder === 0)
|
2018-07-14 19:30:15 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
// right-down
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
for (y = drawTop; y < drawBottom; y++)
|
|
|
|
{
|
|
|
|
for (x = drawLeft; mapData[y] && x < drawRight; x++)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
tile = mapData[y][x];
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
2018-08-21 17:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 08:56:05 +00:00
|
|
|
else if (renderOrder === 1)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
// left-down
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
for (y = drawTop; y < drawBottom; y++)
|
|
|
|
{
|
|
|
|
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
tile = mapData[y][x];
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
2018-07-14 19:30:15 +00:00
|
|
|
}
|
2018-08-21 17:02:00 +00:00
|
|
|
}
|
2020-09-19 08:56:05 +00:00
|
|
|
else if (renderOrder === 2)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
// right-up
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
for (y = drawBottom; y >= drawTop; y--)
|
|
|
|
{
|
|
|
|
for (x = drawLeft; mapData[y] && x < drawRight; x++)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
2018-08-21 17:02:00 +00:00
|
|
|
}
|
2020-09-19 08:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 3)
|
|
|
|
{
|
|
|
|
// left-up
|
|
|
|
|
|
|
|
for (y = drawBottom; y >= drawTop; y--)
|
|
|
|
{
|
|
|
|
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
2018-08-21 17:02:00 +00:00
|
|
|
}
|
2018-07-14 19:30:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 08:56:05 +00:00
|
|
|
else if (layer.orientation === CONST.ISOMETRIC)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
if (renderOrder === 0)
|
|
|
|
{
|
|
|
|
// right-down
|
|
|
|
|
|
|
|
for (y = drawTop; y < drawBottom; y++)
|
|
|
|
{
|
|
|
|
for (x = drawLeft; mapData[y] && x < drawRight; x++)
|
|
|
|
{
|
|
|
|
if (CheckIsoBounds(x,y,layer,camera))
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
2018-08-21 17:02:00 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 1)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
// left-down
|
|
|
|
|
|
|
|
for (y = drawTop; y < drawBottom; y++)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
|
|
|
|
{
|
|
|
|
if (CheckIsoBounds(x,y,layer,camera))
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 2)
|
|
|
|
{
|
|
|
|
// right-up
|
|
|
|
|
|
|
|
for (y = drawBottom; y >= drawTop; y--)
|
|
|
|
{
|
|
|
|
for (x = drawLeft; mapData[y] && x < drawRight; x++)
|
2018-08-21 17:02:00 +00:00
|
|
|
{
|
2020-09-19 08:56:05 +00:00
|
|
|
if (CheckIsoBounds(x,y,layer,camera))
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
2018-08-21 17:02:00 +00:00
|
|
|
}
|
2020-09-19 08:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (renderOrder === 3)
|
|
|
|
{
|
|
|
|
// left-up
|
2018-10-15 18:31:46 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
for (y = drawBottom; y >= drawTop; y--)
|
|
|
|
{
|
|
|
|
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
|
|
|
|
{
|
|
|
|
if (CheckIsoBounds(x,y,layer,camera))
|
|
|
|
{
|
|
|
|
tile = mapData[y][x];
|
|
|
|
|
|
|
|
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 17:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
|
|
|
|
var CullTiles = function (orientation)
|
|
|
|
{
|
|
|
|
switch (orientation)
|
|
|
|
{
|
|
|
|
case CONST.ISOMETRIC:
|
|
|
|
return IsoCullTiles;
|
|
|
|
default:
|
|
|
|
return GetStandardCullTiles(GetCullBounds(orientation));
|
|
|
|
}
|
|
|
|
};
|
2017-11-18 14:31:59 +00:00
|
|
|
module.exports = CullTiles;
|