Skip to content

Commit f58ec09

Browse files
committed
Tilemaps.Components.CullBounds is a new function that calculates the cull bounds for an orthogonal map.
1 parent b11e5fe commit f58ec09

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var SnapCeil = require('../../math/snap/SnapCeil');
8+
var SnapFloor = require('../../math/snap/SnapFloor');
9+
10+
/**
11+
* Returns the bounds in the given orthogonal layer that are within the cameras viewport.
12+
* This is used internally by the cull tiles function.
13+
*
14+
* @function Phaser.Tilemaps.Components.CullBounds
15+
* @since 3.50.0
16+
*
17+
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
18+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to run the cull check against.
19+
*
20+
* @return {object} An object containing the `left`, `right`, `top` and `bottom` bounds.
21+
*/
22+
var CullBounds = function (layer, camera)
23+
{
24+
var tilemap = layer.tilemapLayer.tilemap;
25+
var tilemapLayer = layer.tilemapLayer;
26+
27+
// We need to use the tile sizes defined for the map as a whole, not the layer,
28+
// in order to calculate the bounds correctly. As different sized tiles may be
29+
// placed on the grid and we cannot trust layer.baseTileWidth to give us the true size.
30+
var tileW = Math.floor(tilemap.tileWidth * tilemapLayer.scaleX);
31+
var tileH = Math.floor(tilemap.tileHeight * tilemapLayer.scaleY);
32+
33+
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
34+
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
35+
36+
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
37+
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
38+
39+
return {
40+
left: boundsLeft,
41+
right: boundsRight,
42+
top: boundsTop,
43+
bottom: boundsBottom
44+
};
45+
};
46+
47+
module.exports = CullBounds;

0 commit comments

Comments
 (0)