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}
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
var zoom = camera.zoom;
|
|
|
|
var originX = camera.width / 2;
|
|
|
|
var originY = camera.height / 2;
|
|
|
|
|
|
|
|
camera.matrix.loadIdentity();
|
|
|
|
camera.matrix.translate(camera.x + originX, camera.y + originY);
|
|
|
|
camera.matrix.rotate(camera.rotation);
|
|
|
|
camera.matrix.scale(zoom, zoom);
|
|
|
|
camera.matrix.translate(-originX, -originY);
|
|
|
|
camera.matrix.invert();
|
2017-11-18 14:31:59 +00:00
|
|
|
|
2018-05-24 22:02:59 +00:00
|
|
|
camera.shakeEffect.preRender();
|
|
|
|
|
2017-11-18 14:31:59 +00:00
|
|
|
var tilemapLayer = layer.tilemapLayer;
|
2018-04-23 16:45:42 +00:00
|
|
|
var tileW = layer.tileWidth;
|
|
|
|
var tileH = layer.tileHeight;
|
|
|
|
var cullX = ((camera.scrollX * tilemapLayer.scrollFactorX) - tileW);
|
|
|
|
var cullY = ((camera.scrollY * tilemapLayer.scrollFactorY) - tileH);
|
|
|
|
var cullW = (cullX + (camera.width + tileW * 2));
|
|
|
|
var cullH = (cullY + (camera.height + tileH * 2));
|
|
|
|
var mapData = layer.data;
|
|
|
|
var mapWidth = layer.width;
|
|
|
|
var mapHeight = layer.height;
|
|
|
|
var cameraMatrix = camera.matrix.matrix;
|
|
|
|
var a = cameraMatrix[0];
|
|
|
|
var b = cameraMatrix[1];
|
|
|
|
var c = cameraMatrix[2];
|
|
|
|
var d = cameraMatrix[3];
|
|
|
|
var e = cameraMatrix[4];
|
|
|
|
var f = cameraMatrix[5];
|
|
|
|
var tCullX = cullX * a + cullY * c + e;
|
|
|
|
var tCullY = cullX * b + cullY * d + f;
|
|
|
|
var tCullW = cullW * a + cullH * c + e;
|
|
|
|
var tCullH = cullW * b + cullH * d + f;
|
|
|
|
|
|
|
|
for (var y = 0; y < mapHeight; ++y)
|
|
|
|
{
|
|
|
|
for (var x = 0; x < mapWidth; ++x)
|
|
|
|
{
|
|
|
|
var tile = mapData[y][x];
|
|
|
|
|
2018-04-23 22:52:57 +00:00
|
|
|
if (tile === null || tile.index === -1)
|
2018-05-24 22:02:59 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-23 15:34:45 +00:00
|
|
|
|
|
|
|
var tileX = ((tile.pixelX + tilemapLayer.x) * a + (tile.pixelY + tilemapLayer.y) * c + e);
|
|
|
|
var tileY = ((tile.pixelX + tilemapLayer.x) * b + (tile.pixelY + tilemapLayer.y) * d + f);
|
2018-04-23 16:45:42 +00:00
|
|
|
|
|
|
|
if (tile.visible &&
|
|
|
|
tileX >= tCullX &&
|
|
|
|
tileY >= tCullY &&
|
|
|
|
tileX + tileW <= tCullW &&
|
|
|
|
tileY + tileH <= tCullH
|
2018-04-23 22:52:57 +00:00
|
|
|
)
|
2018-04-23 16:45:42 +00:00
|
|
|
{
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:52:57 +00:00
|
|
|
/* var tilemapLayer = layer.tilemapLayer;
|
2017-11-18 14:31:59 +00:00
|
|
|
var mapData = layer.data;
|
|
|
|
var mapWidth = layer.width;
|
|
|
|
var mapHeight = layer.height;
|
2018-03-15 19:32:09 +00:00
|
|
|
var left = (camera.scrollX * camera.zoom * tilemapLayer.scrollFactorX) - tilemapLayer.x;
|
|
|
|
var top = (camera.scrollY * camera.zoom * tilemapLayer.scrollFactorY) - tilemapLayer.y;
|
2017-11-30 17:15:35 +00:00
|
|
|
var sx = tilemapLayer.scaleX;
|
|
|
|
var sy = tilemapLayer.scaleY;
|
|
|
|
var tileWidth = layer.tileWidth * sx;
|
|
|
|
var tileHeight = layer.tileHeight * sy;
|
2017-11-18 14:31:59 +00:00
|
|
|
|
|
|
|
for (var row = 0; row < mapHeight; ++row)
|
|
|
|
{
|
|
|
|
for (var col = 0; col < mapWidth; ++col)
|
|
|
|
{
|
|
|
|
var tile = mapData[row][col];
|
|
|
|
|
2017-12-01 18:04:15 +00:00
|
|
|
if (tile === null || tile.index === -1) { continue; }
|
2017-11-18 14:31:59 +00:00
|
|
|
|
2017-12-01 15:40:55 +00:00
|
|
|
var tileX = tile.pixelX * sx - left;
|
|
|
|
var tileY = tile.pixelY * sy - top;
|
2017-11-30 17:15:35 +00:00
|
|
|
var cullW = camera.width + tileWidth;
|
|
|
|
var cullH = camera.height + tileHeight;
|
2017-11-18 14:31:59 +00:00
|
|
|
|
|
|
|
if (tile.visible &&
|
2017-11-26 02:47:47 +00:00
|
|
|
tileX > -tileWidth && tileY > -tileHeight &&
|
2017-11-18 14:31:59 +00:00
|
|
|
tileX < cullW && tileY < cullH)
|
|
|
|
{
|
|
|
|
outputArray.push(tile);
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 22:52:57 +00:00
|
|
|
} */
|
2017-11-18 14:31:59 +00:00
|
|
|
|
|
|
|
return outputArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CullTiles;
|