Skip to content

Commit 7402d22

Browse files
committed
Tilemaps.Components.HexagonalWorldToTileXY is a new function that converts world coordinates to hexagonal tile coordinates.
1 parent 7d39bf9 commit 7402d22

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Vector2 = require('../../math/Vector2');
8+
9+
/**
10+
* Converts from world XY coordinates (pixels) to hexagonal tile XY coordinates (tile units), factoring in the
11+
* layer's position, scale and scroll. This will return a new Vector2 object or update the given
12+
* `point` object.
13+
*
14+
* @function Phaser.Tilemaps.Components.HexagonalWorldToTileXY
15+
* @since 3.50.0
16+
*
17+
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
18+
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
19+
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinates down to the nearest integer.
20+
* @param {Phaser.Math.Vector2} [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
21+
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
22+
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
23+
*
24+
* @return {Phaser.Math.Vector2} The XY location in tile units.
25+
*/
26+
var HexagonalWorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
27+
{
28+
if (snapToFloor === undefined) { snapToFloor = true; }
29+
if (point === undefined) { point = new Vector2(); }
30+
31+
var tileWidth = layer.baseTileWidth;
32+
var tileHeight = layer.baseTileHeight;
33+
var tilemapLayer = layer.tilemapLayer;
34+
35+
if (tilemapLayer)
36+
{
37+
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
38+
39+
// Find the world position relative to the static or dynamic layer's top left origin,
40+
// factoring in the camera's vertical scroll
41+
42+
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
43+
44+
tileHeight *= tilemapLayer.scaleY;
45+
46+
// Find the world position relative to the static or dynamic layer's top left origin,
47+
// factoring in the camera's horizontal scroll
48+
49+
worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX));
50+
51+
tileWidth *= tilemapLayer.scaleX;
52+
}
53+
54+
var len = layer.hexSideLength;
55+
var rowHeight = ((tileHeight - len) / 2 + len);
56+
57+
// similar to staggered, because Tiled uses the oddr representation.
58+
var y = (snapToFloor) ? Math.floor((worldY / rowHeight)) : (worldY / rowHeight);
59+
var x = (snapToFloor) ? Math.floor((worldX - (y % 2) * 0.5 * tileWidth) / tileWidth) : (worldX - (y % 2) * 0.5 * tileWidth) / tileWidth;
60+
61+
return point.set(x, y);
62+
};
63+
64+
module.exports = HexagonalWorldToTileXY;

0 commit comments

Comments
 (0)