From 85e75eafa8b8e0cbf2eef522f5bb40054dfadea9 Mon Sep 17 00:00:00 2001 From: Michael Hadley Date: Thu, 30 Nov 2017 09:22:54 -0600 Subject: [PATCH] Fix for coord transform between world <-> tile + expose tile -> world methods --- v3/src/gameobjects/tilemap/Tilemap.js | 42 +++++++++++++++++-- .../tilemap/components/TileToWorldX.js | 6 +-- .../tilemap/components/TileToWorldXY.js | 27 ++++++++++++ .../tilemap/components/TileToWorldY.js | 6 +-- .../tilemap/components/WorldToTileX.js | 2 +- .../tilemap/components/WorldToTileY.js | 2 +- .../gameobjects/tilemap/components/index.js | 3 ++ .../dynamiclayer/DynamicTilemapLayer.js | 30 +++++++++++++ .../tilemap/staticlayer/StaticTilemapLayer.js | 32 +++++++++++++- 9 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 v3/src/gameobjects/tilemap/components/TileToWorldXY.js diff --git a/v3/src/gameobjects/tilemap/Tilemap.js b/v3/src/gameobjects/tilemap/Tilemap.js index e791c4652..12d477411 100644 --- a/v3/src/gameobjects/tilemap/Tilemap.js +++ b/v3/src/gameobjects/tilemap/Tilemap.js @@ -1157,7 +1157,43 @@ var Tilemap = new Class({ /** * See component documentation. If no layer specified, the map's current layer is used. * - * @return {number|null} Returns this, or null if the layer given was invalid. + * @return {number|null} Returns a number, or null if the layer given was invalid. + */ + tileToWorldX: function (tileX, camera, layer) + { + layer = this.getLayer(layer); + if (layer === null) { return null; } + return TilemapComponents.TileToWorldX(tileX, camera, layer); + }, + + /** + * See component documentation. If no layer specified, the map's current layer is used. + * + * @return {number|null} Returns a number, or null if the layer given was invalid. + */ + tileToWorldY: function (tileX, camera, layer) + { + layer = this.getLayer(layer); + if (layer === null) { return null; } + return TilemapComponents.TileToWorldY(tileX, camera, layer); + }, + + /** + * See component documentation. If no layer specified, the map's current layer is used. + * + * @return {Vector2|null} Returns a point, or null if the layer given was invalid. + */ + tileToWorldXY: function (tileX, tileY, point, camera, layer) + { + layer = this.getLayer(layer); + if (layer === null) { return null; } + return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, layer); + }, + + /** + * See component documentation. If no layer specified, the map's current layer is used. + * + * @return {number|null} Returns a number, or null if the layer given was invalid. */ worldToTileX: function (worldX, snapToFloor, camera, layer) { @@ -1169,7 +1205,7 @@ var Tilemap = new Class({ /** * See component documentation. If no layer specified, the map's current layer is used. * - * @return {number|null} Returns this, or null if the layer given was invalid. + * @return {number|null} Returns a number, or null if the layer given was invalid. */ worldToTileY: function (worldY, snapToFloor, camera, layer) { @@ -1181,7 +1217,7 @@ var Tilemap = new Class({ /** * See component documentation. If no layer specified, the map's current layer is used. * - * @return {Vector|null} Returns this, or null if the layer given was invalid. + * @return {Vector2|null} Returns a point, or null if the layer given was invalid. */ worldToTileXY: function (worldX, worldY, snapToFloor, point, camera, layer) { diff --git a/v3/src/gameobjects/tilemap/components/TileToWorldX.js b/v3/src/gameobjects/tilemap/components/TileToWorldX.js index 6776e1a40..306298206 100644 --- a/v3/src/gameobjects/tilemap/components/TileToWorldX.js +++ b/v3/src/gameobjects/tilemap/components/TileToWorldX.js @@ -1,6 +1,6 @@ /** - * Internally used method to convert from tile X coordinates to world X coordinates, factoring in - * layer position, scale and scroll. + * Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the + * layer's position, scale and scroll. * * @param {integer} tileX - [description] * @param {Camera} [camera=main camera] - [description] @@ -17,7 +17,7 @@ var TileToWorldX = function (tileX, camera, layer) { if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; } - layerWorldX = tilemapLayer.x - (camera.scrollX * tilemapLayer.scrollFactorX); + layerWorldX = tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX); tileWidth *= tilemapLayer.scaleX; } diff --git a/v3/src/gameobjects/tilemap/components/TileToWorldXY.js b/v3/src/gameobjects/tilemap/components/TileToWorldXY.js new file mode 100644 index 000000000..664fd88c9 --- /dev/null +++ b/v3/src/gameobjects/tilemap/components/TileToWorldXY.js @@ -0,0 +1,27 @@ +var TileToWorldX = require('./TileToWorldX'); +var TileToWorldY = require('./TileToWorldY'); +var Vector2 = require('../../../math/Vector2'); + +/** + * Converts from tile XY coordinates (tile units) to world XY coordinates (pixels), factoring in the + * layer's position, scale and scroll. This will return a new Vector2 object or update the given + * `point` object. + * + * @param {integer} tileX - [description] + * @param {integer} tileY - [description] + * @param {Vector2} [point] - [description] + * @param {Camera} [camera=main camera] - [description] + * @param {LayerData} layer - [description] + * @returns {Vector2} The XY location in world coordinates. + */ +var TileToWorldXY = function (tileX, tileY, point, camera, layer) +{ + if (point === undefined) { point = new Vector2(0, 0); } + + point.x = TileToWorldX(tileX, camera, layer); + point.y = TileToWorldY(tileY, camera, layer); + + return point; +}; + +module.exports = TileToWorldXY; diff --git a/v3/src/gameobjects/tilemap/components/TileToWorldY.js b/v3/src/gameobjects/tilemap/components/TileToWorldY.js index d0a318e5c..8e1f5ea3b 100644 --- a/v3/src/gameobjects/tilemap/components/TileToWorldY.js +++ b/v3/src/gameobjects/tilemap/components/TileToWorldY.js @@ -1,6 +1,6 @@ /** - * Internally used method to convert from tile Y coordinates to world Y coordinates, factoring in - * layer position, scale and scroll. + * Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the + * layer's position, scale and scroll. * * @param {integer} tileY - [description] * @param {Camera} [camera=main camera] - [description] @@ -17,7 +17,7 @@ var TileToWorldY = function (tileY, camera, layer) { if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; } - layerWorldY = tilemapLayer.y - (camera.scrollY * tilemapLayer.scrollFactorY); + layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY)); tileHeight *= tilemapLayer.scaleY; } diff --git a/v3/src/gameobjects/tilemap/components/WorldToTileX.js b/v3/src/gameobjects/tilemap/components/WorldToTileX.js index b248b81ce..91dd25dc7 100644 --- a/v3/src/gameobjects/tilemap/components/WorldToTileX.js +++ b/v3/src/gameobjects/tilemap/components/WorldToTileX.js @@ -22,7 +22,7 @@ var WorldToTileX = function (worldX, snapToFloor, camera, layer) // Find the world position relative to the static or dynamic layer's top left origin, // factoring in the camera's horizontal scroll - worldX = worldX + (camera.scrollX * tilemapLayer.scrollFactorX) - tilemapLayer.x; + worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX)); tileWidth *= tilemapLayer.scaleX; } diff --git a/v3/src/gameobjects/tilemap/components/WorldToTileY.js b/v3/src/gameobjects/tilemap/components/WorldToTileY.js index bab14494e..fbbc171e0 100644 --- a/v3/src/gameobjects/tilemap/components/WorldToTileY.js +++ b/v3/src/gameobjects/tilemap/components/WorldToTileY.js @@ -22,7 +22,7 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer) // Find the world position relative to the static or dynamic layer's top left origin, // factoring in the camera's vertical scroll - worldY = worldY + (camera.scrollY * tilemapLayer.scrollFactorY) - tilemapLayer.y; + worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY)); tileHeight *= tilemapLayer.scaleY; } diff --git a/v3/src/gameobjects/tilemap/components/index.js b/v3/src/gameobjects/tilemap/components/index.js index b1a4133b4..3a650d624 100644 --- a/v3/src/gameobjects/tilemap/components/index.js +++ b/v3/src/gameobjects/tilemap/components/index.js @@ -31,6 +31,9 @@ module.exports = { SetTileLocationCallback: require('./SetTileLocationCallback'), Shuffle: require('./Shuffle'), SwapByIndex: require('./SwapByIndex'), + TileToWorldX: require('./TileToWorldX'), + TileToWorldXY: require('./TileToWorldXY'), + TileToWorldY: require('./TileToWorldY'), WorldToTileX: require('./WorldToTileX'), WorldToTileXY: require('./WorldToTileXY'), WorldToTileY: require('./WorldToTileY') diff --git a/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js b/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js index 8e4e26036..d62e2231a 100644 --- a/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js +++ b/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js @@ -425,6 +425,36 @@ var DynamicTilemapLayer = new Class({ return this; }, + /** + * See component documentation. + * + * @return {number} + */ + tileToWorldX: function (tileX, camera) + { + return TilemapComponents.TileToWorldX(tileX, camera, this.layer); + }, + + /** + * See component documentation. + * + * @return {number} + */ + tileToWorldY: function (tileY, camera) + { + return TilemapComponents.TileToWorldY(tileY, camera, this.layer); + }, + + /** + * See component documentation. + * + * @return {Vector2} + */ + tileToWorldXY: function (tileX, tileY, point, camera) + { + return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, this.layer); + }, + /** * See component documentation. * diff --git a/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js b/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js index bd1871e36..84303bd70 100644 --- a/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js +++ b/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js @@ -469,6 +469,36 @@ var StaticTilemapLayer = new Class({ return this; }, + /** + * See component documentation. + * + * @return {number} + */ + tileToWorldX: function (tileX, camera) + { + return TilemapComponents.TileToWorldX(tileX, camera, this.layer); + }, + + /** + * See component documentation. + * + * @return {number} + */ + tileToWorldY: function (tileY, camera) + { + return TilemapComponents.TileToWorldY(tileY, camera, this.layer); + }, + + /** + * See component documentation. + * + * @return {Vector2} + */ + tileToWorldXY: function (tileX, tileY, point, camera) + { + return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, this.layer); + }, + /** * See component documentation. * @@ -492,7 +522,7 @@ var StaticTilemapLayer = new Class({ /** * See component documentation. * - * @return {Vector} + * @return {Vector2} */ worldToTileXY: function (worldX, worldY, snapToFloor, point, camera) {