Skip to content

Commit 0b7d323

Browse files
committed
Tilemaps.Components.HexagonalTileToWorldY is a new function that converts a hexagonal Y coordinate to a world coordinate.
1 parent 0b63075 commit 0b7d323

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 hexagonal tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
9+
* layer's position, scale and scroll.
10+
*
11+
* @function Phaser.Tilemaps.Components.HexagonalTileToWorldY
12+
* @since 3.50.0
13+
*
14+
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
15+
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
16+
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
17+
*
18+
* @return {number} The Y location in world coordinates.
19+
*/
20+
var HexagonalTileToWorldY = function (tileY, camera, layer)
21+
{
22+
var tileHeight = layer.baseTileHeight;
23+
var tilemapLayer = layer.tilemapLayer;
24+
var layerWorldY = 0;
25+
26+
if (tilemapLayer)
27+
{
28+
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
29+
30+
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
31+
32+
tileHeight *= tilemapLayer.scaleY;
33+
}
34+
35+
var len = tilemapLayer.tilemap.hexSideLength;
36+
37+
var rowHeight = ((tileHeight - len) / 2 + len);
38+
39+
return layerWorldY + tileY * rowHeight;
40+
};
41+
42+
module.exports = HexagonalTileToWorldY;

0 commit comments

Comments
 (0)