changed world to tile and tile to world in isometric mode

This commit is contained in:
No 2020-01-17 14:47:33 +01:00
parent c1c4aedd2f
commit 83bbf08902
8 changed files with 92 additions and 34 deletions

View file

@ -23,10 +23,11 @@ var WorldToTileY = require('./WorldToTileY');
* *
* @return {?boolean} Returns a boolean, or null if the layer given was invalid. * @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 layer = stlayer.layer
var tileY = WorldToTileY(worldY, true, camera, layer); var tileX = WorldToTileX(worldX, true, camera, layer, orientation);
var tileY = WorldToTileY(worldY, true, camera, layer, orientation);
return HasTileAt(tileX, tileY, layer); return HasTileAt(tileX, tileY, layer);
}; };

View file

@ -15,10 +15,11 @@
* @param {integer} tileX - The x coordinate, in tiles, not pixels. * @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.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}
*/ */
var TileToWorldX = function (tileX, camera, layer) var TileToWorldX = function (tileX, camera, layer, orientation)
{ {
var tileWidth = layer.baseTileWidth; var tileWidth = layer.baseTileWidth;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
@ -33,7 +34,16 @@ var TileToWorldX = function (tileX, camera, layer)
tileWidth *= tilemapLayer.scaleX; 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; module.exports = TileToWorldX;

View file

@ -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.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.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 {Phaser.Math.Vector2} The XY location in world coordinates. * @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); } if (point === undefined) { point = new Vector2(0, 0); }
point.x = TileToWorldX(tileX, camera, layer); if (orientation === "orthogonal") {
point.y = TileToWorldY(tileY, camera, layer); 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; return point;
}; };

View file

@ -16,20 +16,30 @@
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer. * @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.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} The X location in tile units. * @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; } if (snapToFloor === undefined) { snapToFloor = true; }
var tileWidth = layer.baseTileWidth; var tileWidth = layer.baseTileWidth;
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
if (tilemapLayer) if (tilemapLayer)
{ {
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; } 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, // Find the world position relative to the static or dynamic layer's top left origin,
// factoring in the camera's horizontal scroll // factoring in the camera's horizontal scroll
worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX)); worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX));
@ -37,9 +47,17 @@ var WorldToTileX = function (worldX, snapToFloor, camera, layer)
tileWidth *= tilemapLayer.scaleX; tileWidth *= tilemapLayer.scaleX;
} }
return snapToFloor if (orientation === "orthogonal") {
? Math.floor(worldX / tileWidth) return snapToFloor
: worldX / tileWidth; ? 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; module.exports = WorldToTileX;

View file

@ -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.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 {Phaser.Math.Vector2} The XY location in tile units. * @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); } if (point === undefined) { point = new Vector2(0, 0); }
point.x = WorldToTileX(worldX, snapToFloor, camera, layer); point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation);
point.y = WorldToTileY(worldY, snapToFloor, camera, layer); point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation);
return point; return point;
}; };

View file

@ -16,16 +16,18 @@
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer. * @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.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} The Y location in tile units. * @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; } 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;
if (tilemapLayer) if (tilemapLayer)
{ {
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; } 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)); worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
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;
} }
return snapToFloor if (orientation === "orthogonal") {
? Math.floor(worldY / tileHeight) return snapToFloor
: worldY / tileHeight; ? 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; module.exports = WorldToTileY;

View file

@ -32,11 +32,11 @@ var AssignTileProperties = require('./AssignTileProperties');
*/ */
var ParseJSONTiled = function (name, json, insertNull) var ParseJSONTiled = function (name, json, insertNull)
{ {
if (json.orientation !== 'orthogonal') // if (json.orientation !== 'orthogonal')
{ // {
console.warn('Only orthogonal map types are supported in this version of Phaser'); // console.warn('Only orthogonal map types are supported in this version of Phaser');
return null; // return null;
} // }
// Map data will consist of: layers, objects, images, tilesets, sizes // Map data will consist of: layers, objects, images, tilesets, sizes
var mapData = new MapData({ var mapData = new MapData({

View file

@ -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. * 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 * @extends Phaser.GameObjects.GameObject
* @memberof Phaser.Tilemaps * @memberof Phaser.Tilemaps
* @constructor * @constructor
@ -1124,7 +1124,8 @@ var StaticTilemapLayer = new Class({
*/ */
hasTileAtWorldXY: function (worldX, worldY, camera) 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. * layers position, scale and scroll.
* *
* @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldX * @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldX
* @since 3.0.0 * @since 3.0.0
* *
* @param {integer} tileX - The X coordinate, in tile coordinates. * @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. * @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the world values from the tile index.
* *
* @return {number} * @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. * layers position, scale and scroll.
* *
* @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldY * @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldY
* @since 3.0.0 * @since 3.0.0
* *
* @param {integer} tileY - The Y coordinate, in tile coordinates. * @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. * @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the world values from the tile index.
* *
* @return {number} * @return {number}
@ -1389,7 +1392,7 @@ var StaticTilemapLayer = new Class({
*/ */
worldToTileX: function (worldX, snapToFloor, camera) 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) 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) 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);
}, },
/** /**