Skip to content

Commit ffb49bd

Browse files
committed
Tilemaps.Components.StaggeredTileToWorldXY is a new function that converts staggered tile coordinates to world coordinates.
1 parent 4281bc1 commit ffb49bd

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 staggered tile XY coordinates (tile units) to world XY coordinates (pixels), 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.StaggeredTileToWorldXY
15+
* @since 3.50.0
16+
*
17+
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
18+
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
19+
* @param {Phaser.Math.Vector2} [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
20+
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
21+
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
22+
*
23+
* @return {Phaser.Math.Vector2} The XY location in world coordinates.
24+
*/
25+
var StaggeredTileToWorldXY = function (tileX, tileY, point, camera, layer)
26+
{
27+
if (point === undefined) { point = new Vector2(); }
28+
29+
var tileWidth = layer.baseTileWidth;
30+
var tileHeight = layer.baseTileHeight;
31+
var tilemapLayer = layer.tilemapLayer;
32+
33+
var layerWorldX = 0;
34+
var layerWorldY = 0;
35+
36+
if (tilemapLayer)
37+
{
38+
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
39+
40+
layerWorldX = tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX);
41+
42+
tileWidth *= tilemapLayer.scaleX;
43+
44+
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
45+
46+
tileHeight *= tilemapLayer.scaleY;
47+
}
48+
49+
var x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2);
50+
var y = layerWorldY + tileY * (tileHeight / 2);
51+
52+
return point.set(x, y);
53+
};
54+
55+
module.exports = StaggeredTileToWorldXY;

0 commit comments

Comments
 (0)