wordtotileX or Y cannot be called independently either

This commit is contained in:
No 2020-01-17 15:15:15 +01:00
parent ddbcf85194
commit f17aef0abf
3 changed files with 46 additions and 13 deletions

View file

@ -15,6 +15,7 @@
* @param {integer} tileY - The x coordinate, in tiles, not pixels. * @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.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 {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {string} orientation - The Tilemap's orientation
* *
* @return {number} * @return {number}
*/ */
@ -33,7 +34,13 @@ var TileToWorldY = function (tileY, camera, layer)
tileHeight *= tilemapLayer.scaleY; tileHeight *= tilemapLayer.scaleY;
} }
if (orientation === "orthogonal") {
return layerWorldY + tileY * tileHeight; 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; module.exports = TileToWorldY;

View file

@ -31,8 +31,42 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer,
{ {
if (point === undefined) { point = new Vector2(0, 0); } if (point === undefined) { point = new Vector2(0, 0); }
if (orientation === "orthogonal") {
point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation); point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation);
point.y = WorldToTileY(worldY, 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; return point;
}; };

View file

@ -23,8 +23,6 @@
var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation) var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation)
{ {
if (snapToFloor === undefined) { snapToFloor = true; } if (snapToFloor === undefined) { snapToFloor = true; }
var tileWidth = layer.baseTileWidth;
var tileHeight = layer.baseTileHeight; var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
@ -38,11 +36,6 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation)
tileHeight *= tilemapLayer.scaleY; 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") { if (orientation === "orthogonal") {
@ -50,9 +43,8 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation)
? Math.floor(worldY / tileHeight) ? Math.floor(worldY / tileHeight)
: worldY / tileHeight; : worldY / tileHeight;
} else if (orientation === "isometric") { } else if (orientation === "isometric") {
return snapToFloor console.warn('With isometric map types you have to use the WorldToTileXY function.');
? Math.floor(worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2) return null
: (worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2);
} }
}; };