Skip to content

Commit 83bbf08

Browse files
committed
changed world to tile and tile to world in isometric mode
1 parent c1c4aed commit 83bbf08

8 files changed

Lines changed: 92 additions & 34 deletions

File tree

src/tilemaps/components/HasTileAtWorldXY.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ var WorldToTileY = require('./WorldToTileY');
2323
*
2424
* @return {?boolean} Returns a boolean, or null if the layer given was invalid.
2525
*/
26-
var HasTileAtWorldXY = function (worldX, worldY, camera, layer)
26+
var HasTileAtWorldXY = function (worldX, worldY, camera, orientation)
2727
{
28-
var tileX = WorldToTileX(worldX, true, camera, layer);
29-
var tileY = WorldToTileY(worldY, true, camera, layer);
28+
var layer = stlayer.layer
29+
var tileX = WorldToTileX(worldX, true, camera, layer, orientation);
30+
var tileY = WorldToTileY(worldY, true, camera, layer, orientation);
3031

3132
return HasTileAt(tileX, tileY, layer);
3233
};

src/tilemaps/components/TileToWorldX.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
1616
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
1717
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
18+
* @param {string} orientation - The Tilemap's orientation
1819
*
1920
* @return {number}
2021
*/
21-
var TileToWorldX = function (tileX, camera, layer)
22+
var TileToWorldX = function (tileX, camera, layer, orientation)
2223
{
2324
var tileWidth = layer.baseTileWidth;
2425
var tilemapLayer = layer.tilemapLayer;
@@ -33,7 +34,16 @@ var TileToWorldX = function (tileX, camera, layer)
3334
tileWidth *= tilemapLayer.scaleX;
3435
}
3536

36-
return layerWorldX + tileX * tileWidth;
37+
38+
if (orientation === "orthogonal") {
39+
return layerWorldX + tileX * tileWidth;
40+
} else if (orientation === "isometric") {
41+
// Not Best Solution ?
42+
console.warn('With isometric map types you have to use the TileToWorldXY function.');
43+
return null;
44+
}
45+
46+
3747
};
3848

3949
module.exports = TileToWorldX;

src/tilemaps/components/TileToWorldXY.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,26 @@ var Vector2 = require('../../math/Vector2');
2222
* @param {Phaser.Math.Vector2} [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
2323
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
2424
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
25+
* @param {string} orientation - The Tilemap's orientation
2526
*
2627
* @return {Phaser.Math.Vector2} The XY location in world coordinates.
2728
*/
28-
var TileToWorldXY = function (tileX, tileY, point, camera, layer)
29+
var TileToWorldXY = function (tileX, tileY, point, camera, layer, orientation)
2930
{
30-
if (point === undefined) { point = new Vector2(0, 0); }
31+
var tileWidth = layer.baseTileWidth;
32+
var tileHeight = layer.baseTileHeight;
3133

32-
point.x = TileToWorldX(tileX, camera, layer);
33-
point.y = TileToWorldY(tileY, camera, layer);
34+
if (point === undefined) { point = new Vector2(0, 0); }
3435

36+
if (orientation === "orthogonal") {
37+
point.x = TileToWorldX(tileX, camera, layer, orientation);
38+
point.y = TileToWorldY(tileY, camera, layer, orientation);
39+
} else if (orientation === "isometric") {
40+
point.x = (WorldX - WorldY) * (tileWidth/2);
41+
point.y = (WorldX + WorldY) * (tileHeight/2);
42+
43+
}
44+
3545
return point;
3646
};
3747

src/tilemaps/components/WorldToTileX.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,48 @@
1616
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
1717
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
1818
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
19+
* @param {string} orientation - The Tilemap's orientation
1920
*
2021
* @return {number} The X location in tile units.
2122
*/
22-
var WorldToTileX = function (worldX, snapToFloor, camera, layer)
23+
var WorldToTileX = function (worldX, snapToFloor, camera, layer, orientation)
2324
{
25+
26+
2427
if (snapToFloor === undefined) { snapToFloor = true; }
2528

2629
var tileWidth = layer.baseTileWidth;
30+
var tileHeight = layer.baseTileHeight;
2731
var tilemapLayer = layer.tilemapLayer;
2832

2933
if (tilemapLayer)
3034
{
3135
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
3236

37+
// Find the world position relative to the static or dynamic layer's top left origin,
38+
// factoring in the camera's vertical scroll
39+
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
40+
41+
tileHeight *= tilemapLayer.scaleY;
42+
3343
// Find the world position relative to the static or dynamic layer's top left origin,
3444
// factoring in the camera's horizontal scroll
3545
worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX));
3646

3747
tileWidth *= tilemapLayer.scaleX;
3848
}
3949

40-
return snapToFloor
41-
? Math.floor(worldX / tileWidth)
42-
: worldX / tileWidth;
50+
if (orientation === "orthogonal") {
51+
return snapToFloor
52+
? Math.floor(worldX / tileWidth)
53+
: worldX / tileWidth;
54+
} else if (orientation === "isometric") {
55+
return snapToFloor
56+
? Math.floor((worldX/(tileWidth/2) + worldY/(tileHeight/2))/2)
57+
: ((worldX/(tileWidth/2) + worldY/(tileHeight/2))/2);
58+
59+
}
60+
4361
};
4462

4563
module.exports = WorldToTileX;

src/tilemaps/components/WorldToTileXY.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ var Vector2 = require('../../math/Vector2');
2424
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
2525
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
2626
*
27+
* @param {string} orientation - The Tilemap's orientation
2728
* @return {Phaser.Math.Vector2} The XY location in tile units.
2829
*/
29-
var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
30+
var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer,orientation)
3031
{
3132
if (point === undefined) { point = new Vector2(0, 0); }
3233

33-
point.x = WorldToTileX(worldX, snapToFloor, camera, layer);
34-
point.y = WorldToTileY(worldY, snapToFloor, camera, layer);
34+
point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation);
35+
point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation);
3536

3637
return point;
3738
};

src/tilemaps/components/WorldToTileY.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
1717
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the tile index from the world values.
1818
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
19-
*
19+
* @param {string} orientation - The Tilemap's orientation
20+
*
2021
* @return {number} The Y location in tile units.
2122
*/
22-
var WorldToTileY = function (worldY, snapToFloor, camera, layer)
23+
var WorldToTileY = function (worldY, snapToFloor, camera, layer, orientation)
2324
{
2425
if (snapToFloor === undefined) { snapToFloor = true; }
2526

27+
var tileWidth = layer.baseTileWidth;
2628
var tileHeight = layer.baseTileHeight;
2729
var tilemapLayer = layer.tilemapLayer;
28-
30+
2931
if (tilemapLayer)
3032
{
3133
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
@@ -35,11 +37,24 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer)
3537
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
3638

3739
tileHeight *= tilemapLayer.scaleY;
40+
41+
// Find the world position relative to the static or dynamic layer's top left origin,
42+
// factoring in the camera's horizontal scroll
43+
worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX));
44+
45+
tileWidth *= tilemapLayer.scaleX;
3846
}
3947

40-
return snapToFloor
41-
? Math.floor(worldY / tileHeight)
42-
: worldY / tileHeight;
48+
if (orientation === "orthogonal") {
49+
return snapToFloor
50+
? Math.floor(worldY / tileHeight)
51+
: worldY / tileHeight;
52+
} else if (orientation === "isometric") {
53+
return snapToFloor
54+
? Math.floor(worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2)
55+
: (worldY/(tileHeight/2) - (worldX/(tileWidth/2))/2);
56+
57+
}
4358
};
4459

4560
module.exports = WorldToTileY;

src/tilemaps/parsers/tiled/ParseJSONTiled.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ var AssignTileProperties = require('./AssignTileProperties');
3232
*/
3333
var ParseJSONTiled = function (name, json, insertNull)
3434
{
35-
if (json.orientation !== 'orthogonal')
36-
{
37-
console.warn('Only orthogonal map types are supported in this version of Phaser');
38-
return null;
39-
}
35+
// if (json.orientation !== 'orthogonal')
36+
// {
37+
// console.warn('Only orthogonal map types are supported in this version of Phaser');
38+
// return null;
39+
// }
4040

4141
// Map data will consist of: layers, objects, images, tilesets, sizes
4242
var mapData = new MapData({

src/tilemaps/staticlayer/StaticTilemapLayer.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var Utils = require('../../renderer/webgl/Utils');
2323
*
2424
* Use a Static Tilemap Layer instead of a Dynamic Tilemap Layer when you don't need tile manipulation features.
2525
*
26-
* @class StaticTilemapLayer
26+
* @class StaticTilemapLayers
2727
* @extends Phaser.GameObjects.GameObject
2828
* @memberof Phaser.Tilemaps
2929
* @constructor
@@ -1124,7 +1124,8 @@ var StaticTilemapLayer = new Class({
11241124
*/
11251125
hasTileAtWorldXY: function (worldX, worldY, camera)
11261126
{
1127-
return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, this.layer);
1127+
1128+
return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, this.tilemap.orientation);
11281129
},
11291130

11301131
/**
@@ -1320,13 +1321,14 @@ var StaticTilemapLayer = new Class({
13201321
},
13211322

13221323
/**
1323-
* Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the
1324+
* Converts from tile X and Y coordinates (tile units) to world X coordinates (pixels), factoring in the
13241325
* layers position, scale and scroll.
13251326
*
13261327
* @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldX
13271328
* @since 3.0.0
13281329
*
13291330
* @param {integer} tileX - The X coordinate, in tile coordinates.
1331+
* @param {integer} tileY - The Y coordinate, in tile coordinates.
13301332
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the world values from the tile index.
13311333
*
13321334
* @return {number}
@@ -1337,13 +1339,14 @@ var StaticTilemapLayer = new Class({
13371339
},
13381340

13391341
/**
1340-
* Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
1342+
* Converts from tile X and Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
13411343
* layers position, scale and scroll.
13421344
*
13431345
* @method Phaser.Tilemaps.StaticTilemapLayer#tileToWorldY
13441346
* @since 3.0.0
13451347
*
13461348
* @param {integer} tileY - The Y coordinate, in tile coordinates.
1349+
* @param {integer} tileY - The Y coordinate, in tile coordinates.
13471350
* @param {Phaser.Cameras.Scene2D.Camera} [camera=main camera] - The Camera to use when calculating the world values from the tile index.
13481351
*
13491352
* @return {number}
@@ -1389,7 +1392,7 @@ var StaticTilemapLayer = new Class({
13891392
*/
13901393
worldToTileX: function (worldX, snapToFloor, camera)
13911394
{
1392-
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer);
1395+
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer, this.tilemap.orientation);
13931396
},
13941397

13951398
/**
@@ -1408,7 +1411,7 @@ var StaticTilemapLayer = new Class({
14081411
*/
14091412
worldToTileY: function (worldY, snapToFloor, camera)
14101413
{
1411-
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer);
1414+
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer, this.tilemap.orientation);
14121415
},
14131416

14141417
/**
@@ -1430,7 +1433,7 @@ var StaticTilemapLayer = new Class({
14301433
*/
14311434
worldToTileXY: function (worldX, worldY, snapToFloor, point, camera)
14321435
{
1433-
return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, this.layer);
1436+
return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, this.layer, this.tilemap.orientation);
14341437
},
14351438

14361439
/**

0 commit comments

Comments
 (0)