/
StaggeredCullBounds.js
47 lines (40 loc) · 1.92 KB
/
StaggeredCullBounds.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var SnapCeil = require('../../math/snap/SnapCeil');
var SnapFloor = require('../../math/snap/SnapFloor');
/**
* Returns the bounds in the given layer that are within the camera's viewport.
* This is used internally by the cull tiles function.
*
* @function Phaser.Tilemaps.Components.StaggeredCullBounds
* @since 3.50.0
*
* @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.
*
* @return {object} An object containing the `left`, `right`, `top` and `bottom` bounds.
*/
var StaggeredCullBounds = function (layer, camera)
{
var tilemap = layer.tilemapLayer.tilemap;
var tilemapLayer = layer.tilemapLayer;
// 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);
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;
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH / 2, 0, true) - tilemapLayer.cullPaddingY;
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH / 2, 0, true) + tilemapLayer.cullPaddingY;
return {
left: boundsLeft,
right: boundsRight,
top: boundsTop,
bottom: boundsBottom
};
};
module.exports = StaggeredCullBounds;