Skip to content

Commit cb09374

Browse files
committed
Tilemaps.Components.StaggeredWorldToTileY is a new function that converts world Y coordinates from pixels to staggered tile Y coordinates.
1 parent 17df0f7 commit cb09374

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
/**
8+
* Converts from world Y coordinates (pixels) to staggered tile Y coordinates (tile units), factoring in the
9+
* layers position, scale and scroll.
10+
*
11+
* @function Phaser.Tilemaps.Components.StaggeredWorldToTileY
12+
* @since 3.50.0
13+
*
14+
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
15+
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
16+
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
17+
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
18+
*
19+
* @return {number} The Y location in tile units.
20+
*/
21+
var StaggeredWorldToTileY = function (worldY, snapToFloor, camera, layer)
22+
{
23+
if (snapToFloor === undefined) { snapToFloor = true; }
24+
25+
var tileHeight = layer.baseTileHeight;
26+
var tilemapLayer = layer.tilemapLayer;
27+
28+
if (tilemapLayer)
29+
{
30+
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
31+
32+
// Find the world position relative to the static or dynamic layer's top left origin,
33+
// factoring in the camera's vertical scroll
34+
35+
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
36+
37+
tileHeight *= tilemapLayer.scaleY;
38+
}
39+
40+
return snapToFloor
41+
? Math.floor(worldY / (tileHeight / 2))
42+
: worldY / (tileHeight / 2);
43+
};
44+
45+
module.exports = StaggeredWorldToTileY;

0 commit comments

Comments
 (0)