From f17aef0abf35178cb2d1272d2964791a261e93e4 Mon Sep 17 00:00:00 2001 From: No Date: Fri, 17 Jan 2020 15:15:15 +0100 Subject: [PATCH] wordtotileX or Y cannot be called independently either --- src/tilemaps/components/TileToWorldY.js | 9 +++++- src/tilemaps/components/WorldToTileXY.js | 38 ++++++++++++++++++++++-- src/tilemaps/components/WorldToTileY.js | 12 ++------ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/tilemaps/components/TileToWorldY.js b/src/tilemaps/components/TileToWorldY.js index 88fa8089f..ac6e4c273 100644 --- a/src/tilemaps/components/TileToWorldY.js +++ b/src/tilemaps/components/TileToWorldY.js @@ -15,6 +15,7 @@ * @param {integer} tileY - The x coordinate, in tiles, not pixels. * @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values. * @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon. + * @param {string} orientation - The Tilemap's orientation * * @return {number} */ @@ -33,7 +34,13 @@ var TileToWorldY = function (tileY, camera, layer) tileHeight *= tilemapLayer.scaleY; } - return layerWorldY + tileY * tileHeight; + if (orientation === "orthogonal") { + return layerWorldY + tileY * tileHeight; + } else if (orientation === "isometric") { + // Not Best Solution ? + console.warn('With isometric map types you have to use the TileToWorldXY function.'); + return null; + } }; module.exports = TileToWorldY; diff --git a/src/tilemaps/components/WorldToTileXY.js b/src/tilemaps/components/WorldToTileXY.js index e4471c918..180090ac4 100644 --- a/src/tilemaps/components/WorldToTileXY.js +++ b/src/tilemaps/components/WorldToTileXY.js @@ -31,8 +31,42 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer, { if (point === undefined) { point = new Vector2(0, 0); } - point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation); - point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation); + if (orientation === "orthogonal") { + point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation); + point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation); + } else if (orientation == 'isometric') { + + var tileWidth = layer.baseTileWidth; + var tileHeight = layer.baseTileHeight; + var tilemapLayer = layer.tilemapLayer; + + if (tilemapLayer) + { + if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; } + + // Find the world position relative to the static or dynamic layer's top left origin, + // factoring in the camera's vertical scroll + worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY)); + + tileHeight *= tilemapLayer.scaleY; + + // Find the world position relative to the static or dynamic layer's top left origin, + // factoring in the camera's horizontal scroll + worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX)); + + tileWidth *= tilemapLayer.scaleX; + } + + point.x = snapToFloor + ? Math.floor((worldX/(tileWidth/2) + worldY/(tileHeight/2))/2) + : ((worldX/(tileWidth/2) + worldY/(tileHeight/2))/2); + + point.y = snapToFloor + ? Math.floor(worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2) + : (worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2); + } + + return point; }; diff --git a/src/tilemaps/components/WorldToTileY.js b/src/tilemaps/components/WorldToTileY.js index 6bd1454f5..4218587b4 100644 --- a/src/tilemaps/components/WorldToTileY.js +++ b/src/tilemaps/components/WorldToTileY.js @@ -23,8 +23,6 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation) { if (snapToFloor === undefined) { snapToFloor = true; } - - var tileWidth = layer.baseTileWidth; var tileHeight = layer.baseTileHeight; var tilemapLayer = layer.tilemapLayer; @@ -38,11 +36,6 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation) tileHeight *= tilemapLayer.scaleY; - // Find the world position relative to the static or dynamic layer's top left origin, - // factoring in the camera's horizontal scroll - worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX)); - - tileWidth *= tilemapLayer.scaleX; } if (orientation === "orthogonal") { @@ -50,9 +43,8 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation) ? Math.floor(worldY / tileHeight) : worldY / tileHeight; } else if (orientation === "isometric") { - return snapToFloor - ? Math.floor(worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2) - : (worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2); + console.warn('With isometric map types you have to use the WorldToTileXY function.'); + return null } };