refactored things so that there is no more switching at runtime depending on orientation

This commit is contained in:
Svipal 2020-04-03 19:36:13 +02:00
parent 1ccc815ce7
commit 8665e08f13
30 changed files with 10481 additions and 8973 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

9408
dist/phaser.js vendored

File diff suppressed because it is too large Load diff

2
dist/phaser.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -24,6 +24,7 @@
"dist": "webpack --config config/webpack.dist.config.js", "dist": "webpack --config config/webpack.dist.config.js",
"distT": "webpack --config config/webpack.dist.config.js && npm run toT", "distT": "webpack --config config/webpack.dist.config.js && npm run toT",
"toT": "@powershell Copy-Item ./dist/phaser.min.js ../phasertest/phaser.min.js", "toT": "@powershell Copy-Item ./dist/phaser.min.js ../phasertest/phaser.min.js",
"toT2": "@powershell Copy-Item ./dist/phaser.js ../phasertest/phaser.min.js",
"distfb": "webpack --config config/webpack.fb.dist.config.js", "distfb": "webpack --config config/webpack.fb.dist.config.js",
"distfull": "npm run dist && npm run distfb", "distfull": "npm run dist && npm run distfb",
"plugin.cam3d": "webpack --config plugins/camera3d/webpack.config.js", "plugin.cam3d": "webpack --config plugins/camera3d/webpack.config.js",

View file

@ -168,7 +168,26 @@ var CONST = {
* @type {integer} * @type {integer}
* @since 3.2.2 * @since 3.2.2
*/ */
HEXAGONAL: 3 HEXAGONAL: 3,
fromOrientationString: function (orientation)
{
var constor = CONST.ORTHOGONAL;
if (orientation === 'isometric')
{
constor = CONST.ISOMETRIC;
}
else if (orientation === 'staggered')
{
constor = CONST.STAGGERED;
}
else if (orientation === 'hexagonal')
{
constor = CONST.HEXAGONAL;
}
return constor;
}
}; };
module.exports = CONST; module.exports = CONST;

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../const.js');
var Class = require('../utils/Class'); var Class = require('../utils/Class');
var Components = require('../gameobjects/components'); var Components = require('../gameobjects/components');
var Rectangle = require('../geom/rectangle'); var Rectangle = require('../geom/rectangle');
@ -706,7 +707,26 @@ var Tile = new Class({
*/ */
updatePixelXY: function () updatePixelXY: function ()
{ {
if (this.layer.orientation === 'orthogonal') if (this.layer.orientation === CONST.ISOMETRIC)
{
// reminder : for the tilemap to be centered we have to move the image to the right with the camera !
// this is crucial for wordtotile, tiletoworld to work.
this.pixelX = (this.x - this.y) * this.baseWidth * 0.5;
this.pixelY = (this.x + this.y) * this.baseHeight * 0.5;
}
else if (this.layer.orientation === CONST.STAGGERED)
{
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
this.pixelY = this.y * (this.baseHeight / 2);
}
else if (this.layer.orientation === CONST.HEXAGONAL)
{
var sidel = this.layer.hexSideLength;
var rowHeight = ((this.baseHeight - sidel) / 2 + sidel);
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
this.pixelY = this.y * rowHeight;
}
else if (this.layer.orientation === CONST.ORTHOGONAL)
{ {
// In orthogonal mode, Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile is the // In orthogonal mode, Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile is the
// bottom left, while the Phaser renderer assumes the origin is the top left. The y // bottom left, while the Phaser renderer assumes the origin is the top left. The y
@ -715,25 +735,6 @@ var Tile = new Class({
this.pixelY = this.y * this.baseHeight; this.pixelY = this.y * this.baseHeight;
} }
else if (this.layer.orientation === 'isometric')
{
// reminder : for the tilemap to be centered we have to move the image to the right with the camera !
// this is crucial for wordtotile, tiletoworld to work.
this.pixelX = (this.x - this.y) * this.baseWidth * 0.5;
this.pixelY = (this.x + this.y) * this.baseHeight * 0.5;
}
else if (this.layer.orientation === 'staggered')
{
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
this.pixelY = this.y * (this.baseHeight / 2);
}
else if (this.layer.orientation === 'hexagonal')
{
var sidel = this.layer.hexSideLength;
var rowHeight = ((this.baseHeight - sidel) / 2 + sidel);
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
this.pixelY = this.y * rowHeight;
}
return this; return this;
}, },

View file

@ -260,6 +260,19 @@ var Tilemap = new Class({
* @since 3.0.0 * @since 3.0.0
*/ */
this.hexSideLength = mapData.hexSideLength; this.hexSideLength = mapData.hexSideLength;
/**
* Components used for conversions between real world coordinates and tile coordinates,
* initialized here to prevent switching between them at runtime depending on map orientation.
* refer to the components themselves for documentation.
* @since 3.2.2
*/
this.WorldToTileXY = TilemapComponents.WorldToTileXY(this.orientation);
this.WorldToTileX = TilemapComponents.WorldToTileX(this.orientation);
this.WorldToTileY = TilemapComponents.WorldToTileY(this.orientation);
this.TileToWorldXY = TilemapComponents.TileToWorldXY(this.orientation);
this.TileToWorldX = TilemapComponents.TileToWorldX(this.orientation);
this.TileToWorldY = TilemapComponents.TileToWorldY(this.orientation);
}, },
/** /**
@ -2355,7 +2368,7 @@ var Tilemap = new Class({
if (layer === null) { return null; } if (layer === null) { return null; }
return TilemapComponents.TileToWorldX(tileX, camera, layer); return this.TileToWorldX(tileX, camera, layer);
}, },
/** /**
@ -2380,7 +2393,7 @@ var Tilemap = new Class({
if (layer === null) { return null; } if (layer === null) { return null; }
return TilemapComponents.TileToWorldY(tileX, camera, layer); return this.TileToWorldY(tileX, camera, layer);
}, },
/** /**
@ -2401,13 +2414,14 @@ var Tilemap = new Class({
* *
* @return {?Phaser.Math.Vector2} Returns a point, or null if the layer given was invalid. * @return {?Phaser.Math.Vector2} Returns a point, or null if the layer given was invalid.
*/ */
tileToWorldXY: function (tileX, tileY, point, camera, layer) tileToWorldXY: function (tileX, tileY, point, camera, layer)
{ {
layer = this.getLayer(layer); layer = this.getLayer(layer);
if (layer === null) { return null; } if (layer === null) { return null; }
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, layer); return this.TileToWorldXY(tileX, tileY, point, camera, layer);
}, },
/** /**
@ -2478,7 +2492,7 @@ var Tilemap = new Class({
if (layer === null) { return null; } if (layer === null) { return null; }
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, layer); return this.WorldToTileX(worldX, snapToFloor, camera, layer);
}, },
/** /**
@ -2503,7 +2517,7 @@ var Tilemap = new Class({
if (layer === null) { return null; } if (layer === null) { return null; }
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, layer); return this.WorldToTileY(worldY, snapToFloor, camera, layer);
}, },
/** /**
@ -2531,7 +2545,7 @@ var Tilemap = new Class({
if (layer === null) { return null; } if (layer === null) { return null; }
return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, layer); return this.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, layer);
}, },
/** /**

View file

@ -4,7 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var TileToWorldXY = require('./TileToWorldXY');
var GetTilesWithin = require('./GetTilesWithin'); var GetTilesWithin = require('./GetTilesWithin');
var ReplaceByIndex = require('./ReplaceByIndex'); var ReplaceByIndex = require('./ReplaceByIndex');
@ -47,7 +47,7 @@ var CreateFromTiles = function (indexes, replacements, spriteConfig, scene, came
if (indexes.indexOf(tile.index) !== -1) if (indexes.indexOf(tile.index) !== -1)
{ {
var point = TileToWorldXY(tile.x,tile.y, undefined, camera, layer); var point = tilemapLayer.tileToWorldXY(tile.x,tile.y, undefined, camera);
spriteConfig.x = point.x; spriteConfig.x = point.x;
spriteConfig.y = point.y; spriteConfig.y = point.y;
var sprite = scene.make.sprite(spriteConfig); var sprite = scene.make.sprite(spriteConfig);

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../const.js');
var SnapFloor = require('../../math/snap/SnapFloor'); var SnapFloor = require('../../math/snap/SnapFloor');
var SnapCeil = require('../../math/snap/SnapCeil'); var SnapCeil = require('../../math/snap/SnapCeil');
var CheckIsoBounds = require('./CheckIsoBounds'); var CheckIsoBounds = require('./CheckIsoBounds');
@ -48,7 +49,7 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1) if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
{ {
if (layer.orientation === 'orthogonal' || layer.orientation === 'staggered' || layer.orientation === 'hexagonal') if (layer.orientation === CONST.ORTHOGONAL || layer.orientation === CONST.STAGGERED || layer.orientation === CONST.HEXAGONAL)
{ {
// Camera world view bounds, snapped for scaled tile size // Camera world view bounds, snapped for scaled tile size
// Cull Padding values are given in tiles, not pixels // Cull Padding values are given in tiles, not pixels
@ -58,17 +59,17 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
var boundsTop; var boundsTop;
var boundsBottom; var boundsBottom;
if (layer.orientation === 'orthogonal') if (layer.orientation === CONST.ORTHOGONAL)
{ {
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY; boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY; boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
} }
else if (layer.orientation === 'staggered') else if (layer.orientation === CONST.STAGGERED)
{ {
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH / 2, 0, true) - tilemapLayer.cullPaddingY; boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH / 2, 0, true) - tilemapLayer.cullPaddingY;
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH / 2, 0, true) + tilemapLayer.cullPaddingY; boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH / 2, 0, true) + tilemapLayer.cullPaddingY;
} }
else if (layer.orientation === 'hexagonal') else if (layer.orientation === CONST.HEXAGONAL)
{ {
var sidel = layer.hexSideLength; var sidel = layer.hexSideLength;
var rowH = ((tileH - sidel) / 2 + sidel); var rowH = ((tileH - sidel) / 2 + sidel);
@ -92,7 +93,7 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
var tile; var tile;
if (layer.orientation === 'orthogonal' || layer.orientation === 'staggered' || layer.orientation === 'hexagonal') if (layer.orientation === CONST.ORTHOGONAL || layer.orientation === CONST.STAGGERED || layer.orientation === CONST.HEXAGONAL)
{ {
if (renderOrder === 0) if (renderOrder === 0)
@ -172,7 +173,7 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
} }
} }
} }
else if (layer.orientation === 'isometric') else if (layer.orientation === CONST.ISOMETRIC)
{ {
if (renderOrder === 0) if (renderOrder === 0)
{ {

View file

@ -5,7 +5,6 @@
*/ */
var GetTileAt = require('./GetTileAt'); var GetTileAt = require('./GetTileAt');
var WorldToTileXY = require('./WorldToTileXY');
/** /**
* Gets a tile at the given world coordinates from the given layer. * Gets a tile at the given world coordinates from the given layer.
@ -25,7 +24,7 @@ var WorldToTileXY = require('./WorldToTileXY');
*/ */
var GetTileAtWorldXY = function (worldX, worldY, nonNull, camera, layer) var GetTileAtWorldXY = function (worldX, worldY, nonNull, camera, layer)
{ {
var point = WorldToTileXY(worldX, worldY, true, undefined, camera, layer); var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
var tileX = point.x; var tileX = point.x;
var tileY = point.y; var tileY = point.y;
return GetTileAt(tileX, tileY, nonNull, layer); return GetTileAt(tileX, tileY, nonNull, layer);

View file

@ -8,8 +8,7 @@ var Geom = require('../../geom/');
var GetTilesWithin = require('./GetTilesWithin'); var GetTilesWithin = require('./GetTilesWithin');
var Intersects = require('../../geom/intersects/'); var Intersects = require('../../geom/intersects/');
var NOOP = require('../../utils/NOOP'); var NOOP = require('../../utils/NOOP');
var TileToWorldXY = require('./TileToWorldXY');
var WorldToTileXY = require('./WorldToTileXY');
var TriangleToRectangle = function (triangle, rect) var TriangleToRectangle = function (triangle, rect)
{ {
@ -49,12 +48,12 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
else if (shape instanceof Geom.Line) { intersectTest = Intersects.LineToRectangle; } else if (shape instanceof Geom.Line) { intersectTest = Intersects.LineToRectangle; }
// Top left corner of the shapes's bounding box, rounded down to include partial tiles // Top left corner of the shapes's bounding box, rounded down to include partial tiles
var pointStart = WorldToTileXY(shape.left, shape.top, true, undefined, camera, layer); var pointStart = layer.tilemapLayer.worldToTileXY(shape.left, shape.top, true, undefined, camera);
var xStart = pointStart.x; var xStart = pointStart.x;
var yStart = pointStart.y; var yStart = pointStart.y;
// Bottom right corner of the shapes's bounding box, rounded up to include partial tiles // Bottom right corner of the shapes's bounding box, rounded up to include partial tiles
var pointEnd = WorldToTileXY(shape.right, shape.bottom, true, undefined, camera, layer); var pointEnd = layer.tilemapLayer.worldToTileXY(shape.right, shape.bottom, true, undefined, camera);
var xEnd = Math.ceil(pointEnd.x); var xEnd = Math.ceil(pointEnd.x);
var yEnd = Math.ceil(pointEnd.y); var yEnd = Math.ceil(pointEnd.y);
@ -77,7 +76,7 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
for (var i = 0; i < tiles.length; i++) for (var i = 0; i < tiles.length; i++)
{ {
var tile = tiles[i]; var tile = tiles[i];
var point = TileToWorldXY(tile.x, tile.y, undefined, camera, layer); var point = layer.tilemapLayer.tileToWorldXY(tile.x, tile.y, undefined, camera);
tileRect.x = point.x; tileRect.x = point.x;
tileRect.y = point.y; tileRect.y = point.y;
if (intersectTest(shape, tileRect)) if (intersectTest(shape, tileRect))

View file

@ -5,7 +5,6 @@
*/ */
var GetTilesWithin = require('./GetTilesWithin'); var GetTilesWithin = require('./GetTilesWithin');
var WorldToTileXY = require('./WorldToTileXY');
/** /**
* Gets the tiles in the given rectangular area (in world coordinates) of the layer. * Gets the tiles in the given rectangular area (in world coordinates) of the layer.
@ -31,12 +30,12 @@ var GetTilesWithinWorldXY = function (worldX, worldY, width, height, filteringOp
{ {
// Top left corner of the rect, rounded down to include partial tiles // Top left corner of the rect, rounded down to include partial tiles
var pointStart = WorldToTileXY(worldX, worldY, true, undefined, camera, layer); var pointStart = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
var xStart = pointStart.x; var xStart = pointStart.x;
var yStart = pointStart.y; var yStart = pointStart.y;
// Bottom right corner of the rect, rounded up to include partial tiles // Bottom right corner of the rect, rounded up to include partial tiles
var pointEnd = WorldToTileXY(worldX + width, worldY + height, false, undefined, camera, layer); var pointEnd = layer.tilemapLayer.worldToTileXY(worldX + width, worldY + height, false, undefined, camera);
var xEnd = Math.ceil(pointEnd.x); var xEnd = Math.ceil(pointEnd.x);
var yEnd = Math.ceil(pointEnd.y); var yEnd = Math.ceil(pointEnd.y);

View file

@ -5,7 +5,6 @@
*/ */
var HasTileAt = require('./HasTileAt'); var HasTileAt = require('./HasTileAt');
var WorldToTileXY = require('./WorldToTileXY');
/** /**
* Checks if there is a tile at the given location (in world coordinates) in the given layer. Returns * Checks if there is a tile at the given location (in world coordinates) in the given layer. Returns
@ -24,7 +23,7 @@ var WorldToTileXY = require('./WorldToTileXY');
*/ */
var HasTileAtWorldXY = function (worldX, worldY, camera, layer) var HasTileAtWorldXY = function (worldX, worldY, camera, layer)
{ {
var point = WorldToTileXY(worldX, worldY, true, undefined, camera, layer); var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
var tileX = point.x; var tileX = point.x;
var tileY = point.y; var tileY = point.y;
return HasTileAt(tileX, tileY, layer); return HasTileAt(tileX, tileY, layer);

View file

@ -5,7 +5,6 @@
*/ */
var PutTileAt = require('./PutTileAt'); var PutTileAt = require('./PutTileAt');
var WorldToTileXY = require('./WorldToTileXY');
/** /**
* Puts a tile at the given world coordinates (pixels) in the specified layer. You can pass in either * Puts a tile at the given world coordinates (pixels) in the specified layer. You can pass in either
@ -28,7 +27,7 @@ var WorldToTileXY = require('./WorldToTileXY');
*/ */
var PutTileAtWorldXY = function (tile, worldX, worldY, recalculateFaces, camera, layer) var PutTileAtWorldXY = function (tile, worldX, worldY, recalculateFaces, camera, layer)
{ {
var point = WorldToTileXY(worldX, worldY, true, undefined, camera, layer); var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
var tileX = point.x; var tileX = point.x;
var tileY = point.y; var tileY = point.y;
return PutTileAt(tile, tileX, tileY, recalculateFaces, layer); return PutTileAt(tile, tileX, tileY, recalculateFaces, layer);

View file

@ -5,7 +5,7 @@
*/ */
var RemoveTileAt = require('./RemoveTileAt'); var RemoveTileAt = require('./RemoveTileAt');
var WorldToTileXY = require('./WorldToTileXY');
/** /**
* Removes the tile at the given world coordinates in the specified layer and updates the layer's * Removes the tile at the given world coordinates in the specified layer and updates the layer's
@ -26,7 +26,7 @@ var WorldToTileXY = require('./WorldToTileXY');
*/ */
var RemoveTileAtWorldXY = function (worldX, worldY, replaceWithNull, recalculateFaces, camera, layer) var RemoveTileAtWorldXY = function (worldX, worldY, replaceWithNull, recalculateFaces, camera, layer)
{ {
var point = WorldToTileXY(worldX, worldY, true, undefined, camera, layer); var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
var tileX = point.x; var tileX = point.x;
var tileY = point.y; var tileY = point.y;
return RemoveTileAt(tileX, tileY, replaceWithNull, recalculateFaces, layer); return RemoveTileAt(tileX, tileY, replaceWithNull, recalculateFaces, layer);

View file

@ -4,8 +4,10 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../const.js');
/** /**
* Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the * Converts from orthogonal tile X coordinates (tile units) to world X coordinates (pixels), factoring in the
* layer's position, scale and scroll. * layer's position, scale and scroll.
* *
* @function Phaser.Tilemaps.Components.TileToWorldX * @function Phaser.Tilemaps.Components.TileToWorldX
@ -18,9 +20,8 @@
* *
* @return {number} * @return {number}
*/ */
var TileToWorldX = function (tileX, camera, layer) var OrthoTileToWorldX = function (tileX, camera, layer)
{ {
var orientation = layer.orientation;
var tileWidth = layer.baseTileWidth; var tileWidth = layer.baseTileWidth;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
var layerWorldX = 0; var layerWorldX = 0;
@ -34,19 +35,27 @@ var TileToWorldX = function (tileX, camera, layer)
tileWidth *= tilemapLayer.scaleX; tileWidth *= tilemapLayer.scaleX;
} }
if (orientation === 'orthogonal')
{
return layerWorldX + tileX * tileWidth; return layerWorldX + tileX * tileWidth;
}
else if (orientation === 'isometric' || orientation === 'staggered' || orientation === 'hexagonal')
{
// Not Best Solution ?
console.warn('With the current map type you have to use the TileToWorldXY function.');
return null;
}
}; };
var nullFunc = function ()
{
console.warn('With the current map type you have to use the TileToWorldXY function.');
return null;
};
var TileToWorldX = function (orientation)
{
switch (orientation)
{
case CONST.ORTHOGONAL:
return OrthoTileToWorldX;
default:
return nullFunc;
}
};
module.exports = TileToWorldX; module.exports = TileToWorldX;

View file

@ -4,8 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var TileToWorldX = require('./TileToWorldX'); var CONST = require('../../const.js');
var TileToWorldY = require('./TileToWorldY');
var Vector2 = require('../../math/Vector2'); var Vector2 = require('../../math/Vector2');
/** /**
@ -13,7 +12,7 @@ var Vector2 = require('../../math/Vector2');
* layer's position, scale and scroll. This will return a new Vector2 object or update the given * layer's position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object. * `point` object.
* *
* @function Phaser.Tilemaps.Components.TileToWorldXY * @function Phaser.Tilemaps.Components.OrthoTileToWorldXY
* @private * @private
* @since 3.0.0 * @since 3.0.0
* *
@ -25,27 +24,62 @@ var Vector2 = require('../../math/Vector2');
* *
* @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 OrthoTileToWorldXY = function (tileX, tileY, point, camera, layer)
{
if (point === undefined) { point = new Vector2(0, 0); }
var tileHeight = layer.baseTileHeight;
var tileWidth = layer.baseTileWidth;
var tilemapLayer = layer.tilemapLayer;
var layerWorldX = 0;
var layerWorldY = 0;
if (tilemapLayer)
{
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
layerWorldX = tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX);
tileWidth *= tilemapLayer.scaleX;
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
point.x = layerWorldX + tileX * tileWidth;
point.y = layerWorldY + tileY * tileHeight;
return point;
};
/**
* Converts from isometric 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.
*
* @function Phaser.Tilemaps.Components.IsoTileToWorldXY
* @private
* @since 3.2.2
*
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @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.
*
* @return {Phaser.Math.Vector2} The XY location in world coordinates.
*/
var IsoTileToWorldXY = function (tileX, tileY, point, camera, layer)
{ {
var orientation = layer.orientation;
var tileWidth = layer.baseTileWidth; var tileWidth = layer.baseTileWidth;
var tileHeight = layer.baseTileHeight; var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
if (point === undefined) { point = new Vector2(0, 0); } if (point === undefined) { point = new Vector2(0, 0); }
if (orientation === 'orthogonal')
{
point.x = TileToWorldX(tileX, camera, layer, orientation);
point.y = TileToWorldY(tileY, camera, layer, orientation);
}
else if (orientation === 'isometric' || orientation === 'staggered' || orientation === 'hexagonal')
{
var layerWorldX = 0; var layerWorldX = 0;
var layerWorldY = 0; var layerWorldY = 0;
@ -59,30 +93,117 @@ var TileToWorldXY = function (tileX, tileY, point, camera, layer)
} }
if (orientation === 'isometric')
{
point.x = layerWorldX + (tileX - tileY) * (tileWidth / 2); point.x = layerWorldX + (tileX - tileY) * (tileWidth / 2);
point.y = layerWorldY + (tileX + tileY) * (tileHeight / 2); point.y = layerWorldY + (tileX + tileY) * (tileHeight / 2);
}
else if (orientation === 'staggered')
return point;
};
/**
* Converts from staggered 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.
*
* @function Phaser.Tilemaps.Components.StagTileToWorldXY
* @private
* @since 3.2.2
*
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @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.
*
* @return {Phaser.Math.Vector2} The XY location in world coordinates.
*/
var StagTileToWorldXY = function (tileX, tileY, point, camera, layer)
{
var tileWidth = layer.baseTileWidth;
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer;
if (point === undefined) { point = new Vector2(0, 0); }
var layerWorldX = 0;
var layerWorldY = 0;
if (tilemapLayer)
{ {
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
layerWorldX = tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX);
tileWidth *= tilemapLayer.scaleX;
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2); point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2);
point.y = layerWorldY + tileY * (tileHeight / 2); point.y = layerWorldY + tileY * (tileHeight / 2);
}
else if (orientation === 'hexagonal') return point;
};
/**
* Converts from hexagonal 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.
*
* @function Phaser.Tilemaps.Components.HexTileToWorldXY
* @private
* @since 3.2.2
*
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @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.
*
* @return {Phaser.Math.Vector2} The XY location in world coordinates.
*/
var HexTileToWorldXY = function (tileX, tileY, point, camera, layer)
{
var tileWidth = layer.baseTileWidth;
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer;
if (point === undefined) { point = new Vector2(0, 0); }
var layerWorldX = 0;
var layerWorldY = 0;
if (tilemapLayer)
{ {
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
layerWorldX = tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX);
tileWidth *= tilemapLayer.scaleX;
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
var sidel = layer.hexSideLength; var sidel = layer.hexSideLength;
var rowHeight = ((tileHeight - sidel) / 2 + sidel); var rowHeight = ((tileHeight - sidel) / 2 + sidel);
// similar to staggered, because Tiled uses the oddr representation. // similar to staggered, because Tiled uses the oddr representation.
point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2); point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2);
point.y = layerWorldY + tileY * rowHeight; point.y = layerWorldY + tileY * rowHeight;
}
}
return point; return point;
}; };
var TileToWorldXY = function (orientation)
{
switch (orientation)
{
case CONST.STAGGERED:
return StagTileToWorldXY;
case CONST.ISOMETRIC:
return IsoTileToWorldXY;
case CONST.HEXAGONAL:
return HexTileToWorldXY;
default:
return OrthoTileToWorldXY;
}
};
module.exports = TileToWorldXY; module.exports = TileToWorldXY;

View file

@ -4,8 +4,11 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../const.js');
/** /**
* Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the * Converts from orthogonal tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
* layer's position, scale and scroll. * layer's position, scale and scroll.
* *
* @function Phaser.Tilemaps.Components.TileToWorldY * @function Phaser.Tilemaps.Components.TileToWorldY
@ -18,9 +21,8 @@
* *
* @return {number} * @return {number}
*/ */
var TileToWorldY = function (tileY, camera, layer) var OrthoTileToWorldY = function (tileY, camera, layer)
{ {
var orientation = layer.orientation;
var tileHeight = layer.baseTileHeight; var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
var layerWorldY = 0; var layerWorldY = 0;
@ -32,28 +34,100 @@ 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') };
/**
* Converts from staggered tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
* layer's position, scale and scroll.
*
* @function Phaser.Tilemaps.Components.StagTileToWorldY
* @private
* @since 3.0.0
*
* @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.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
*
* @return {number}
*/
var StagTileToWorldY = function (tileY, camera, layer)
{
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer;
var layerWorldY = 0;
if (tilemapLayer)
{ {
// Not Best Solution ? if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
console.warn('With isometric map types you have to use the TileToWorldXY function.'); layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
return null; tileHeight *= tilemapLayer.scaleY;
} }
else if (orientation === 'staggered')
{
return layerWorldY + tileY * (tileHeight / 2); return layerWorldY + tileY * (tileHeight / 2);
}
else if (orientation === 'hexagonal') };
/**
* Converts from hexagonal tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
* layer's position, scale and scroll.
*
* @function Phaser.Tilemaps.Components.HexTileToWorldY
* @private
* @since 3.0.0
*
* @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.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
*
* @return {number}
*/
var HexTileToWorldY = function (tileY, camera, layer)
{
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer;
var layerWorldY = 0;
if (tilemapLayer)
{ {
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
var sidel = layer.tilemapLayer.tilemap.hexSideLength; var sidel = layer.tilemapLayer.tilemap.hexSideLength;
var rowHeight = ((tileHeight - sidel) / 2 + sidel); var rowHeight = ((tileHeight - sidel) / 2 + sidel);
// same as staggered // same as staggered
return layerWorldY + tileY * rowHeight; return layerWorldY + tileY * rowHeight;
};
var nullFunc = function ()
{
console.warn('With the current map type you have to use the TileToWorldXY function.');
return null;
};
var TileToWorldY = function (orientation)
{
switch (orientation)
{
case CONST.STAGGERED:
return StagTileToWorldY;
case CONST.ISOMETRIC:
return nullFunc;
case CONST.HEXAGONAL:
return HexTileToWorldY;
default:
return OrthoTileToWorldY;
} }
}; };
module.exports = TileToWorldY; module.exports = TileToWorldY;

View file

@ -4,8 +4,10 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../const.js');
/** /**
* Converts from world X coordinates (pixels) to tile X coordinates (tile units), factoring in the * Converts from world X coordinates (pixels) to orthogonal tile X coordinates (tile units), factoring in the
* layer's position, scale and scroll. * layer's position, scale and scroll.
* *
* @function Phaser.Tilemaps.Components.WorldToTileX * @function Phaser.Tilemaps.Components.WorldToTileX
@ -19,10 +21,9 @@
* *
* @return {number} The X location in tile units. * @return {number} The X location in tile units.
*/ */
var WorldToTileX = function (worldX, snapToFloor, camera, layer) var OrthoWorldToTileX = function (worldX, snapToFloor, camera, layer)
{ {
var orientation = layer.orientation;
if (snapToFloor === undefined) { snapToFloor = true; } if (snapToFloor === undefined) { snapToFloor = true; }
var tileWidth = layer.baseTileWidth; var tileWidth = layer.baseTileWidth;
@ -39,16 +40,28 @@ var WorldToTileX = function (worldX, snapToFloor, camera, layer)
tileWidth *= tilemapLayer.scaleX; tileWidth *= tilemapLayer.scaleX;
} }
if (orientation === 'orthogonal')
{
return snapToFloor return snapToFloor
? Math.floor(worldX / tileWidth) ? Math.floor(worldX / tileWidth)
: worldX / tileWidth; : worldX / tileWidth;
}
else if (orientation === 'isometric') };
{
console.warn('With isometric map types you have to use the WorldToTileXY function.'); var nullFunc = function ()
{
console.warn('With the current map type you have to use the WorldToTileXY function.');
return null; return null;
};
var WorldToTileX = function (orientation)
{
switch (orientation)
{
case CONST.ORTHOGONAL:
return OrthoWorldToTileX;
default:
return nullFunc;
} }
}; };

View file

@ -4,16 +4,17 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var WorldToTileX = require('./WorldToTileX'); var CONST = require('../../const.js');
var WorldToTileY = require('./WorldToTileY'); var WorldToTileX = require('./WorldToTileX').func;
var WorldToTileY = require('./WorldToTileY').func;
var Vector2 = require('../../math/Vector2'); var Vector2 = require('../../math/Vector2');
/** /**
* Converts from world XY coordinates (pixels) to tile XY coordinates (tile units), factoring in the * Converts from world XY coordinates (pixels) to orthogonal tile XY coordinates (tile units), factoring in the
* layer's position, scale and scroll. This will return a new Vector2 object or update the given * layer's position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object. * `point` object.
* *
* @function Phaser.Tilemaps.Components.WorldToTileXY * @function Phaser.Tilemaps.Components.OrthoWorldToTileXY
* @private * @private
* @since 3.0.0 * @since 3.0.0
* *
@ -26,18 +27,36 @@ var Vector2 = require('../../math/Vector2');
* *
* @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 OrthoWorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
{ {
var orientation = layer.orientation;
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);
{ point.y = WorldToTileY(worldY, snapToFloor, camera, layer);
point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation); return point;
point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation); };
}
else if (orientation === 'isometric' || orientation === 'staggered' || orientation === 'hexagonal') /**
{ * Converts from world XY coordinates (pixels) to isometric tile XY coordinates (tile units), factoring in the
* layer's position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object.
*
* @function Phaser.Tilemaps.Components.IsoWorldToTileXY
* @private
* @since 3.0.0
*
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
* @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.
*
* @return {Phaser.Math.Vector2} The XY location in tile units.
*/
var IsoWorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
{
if (point === undefined) { point = new Vector2(0, 0); }
var tileWidth = layer.baseTileWidth; var tileWidth = layer.baseTileWidth;
var tileHeight = layer.baseTileHeight; var tileHeight = layer.baseTileHeight;
@ -62,8 +81,6 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
tileWidth *= tilemapLayer.scaleX; tileWidth *= tilemapLayer.scaleX;
} }
if (orientation === 'isometric')
{
point.x = snapToFloor point.x = snapToFloor
? Math.floor((worldX / (tileWidth / 2) + worldY / (tileHeight / 2)) / 2) ? Math.floor((worldX / (tileWidth / 2) + worldY / (tileHeight / 2)) / 2)
: ((worldX / (tileWidth / 2) + worldY / (tileHeight / 2)) / 2); : ((worldX / (tileWidth / 2) + worldY / (tileHeight / 2)) / 2);
@ -71,28 +88,55 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
point.y = snapToFloor point.y = snapToFloor
? Math.floor((worldY / (tileHeight / 2) - worldX / (tileWidth / 2)) / 2) ? Math.floor((worldY / (tileHeight / 2) - worldX / (tileWidth / 2)) / 2)
: ((worldY / (tileHeight / 2) - worldX / (tileWidth / 2)) / 2); : ((worldY / (tileHeight / 2) - worldX / (tileWidth / 2)) / 2);
}
if (orientation === 'orthogonal')
{
point.x = snapToFloor
? Math.floor(worldX / tileWidth)
: worldX / tileWidth;
point.y = snapToFloor
? Math.floor(worldY / tileHeight)
: worldY / tileHeight;
}
else if (orientation === 'staggered')
{
point.y = snapToFloor
? Math.floor((worldY / (tileHeight / 2)))
: (worldY / (tileHeight / 2));
point.x = snapToFloor
? Math.floor((worldX + (point.y % 2) * 0.5 * tileWidth) / tileWidth)
: (worldX + (point.y % 2) * 0.5 * tileWidth) / tileWidth;
} return point;
else if (orientation === 'hexagonal') };
/**
* Converts from world XY coordinates (pixels) to hexagonal tile XY coordinates (tile units), factoring in the
* layer's position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object.
*
* @function Phaser.Tilemaps.Components.HexWorldToTileXY
* @private
* @since 3.0.0
*
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
* @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.
*
* @return {Phaser.Math.Vector2} The XY location in tile units.
*/
var HexWorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
{
if (point === undefined) { point = new Vector2(0, 0); }
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
// console.log(1,worldY)
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
// console.log(worldY)
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;
}
var sidel = layer.hexSideLength; var sidel = layer.hexSideLength;
var rowHeight = ((tileHeight - sidel) / 2 + sidel); var rowHeight = ((tileHeight - sidel) / 2 + sidel);
@ -103,10 +147,85 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
point.x = snapToFloor point.x = snapToFloor
? Math.floor((worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth) ? Math.floor((worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth)
: (worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth; : (worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth;
}
return point;
};
/**
* Converts from world XY coordinates (pixels) to staggered tile XY coordinates (tile units), factoring in the
* layer's position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object.
*
* @function Phaser.Tilemaps.Components.StagWorldToTileXY
* @private
* @since 3.0.0
*
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @param {boolean} [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
* @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.
*
* @return {Phaser.Math.Vector2} The XY location in tile units.
*/
var StagWorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
{
if (point === undefined) { point = new Vector2(0, 0); }
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
// console.log(1,worldY)
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
// console.log(worldY)
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.y = snapToFloor
? Math.floor((worldY / (tileHeight / 2)))
: (worldY / (tileHeight / 2));
point.x = snapToFloor
? Math.floor((worldX + (point.y % 2) * 0.5 * tileWidth) / tileWidth)
: (worldX + (point.y % 2) * 0.5 * tileWidth) / tileWidth;
} }
return point; return point;
}; };
var WorldToTileXY = function (orientation)
{
switch (orientation)
{
case CONST.STAGGERED:
return StagWorldToTileXY;
case CONST.ISOMETRIC:
return IsoWorldToTileXY;
case CONST.HEXAGONAL:
return HexWorldToTileXY;
default:
return OrthoWorldToTileXY;
}
};
module.exports = WorldToTileXY; module.exports = WorldToTileXY;

View file

@ -4,11 +4,13 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../const.js');
/** /**
* Converts from world Y coordinates (pixels) to tile Y coordinates (tile units), factoring in the * Converts from world Y coordinates (pixels) to orthogonal tile Y coordinates (tile units), factoring in the
* layer's position, scale and scroll. * layer's position, scale and scroll.
* *
* @function Phaser.Tilemaps.Components.WorldToTileY * @function Phaser.Tilemaps.Components.OrthoWorldToTileY
* @private * @private
* @since 3.0.0 * @since 3.0.0
* *
@ -19,9 +21,9 @@
* *
* @return {number} The Y location in tile units. * @return {number} The Y location in tile units.
*/ */
var WorldToTileY = function (worldY, snapToFloor, camera, layer) var OrthoWorldToTileY = function (worldY, snapToFloor, camera, layer)
{ {
var orientation = layer.orientation;
if (snapToFloor === undefined) { snapToFloor = true; } if (snapToFloor === undefined) { snapToFloor = true; }
var tileHeight = layer.baseTileHeight; var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer; var tilemapLayer = layer.tilemapLayer;
@ -38,33 +40,117 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer)
} }
if (orientation === 'orthogonal')
{
return snapToFloor return snapToFloor
? Math.floor(worldY / tileHeight) ? Math.floor(worldY / tileHeight)
: worldY / tileHeight; : worldY / tileHeight;
}
else if (orientation === 'isometric') };
/**
* Converts from world Y coordinates (pixels) to staggered tile Y coordinates (tile units), factoring in the
* layer's position, scale and scroll.
*
* @function Phaser.Tilemaps.Components.StagWorldToTileY
* @private
* @since 3.2.2
*
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @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.
*
* @return {number} The Y location in tile units.
*/
var StagWorldToTileY = function (worldY, snapToFloor, camera, layer)
{
if (snapToFloor === undefined) { snapToFloor = true; }
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer;
if (tilemapLayer)
{ {
console.warn('With standard isometric map types you have to use the WorldToTileXY function.'); if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
return null;
// 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;
} }
else if (orientation === 'staggered')
{
return snapToFloor return snapToFloor
? Math.floor(worldY / (tileHeight / 2)) ? Math.floor(worldY / (tileHeight / 2))
: worldY / (tileHeight / 2); : worldY / (tileHeight / 2);
} };
else if (orientation === 'hexagonal')
/**
* Converts from world Y coordinates (pixels) to hexagonal tile Y coordinates (tile units), factoring in the
* layer's position, scale and scroll.
*
* @function Phaser.Tilemaps.Components.HexWorldToTileY
* @private
* @since 3.2.2
*
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
* @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.
*
* @return {number} The Y location in tile units.
*/
var HexWorldToTileY = function (worldY, snapToFloor, camera, layer)
{
if (snapToFloor === undefined) { snapToFloor = true; }
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;
}
var sidel = layer.hexSideLength; var sidel = layer.hexSideLength;
var rowHeight = ((tileHeight - sidel) / 2 + sidel); var rowHeight = ((tileHeight - sidel) / 2 + sidel);
return snapToFloor return snapToFloor
? Math.floor(worldY / rowHeight) ? Math.floor(worldY / rowHeight)
: worldY / rowHeight; : worldY / rowHeight;
};
var nullFunc = function ()
{
console.warn('With the current map type you have to use the WorldToTileXY function.');
return null;
};
var WorldToTileY = function (orientation)
{
switch (orientation)
{
case CONST.ORTHOGONAL:
return OrthoWorldToTileY;
case CONST.HEXAGONAL:
return HexWorldToTileY;
case CONST.STAGGERED:
return StagWorldToTileY;
default:
return nullFunc;
} }
}; };
module.exports = WorldToTileY; module.exports = WorldToTileY;

View file

@ -261,9 +261,6 @@ var DynamicTilemapLayer = new Class({
this.setSize(tilemap.tileWidth * this.layer.width, tilemap.tileHeight * this.layer.height); this.setSize(tilemap.tileWidth * this.layer.width, tilemap.tileHeight * this.layer.height);
this.initPipeline('TextureTintPipeline'); this.initPipeline('TextureTintPipeline');
console.log('layer sizes');
console.log(this.layer.tileWidth,this.layer.tileHeight);
}, },
/** /**
@ -1194,7 +1191,7 @@ var DynamicTilemapLayer = new Class({
*/ */
tileToWorldX: function (tileX, camera) tileToWorldX: function (tileX, camera)
{ {
return TilemapComponents.TileToWorldX(tileX, camera, this.layer); return this.tilemap.TileToWorldX(tileX, camera, this.layer);
}, },
/** /**
@ -1211,7 +1208,7 @@ var DynamicTilemapLayer = new Class({
*/ */
tileToWorldY: function (tileY, camera) tileToWorldY: function (tileY, camera)
{ {
return TilemapComponents.TileToWorldY(tileY, camera, this.layer); return this.tilemap.TileToWorldY(tileY, camera, this.layer);
}, },
/** /**
@ -1231,7 +1228,7 @@ var DynamicTilemapLayer = new Class({
*/ */
tileToWorldXY: function (tileX, tileY, point, camera) tileToWorldXY: function (tileX, tileY, point, camera)
{ {
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, this.layer); return this.tilemap.TileToWorldXY(tileX, tileY, point, camera, this.layer);
}, },
/** /**
@ -1284,7 +1281,7 @@ var DynamicTilemapLayer = new Class({
*/ */
worldToTileX: function (worldX, snapToFloor, camera) worldToTileX: function (worldX, snapToFloor, camera)
{ {
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer); return this.tilemap.WorldToTileX(worldX, snapToFloor, camera, this.layer);
}, },
/** /**
@ -1302,7 +1299,7 @@ var DynamicTilemapLayer = new Class({
*/ */
worldToTileY: function (worldY, snapToFloor, camera) worldToTileY: function (worldY, snapToFloor, camera)
{ {
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer); return this.tilemap.WorldToTileY(worldY, snapToFloor, camera, this.layer);
}, },
/** /**
@ -1323,7 +1320,7 @@ var DynamicTilemapLayer = 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 this.tilemap.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, this.layer);
} }
}); });

View file

@ -4,6 +4,8 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../const.js');
/** /**
* Renders this Game Object with the Canvas Renderer to the given Camera. * Renders this Game Object with the Canvas Renderer to the given Camera.
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera. * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@ -92,7 +94,7 @@ var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPe
var width = tile.width; var width = tile.width;
var height = tile.width; var height = tile.width;
if (src.layer.orientation === 'isometric' || src.layer.orientation === 'staggered' || src.layer.orientation === 'hexagonal') if (src.layer.orientation === CONST.ISOMETRIC || src.layer.orientation === CONST.STAGGERED || src.layer.orientation === CONST.HEXAGONAL)
{ {
// we use the tileset width and height because in isometric and hexagonal maps the tileset's height is often different from the tilemap's. // we use the tileset width and height because in isometric and hexagonal maps the tileset's height is often different from the tilemap's.
width = tileset.tileWidth; width = tileset.tileWidth;

View file

@ -4,7 +4,9 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var Utils = require('../../renderer/webgl/Utils'); var Utils = require('../../renderer/webgl/Utils');
var CONST = require('../../const.js');
/** /**
* Renders this Game Object with the WebGL Renderer to the given Camera. * Renders this Game Object with the WebGL Renderer to the given Camera.
@ -78,7 +80,7 @@ var DynamicTilemapLayerWebGLRenderer = function (renderer, src, interpolationPer
var frameWidth = 0; var frameWidth = 0;
var frameHeight = 0; var frameHeight = 0;
if (src.layer.orientation === 'isometric' || src.layer.orientation === 'staggered' || src.layer.orientation === 'hexagonal') if (src.layer.orientation === CONST.ISOMETRIC || src.layer.orientation === CONST.STAGGERED || src.layer.orientation === CONST.HEXAGONAL)
{ {
// we use the tileset width and height because in isometric maps the tileset's height is often different from the tilemap's. // we use the tileset width and height because in isometric maps the tileset's height is often different from the tilemap's.
frameWidth = tileset.tileWidth; frameWidth = tileset.tileWidth;

View file

@ -6,6 +6,7 @@
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var GetFastValue = require('../../utils/object/GetFastValue'); var GetFastValue = require('../../utils/object/GetFastValue');
var CONST = require('../../const.js');
/** /**
* @classdesc * @classdesc
@ -116,7 +117,7 @@ var LayerData = new Class({
* @type {string} * @type {string}
* @since 3.23beta.PR_svipal * @since 3.23beta.PR_svipal
*/ */
this.orientation = GetFastValue(config, 'orientation', 'orthogonal'); this.orientation = GetFastValue(config, 'orientation', CONST.ORTHOGONAL);
/** /**
* The width in pixels of the entire layer. * The width in pixels of the entire layer.

View file

@ -6,6 +6,7 @@
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var GetFastValue = require('../../utils/object/GetFastValue'); var GetFastValue = require('../../utils/object/GetFastValue');
var CONST = require('../../const.js');
/** /**
* @classdesc * @classdesc
@ -116,7 +117,7 @@ var MapData = new Class({
* @type {string} * @type {string}
* @since 3.0.0 * @since 3.0.0
*/ */
this.orientation = GetFastValue(config, 'orientation', 'orthogonal'); this.orientation = GetFastValue(config, 'orientation', CONST.ORTHOGONAL);
/** /**
* Determines the draw order of tilemap. Default is right-down * Determines the draw order of tilemap. Default is right-down

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../../const.js');
var Formats = require('../../Formats'); var Formats = require('../../Formats');
var MapData = require('../../mapdata/MapData'); var MapData = require('../../mapdata/MapData');
var ParseTileLayers = require('./ParseTileLayers'); var ParseTileLayers = require('./ParseTileLayers');
@ -46,6 +47,7 @@ var ParseJSONTiled = function (name, json, insertNull)
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({
width: json.width, width: json.width,
@ -53,7 +55,7 @@ var ParseJSONTiled = function (name, json, insertNull)
name: name, name: name,
tileWidth: json.tilewidth, tileWidth: json.tilewidth,
tileHeight: json.tileheight, tileHeight: json.tileheight,
orientation: json.orientation, orientation: CONST.fromOrientationString(json.orientation),
format: Formats.TILED_JSON, format: Formats.TILED_JSON,
version: json.version, version: json.version,
properties: json.properties, properties: json.properties,
@ -61,7 +63,10 @@ var ParseJSONTiled = function (name, json, insertNull)
infinite: json.infinite infinite: json.infinite
}); });
if (mapData.orientation === 'hexagonal')
if (mapData.orientation === CONST.HEXAGONAL)
{ {
mapData.hexSideLength = json.hexsidelength; mapData.hexSideLength = json.hexsidelength;
} }

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var CONST = require('../../../const.js');
var Base64Decode = require('./Base64Decode'); var Base64Decode = require('./Base64Decode');
var GetFastValue = require('../../../utils/object/GetFastValue'); var GetFastValue = require('../../../utils/object/GetFastValue');
var LayerData = require('../../mapdata/LayerData'); var LayerData = require('../../mapdata/LayerData');
@ -128,10 +129,10 @@ var ParseTileLayers = function (json, insertNull)
alpha: (curGroupState.opacity * curl.opacity), alpha: (curGroupState.opacity * curl.opacity),
visible: (curGroupState.visible && curl.visible), visible: (curGroupState.visible && curl.visible),
properties: GetFastValue(curl, 'properties', {}), properties: GetFastValue(curl, 'properties', {}),
orientation: json.orientation orientation: CONST.fromOrientationString(json.orientation)
}); });
if (layerData.orientation === 'hexagonal') if (layerData.orientation === CONST.HEXAGONAL)
{ {
layerData.hexSideLength = json.hexsidelength; layerData.hexSideLength = json.hexsidelength;
} }
@ -207,10 +208,10 @@ var ParseTileLayers = function (json, insertNull)
alpha: (curGroupState.opacity * curl.opacity), alpha: (curGroupState.opacity * curl.opacity),
visible: (curGroupState.visible && curl.visible), visible: (curGroupState.visible && curl.visible),
properties: GetFastValue(curl, 'properties', {}), properties: GetFastValue(curl, 'properties', {}),
orientation: json.orientation orientation: CONST.fromOrientationString(json.orientation)
}); });
if (layerData.orientation === 'hexagonal') if (layerData.orientation === CONST.HEXAGONAL)
{ {
layerData.hexSideLength = json.hexsidelength; layerData.hexSideLength = json.hexsidelength;
} }

View file

@ -12,6 +12,7 @@ var StaticTilemapLayerRender = require('./StaticTilemapLayerRender');
var TilemapComponents = require('../components'); var TilemapComponents = require('../components');
var TransformMatrix = require('../../gameobjects/components/TransformMatrix'); var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
var Utils = require('../../renderer/webgl/Utils'); var Utils = require('../../renderer/webgl/Utils');
var Vector2 = require('../../math/Vector2');
/** /**
* @classdesc * @classdesc
@ -150,6 +151,18 @@ var StaticTilemapLayer = new Class({
*/ */
this.skipCull = false; this.skipCull = false;
/**
* In isometric mode, you can control the amount of distance (in tiles, from the cameras' borders) that the Cameras before culling.
* By default the camera will allow 1 full tile in all directions.
*
* However, there are some instances when you may wish to adjust this, and changing this variable allows you to do so.
*
* @name Phaser.Tilemaps.StaticTilemapLayer#isoCullDistances
* @type {Phaser.Math.Vector2}
* @since 3.23.0
*/
this.isoCullDistances = new Vector2(1, 1);
/** /**
* Canvas only. * Canvas only.
* *
@ -363,6 +376,7 @@ var StaticTilemapLayer = new Class({
{ {
this.updateVBOData(); this.updateVBOData();
}, this); }, this);
}, },
/** /**
@ -1333,7 +1347,7 @@ var StaticTilemapLayer = new Class({
*/ */
tileToWorldX: function (tileX, camera) tileToWorldX: function (tileX, camera)
{ {
return TilemapComponents.TileToWorldX(tileX, camera, this.layer); return this.tilemap.TileToWorldX(tileX, camera, this.layer);
}, },
/** /**
@ -1350,7 +1364,7 @@ var StaticTilemapLayer = new Class({
*/ */
tileToWorldY: function (tileY, camera) tileToWorldY: function (tileY, camera)
{ {
return TilemapComponents.TileToWorldY(tileY, camera, this.layer); return this.tilemap.TileToWorldY(tileY, camera, this.layer);
}, },
/** /**
@ -1370,7 +1384,7 @@ var StaticTilemapLayer = new Class({
*/ */
tileToWorldXY: function (tileX, tileY, point, camera) tileToWorldXY: function (tileX, tileY, point, camera)
{ {
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, this.layer); return this.tilemap.TileToWorldXY(tileX, tileY, point, camera, this.layer);
}, },
/** /**
@ -1389,7 +1403,7 @@ var StaticTilemapLayer = new Class({
*/ */
worldToTileX: function (worldX, snapToFloor, camera) worldToTileX: function (worldX, snapToFloor, camera)
{ {
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer); return this.tilemap.WorldToTileX(worldX, snapToFloor, camera, this.layer);
}, },
/** /**
@ -1408,7 +1422,7 @@ var StaticTilemapLayer = new Class({
*/ */
worldToTileY: function (worldY, snapToFloor, camera) worldToTileY: function (worldY, snapToFloor, camera)
{ {
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer); return this.tilemap.WorldToTileY(worldY, snapToFloor, camera, this.layer);
}, },
/** /**
@ -1430,7 +1444,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 this.tilemap.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, this.layer);
}, },
/** /**