|
| 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 staggered 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.StaggeredWorldToTileXY |
| 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 coordinate 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 StaggeredWorldToTileXY = 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 y = (snapToFloor) ? Math.floor((worldY / (tileHeight / 2))) : (worldY / (tileHeight / 2)); |
| 55 | + var x = (snapToFloor) ? Math.floor((worldX + (y % 2) * 0.5 * tileWidth) / tileWidth) : (worldX + (y % 2) * 0.5 * tileWidth) / tileWidth; |
| 56 | + |
| 57 | + return point.set(x, y); |
| 58 | +}; |
| 59 | + |
| 60 | +module.exports = StaggeredWorldToTileXY; |
0 commit comments