mirror of
https://github.com/photonstorm/phaser
synced 2024-12-23 11:33:28 +00:00
changed world to tile and tile to world in isometric mode
This commit is contained in:
parent
c1c4aedd2f
commit
83bbf08902
8 changed files with 92 additions and 34 deletions
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue