Skip to content

Commit e42b407

Browse files
committed
Tilemaps.Components.HexagonalCullBounds is a new function that calculates the cull bounds for a hexagonal map.
1 parent 2450a16 commit e42b407

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 layer that are within the camera's viewport.
12+
* This is used internally by the cull tiles function.
13+
*
14+
* @function Phaser.Tilemaps.Components.HexagonalCullBounds
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 HexagonalCullBounds = 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 len = layer.hexSideLength;
34+
var rowH = ((tileH - len) / 2 + len);
35+
36+
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
37+
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
38+
39+
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, rowH, 0, true) - tilemapLayer.cullPaddingY;
40+
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, rowH, 0, true) + tilemapLayer.cullPaddingY;
41+
42+
return {
43+
left: boundsLeft,
44+
right: boundsRight,
45+
top: boundsTop,
46+
bottom: boundsBottom
47+
};
48+
};
49+
50+
module.exports = HexagonalCullBounds;

0 commit comments

Comments
 (0)