From 83bbf089027792fe68c0949a9b351b0872480386 Mon Sep 17 00:00:00 2001 From: No Date: Fri, 17 Jan 2020 14:47:33 +0100 Subject: [PATCH] changed world to tile and tile to world in isometric mode --- src/tilemaps/components/HasTileAtWorldXY.js | 7 ++--- src/tilemaps/components/TileToWorldX.js | 14 ++++++++-- src/tilemaps/components/TileToWorldXY.js | 18 ++++++++++--- src/tilemaps/components/WorldToTileX.js | 26 +++++++++++++++--- src/tilemaps/components/WorldToTileXY.js | 7 ++--- src/tilemaps/components/WorldToTileY.js | 27 ++++++++++++++----- src/tilemaps/parsers/tiled/ParseJSONTiled.js | 10 +++---- .../staticlayer/StaticTilemapLayer.js | 17 +++++++----- 8 files changed, 92 insertions(+), 34 deletions(-) diff --git a/src/tilemaps/components/HasTileAtWorldXY.js b/src/tilemaps/components/HasTileAtWorldXY.js index 784a6c9dc..135c12544 100644 --- a/src/tilemaps/components/HasTileAtWorldXY.js +++ b/src/tilemaps/components/HasTileAtWorldXY.js @@ -23,10 +23,11 @@ var WorldToTileY = require('./WorldToTileY'); * * @return {?boolean} Returns a boolean, or null if the layer given was invalid. */ -var HasTileAtWorldXY = function (worldX, worldY, camera, layer) +var HasTileAtWorldXY = function (worldX, worldY, camera, orientation) { - var tileX = WorldToTileX(worldX, true, camera, layer); - var tileY = WorldToTileY(worldY, true, camera, layer); + var layer = stlayer.layer + var tileX = WorldToTileX(worldX, true, camera, layer, orientation); + var tileY = WorldToTileY(worldY, true, camera, layer, orientation); return HasTileAt(tileX, tileY, layer); }; diff --git a/src/tilemaps/components/TileToWorldX.js b/src/tilemaps/components/TileToWorldX.js index 97654412a..a95722d85 100644 --- a/src/tilemaps/components/TileToWorldX.js +++ b/src/tilemaps/components/TileToWorldX.js @@ -15,10 +15,11 @@ * @param {integer} tileX - 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} */ -var TileToWorldX = function (tileX, camera, layer) +var TileToWorldX = function (tileX, camera, layer, orientation) { var tileWidth = layer.baseTileWidth; var tilemapLayer = layer.tilemapLayer; @@ -33,7 +34,16 @@ var TileToWorldX = function (tileX, camera, layer) tileWidth *= tilemapLayer.scaleX; } - return layerWorldX + tileX * tileWidth; + + if (orientation === "orthogonal") { + return layerWorldX + tileX * tileWidth; + } else if (orientation === "isometric") { + // Not Best Solution ? + console.warn('With isometric map types you have to use the TileToWorldXY function.'); + return null; + } + + }; module.exports = TileToWorldX; diff --git a/src/tilemaps/components/TileToWorldXY.js b/src/tilemaps/components/TileToWorldXY.js index 0822d5d96..70e89a7ab 100644 --- a/src/tilemaps/components/TileToWorldXY.js +++ b/src/tilemaps/components/TileToWorldXY.js @@ -22,16 +22,26 @@ var Vector2 = require('../../math/Vector2'); * @param {Phaser.Math.Vector2} [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created. * @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 {Phaser.Math.Vector2} The XY location in world coordinates. */ -var TileToWorldXY = function (tileX, tileY, point, camera, layer) +var TileToWorldXY = function (tileX, tileY, point, camera, layer, orientation) { + var tileWidth = layer.baseTileWidth; + var tileHeight = layer.baseTileHeight; + if (point === undefined) { point = new Vector2(0, 0); } - point.x = TileToWorldX(tileX, camera, layer); - point.y = TileToWorldY(tileY, camera, layer); - + if (orientation === "orthogonal") { + point.x = TileToWorldX(tileX, camera, layer, orientation); + point.y = TileToWorldY(tileY, camera, layer, orientation); + } else if (orientation === "isometric") { + point.x = (WorldX - WorldY) * (tileWidth/2); + point.y = (WorldX + WorldY) * (tileHeight/2); + + } + return point; }; diff --git a/src/tilemaps/components/WorldToTileX.js b/src/tilemaps/components/WorldToTileX.js index cfbc2a2bf..b3e23bbeb 100644 --- a/src/tilemaps/components/WorldToTileX.js +++ b/src/tilemaps/components/WorldToTileX.js @@ -16,20 +16,30 @@ * @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer. * @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} The X location in tile units. */ -var WorldToTileX = function (worldX, snapToFloor, camera, layer) +var WorldToTileX = function (worldX, snapToFloor, camera, layer, orientation) { + + if (snapToFloor === undefined) { snapToFloor = true; } 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)); @@ -37,9 +47,17 @@ var WorldToTileX = function (worldX, snapToFloor, camera, layer) tileWidth *= tilemapLayer.scaleX; } - return snapToFloor - ? Math.floor(worldX / tileWidth) - : worldX / tileWidth; + if (orientation === "orthogonal") { + return snapToFloor + ? Math.floor(worldX / tileWidth) + : worldX / tileWidth; + } else if (orientation === "isometric") { + return snapToFloor + ? Math.floor((worldX/(tileWidth/2) + worldY/(tileHeight/2))/2) + : ((worldX/(tileWidth/2) + worldY/(tileHeight/2))/2); + + } + }; module.exports = WorldToTileX; diff --git a/src/tilemaps/components/WorldToTileXY.js b/src/tilemaps/components/WorldToTileXY.js index 424c2161c..e4471c918 100644 --- a/src/tilemaps/components/WorldToTileXY.js +++ b/src/tilemaps/components/WorldToTileXY.js @@ -24,14 +24,15 @@ var Vector2 = require('../../math/Vector2'); * @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 {Phaser.Math.Vector2} The XY location in tile units. */ -var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer) +var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer,orientation) { if (point === undefined) { point = new Vector2(0, 0); } - point.x = WorldToTileX(worldX, snapToFloor, camera, layer); - point.y = WorldToTileY(worldY, snapToFloor, camera, layer); + point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation); + point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation); return point; }; diff --git a/src/tilemaps/components/WorldToTileY.js b/src/tilemaps/components/WorldToTileY.js index 37124f8de..6bd1454f5 100644 --- a/src/tilemaps/components/WorldToTileY.js +++ b/src/tilemaps/components/WorldToTileY.js @@ -16,16 +16,18 @@ * @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer. * @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} The Y location in tile units. */ -var WorldToTileY = function (worldY, snapToFloor, camera, layer) +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; - + if (tilemapLayer) { if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; } @@ -35,11 +37,24 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer) 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; } - return snapToFloor - ? Math.floor(worldY / tileHeight) - : worldY / tileHeight; + if (orientation === "orthogonal") { + return snapToFloor + ? 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); + + } }; module.exports = WorldToTileY; diff --git a/src/tilemaps/parsers/tiled/ParseJSONTiled.js b/src/tilemaps/parsers/tiled/ParseJSONTiled.js index f0fb829ed..28fb62ac8 100644 --- a/src/tilemaps/parsers/tiled/ParseJSONTiled.js +++ b/src/tilemaps/parsers/tiled/ParseJSONTiled.js @@ -32,11 +32,11 @@ var AssignTileProperties = require('./AssignTileProperties'); */ var ParseJSONTiled = function (name, json, insertNull) { - if (json.orientation !== 'orthogonal') - { - console.warn('Only orthogonal map types are supported in this version of Phaser'); - return null; - } + // if (json.orientation !== 'orthogonal') + // { + // console.warn('Only orthogonal map types are supported in this version of Phaser'); + // return null; + // } // Map data will consist of: layers, objects, images, tilesets, sizes var mapData = new MapData({ diff --git a/src/tilemaps/staticlayer/StaticTilemapLayer.js b/src/tilemaps/staticlayer/StaticTilemapLayer.js index cf16c7934..b31f31b9f 100644 --- a/src/tilemaps/staticlayer/StaticTilemapLayer.js +++ b/src/tilemaps/staticlayer/StaticTilemapLayer.js @@ -23,7 +23,7 @@ var Utils = require('../../renderer/webgl/Utils'); * * Use a Static Tilemap Layer instead of a Dynamic Tilemap Layer when you don't need tile manipulation features. * - * @class StaticTilemapLayer + * @class StaticTilemapLayers * @extends Phaser.GameObjects.GameObject * @memberof Phaser.Tilemaps * @constructor @@ -1124,7 +1124,8 @@ var StaticTilemapLayer = new Class({ */ hasTileAtWorldXY: function (worldX, worldY, camera) { - return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, this.layer); + + return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, this.tilemap.orientation); }, /** @@ -1320,13 +1321,14 @@ var StaticTilemapLayer = new Class({ }, /** - * Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the + * Converts from tile X and Y coordinates (tile units) to world X coordinates (pixels), factoring in the * layers position, scale and scroll. * * @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldX * @since 3.0.0 * * @param {integer} tileX - The X coordinate, in tile coordinates. + * @param {integer} tileY - The Y coordinate, in tile coordinates. * @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the world values from the tile index. * * @return {number} @@ -1337,13 +1339,14 @@ var StaticTilemapLayer = new Class({ }, /** - * Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the + * Converts from tile X and Y coordinates (tile units) to world Y coordinates (pixels), factoring in the * layers position, scale and scroll. * * @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldY * @since 3.0.0 * * @param {integer} tileY - The Y coordinate, in tile coordinates. + * @param {integer} tileY - The Y coordinate, in tile coordinates. * @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the world values from the tile index. * * @return {number} @@ -1389,7 +1392,7 @@ var StaticTilemapLayer = new Class({ */ worldToTileX: function (worldX, snapToFloor, camera) { - return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer); + return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer, this.tilemap.orientation); }, /** @@ -1408,7 +1411,7 @@ var StaticTilemapLayer = new Class({ */ worldToTileY: function (worldY, snapToFloor, camera) { - return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer); + return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer, this.tilemap.orientation); }, /** @@ -1430,7 +1433,7 @@ var StaticTilemapLayer = new Class({ */ worldToTileXY: function (worldX, worldY, snapToFloor, point, camera) { - return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, this.layer); + return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, this.layer, this.tilemap.orientation); }, /**