Merge pull request #4992 from svipal/master

Preliminary PR for isometric support
This commit is contained in:
Richard Davey 2020-10-02 09:42:47 +01:00 committed by GitHub
commit c3c35322c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 1708 additions and 291 deletions

View file

@ -131,7 +131,69 @@ var CONST = {
* @type {integer}
* @since 3.0.0
*/
RIGHT: 8
RIGHT: 8,
/**
* Orientation constant.
*
* @name Phaser.ORTHOGONAL
* @const
* @type {integer}
* @since 3.2.2
*/
ORTHOGONAL: 0,
/**
* Orientation constant.
*
* @name Phaser.ISOMETRIC
* @const
* @type {integer}
* @since 3.2.2
*/
ISOMETRIC: 1,
/**
* Orientation constant.
*
* @name Phaser.STAGGERED
* @const
* @type {integer}
* @since 3.2.2
*/
STAGGERED: 2,
/**
* Orientation constant.
*
* @name Phaser.HEXAGONAL
* @const
* @type {integer}
* @since 3.2.2
*/
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;
}
};

View file

@ -2254,8 +2254,10 @@ var World = new Class({
tilemapLayer = tile.tilemapLayer;
tileWorldRect.left = tilemapLayer.tileToWorldX(tile.x);
tileWorldRect.top = tilemapLayer.tileToWorldY(tile.y);
var point = tilemapLayer.tileToWorldXY(tile.x,tile.y);
tileWorldRect.left = point.x;
tileWorldRect.top = point.y;
// If the map's base tile size differs from the layer's tile size, only the top of the rect
// needs to be adjusted since its origin is (0, 1).

View file

@ -1,30 +1,30 @@
#define SHADER_NAME PHASER_MULTI_FS
precision mediump float;
uniform sampler2D uMainSampler[%count%];
varying vec2 outTexCoord;
varying float outTexId;
varying float outTintEffect;
varying vec4 outTint;
void main()
{
vec4 texture;
%forloop%
vec4 texel = vec4(outTint.bgr * outTint.a, outTint.a);
// Multiply texture tint
vec4 color = texture * texel;
if (outTintEffect == 1.0)
{
// Solid color + texture alpha
color.rgb = mix(texture.rgb, outTint.bgr * outTint.a, texture.a);
}
gl_FragColor = color;
}
#define SHADER_NAME PHASER_MULTI_FS
precision mediump float;
uniform sampler2D uMainSampler[%count%];
varying vec2 outTexCoord;
varying float outTexId;
varying float outTintEffect;
varying vec4 outTint;
void main()
{
vec4 texture;
%forloop%
vec4 texel = vec4(outTint.bgr * outTint.a, outTint.a);
// Multiply texture tint
vec4 color = texture * texel;
if (outTintEffect == 1.0)
{
// Solid color + texture alpha
color.rgb = mix(texture.rgb, outTint.bgr * outTint.a, texture.a);
}
gl_FragColor = color;
}

View file

@ -1,29 +1,29 @@
#define SHADER_NAME PHASER_MULTI_VS
precision mediump float;
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
attribute vec2 inPosition;
attribute vec2 inTexCoord;
attribute float inTexId;
attribute float inTintEffect;
attribute vec4 inTint;
varying vec2 outTexCoord;
varying float outTexId;
varying float outTintEffect;
varying vec4 outTint;
void main ()
{
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(inPosition, 1.0, 1.0);
outTexCoord = inTexCoord;
outTexId = inTexId;
outTint = inTint;
outTintEffect = inTintEffect;
}
#define SHADER_NAME PHASER_MULTI_VS
precision mediump float;
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
attribute vec2 inPosition;
attribute vec2 inTexCoord;
attribute float inTexId;
attribute float inTintEffect;
attribute vec4 inTint;
varying vec2 outTexCoord;
varying float outTexId;
varying float outTintEffect;
varying vec4 outTint;
void main ()
{
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(inPosition, 1.0, 1.0);
outTexCoord = inTexCoord;
outTexId = inTexId;
outTint = inTint;
outTintEffect = inTintEffect;
}

View file

@ -1,26 +1,26 @@
#define SHADER_NAME PHASER_SINGLE_FS
precision mediump float;
uniform sampler2D uMainSampler;
varying vec2 outTexCoord;
varying float outTintEffect;
varying vec4 outTint;
void main()
{
vec4 texture = texture2D(uMainSampler, outTexCoord);
vec4 texel = vec4(outTint.bgr * outTint.a, outTint.a);
// Multiply texture tint
vec4 color = texture * texel;
if (outTintEffect == 1.0)
{
// Solid color + texture alpha
color.rgb = mix(texture.rgb, outTint.bgr * outTint.a, texture.a);
}
gl_FragColor = color;
}
#define SHADER_NAME PHASER_SINGLE_FS
precision mediump float;
uniform sampler2D uMainSampler;
varying vec2 outTexCoord;
varying float outTintEffect;
varying vec4 outTint;
void main()
{
vec4 texture = texture2D(uMainSampler, outTexCoord);
vec4 texel = vec4(outTint.bgr * outTint.a, outTint.a);
// Multiply texture tint
vec4 color = texture * texel;
if (outTintEffect == 1.0)
{
// Solid color + texture alpha
color.rgb = mix(texture.rgb, outTint.bgr * outTint.a, texture.a);
}
gl_FragColor = color;
}

View file

@ -1,25 +1,25 @@
#define SHADER_NAME PHASER_SINGLE_VS
precision mediump float;
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
attribute vec2 inPosition;
attribute vec2 inTexCoord;
attribute float inTintEffect;
attribute vec4 inTint;
varying vec2 outTexCoord;
varying float outTintEffect;
varying vec4 outTint;
void main ()
{
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(inPosition, 1.0, 1.0);
outTexCoord = inTexCoord;
outTint = inTint;
outTintEffect = inTintEffect;
}
#define SHADER_NAME PHASER_SINGLE_VS
precision mediump float;
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
attribute vec2 inPosition;
attribute vec2 inTexCoord;
attribute float inTintEffect;
attribute vec4 inTint;
varying vec2 outTexCoord;
varying float outTintEffect;
varying vec4 outTint;
void main ()
{
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(inPosition, 1.0, 1.0);
outTexCoord = inTexCoord;
outTint = inTint;
outTintEffect = inTintEffect;
}

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CONST = require('../const.js');
var Class = require('../utils/Class');
var Components = require('../gameobjects/components');
var Rectangle = require('../geom/rectangle');
@ -706,14 +707,34 @@ var Tile = new Class({
*/
updatePixelXY: function ()
{
// 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
// coordinate needs to be adjusted by the difference.
this.pixelX = this.x * this.baseWidth;
this.pixelY = this.y * this.baseHeight;
// this.pixelY = this.y * this.baseHeight - (this.height - this.baseHeight);
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
// bottom left, while the Phaser renderer assumes the origin is the top left. The y
// coordinate needs to be adjusted by the difference.
this.pixelX = this.x * this.baseWidth;
this.pixelY = this.y * this.baseHeight;
}
return this;
},

View file

@ -251,6 +251,28 @@ var Tilemap = new Class({
* @since 3.0.0
*/
this.currentLayerIndex = 0;
/**
* Optional : Only for hexagonal tilemaps.
* The length of the horizontal sides of the hexagon.
* @name Phaser.Tilemaps.MapData#tiles
* @type {integer}
* @since 3.0.0
*/
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);
},
/**
@ -501,7 +523,8 @@ var Tilemap = new Class({
tileWidth: tileWidth,
tileHeight: tileHeight,
width: width,
height: height
height: height,
orientation: this.orientation
});
var row;
@ -2345,7 +2368,7 @@ var Tilemap = new Class({
if (layer === null) { return null; }
return TilemapComponents.TileToWorldX(tileX, camera, layer);
return this.TileToWorldX(tileX, camera, layer);
},
/**
@ -2370,7 +2393,7 @@ var Tilemap = new Class({
if (layer === null) { return null; }
return TilemapComponents.TileToWorldY(tileX, camera, layer);
return this.TileToWorldY(tileX, camera, layer);
},
/**
@ -2391,13 +2414,14 @@ var Tilemap = new Class({
*
* @return {?Phaser.Math.Vector2} Returns a point, or null if the layer given was invalid.
*/
tileToWorldXY: function (tileX, tileY, point, camera, layer)
{
layer = this.getLayer(layer);
if (layer === null) { return null; }
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, layer);
return this.TileToWorldXY(tileX, tileY, point, camera, layer);
},
/**
@ -2468,7 +2492,7 @@ var Tilemap = new Class({
if (layer === null) { return null; }
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, layer);
return this.WorldToTileX(worldX, snapToFloor, camera, layer);
},
/**
@ -2493,7 +2517,7 @@ var Tilemap = new Class({
if (layer === null) { return null; }
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, layer);
return this.WorldToTileY(worldY, snapToFloor, camera, layer);
},
/**
@ -2521,7 +2545,7 @@ var Tilemap = new Class({
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

@ -0,0 +1,34 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Sets the tiles in the given rectangular area (in tile coordinates) of the layer with the
* specified index. Tiles will be set to collide if the given index is a colliding index.
* Collision information in the region will be recalculated.
*
* @function Phaser.Tilemaps.Components.CheckIsoBounds
* @private
* @since 3.50.iso
*
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to check against.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
*/
var CheckIsoBounds = function (tileX,tileY,layer,camera)
{
var tilemapLayer = layer.tilemapLayer;
var cullDistances = tilemapLayer.isoCullDistances;
var pos = tilemapLayer.tileToWorldXY(tileX,tileY,undefined,camera);
// we always subtract 1/2 of the tile's height/width to make the culling distance start from the center of the tiles.
return pos.x > camera.worldView.x + tilemapLayer.scaleX * layer.tileWidth * (- cullDistances.x - 1 / 2)
&& pos.x < camera.worldView.right + tilemapLayer.scaleX * layer.tileWidth * (cullDistances.x - 1 / 2)
&& pos.y > camera.worldView.y + tilemapLayer.scaleY * layer.tileHeight * (- cullDistances.y - 1 / 2)
&& pos.y < camera.worldView.bottom + tilemapLayer.scaleY * layer.tileHeight * (cullDistances.y - 1 / 2);
};
module.exports = CheckIsoBounds;

View file

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

View file

@ -4,14 +4,156 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CONST = require('../../const.js');
var SnapFloor = require('../../math/snap/SnapFloor');
var SnapCeil = require('../../math/snap/SnapCeil');
var CheckIsoBounds = require('./CheckIsoBounds');
var GetCullBounds = require('./GetCullBounds');
/**
* Orientation-Decorated function that returns the tiles in the given layer that are within the camera's viewport. This is used internally.
*
* @function Phaser.Tilemaps.Components.GetStandardCullTiles
* @since 3.50.iso
* --- decorator:
* @param {function} getCullBounds
* --- inner function :
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
* @param {array} [outputArray] - An optional array to store the Tile objects within.
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var GetStandardCullTiles = function (getCullBounds)
{
console.log('getting standard cull tiles function');
var StandardCullTiles = function (layer, camera, outputArray, renderOrder)
{
if (outputArray === undefined) { outputArray = []; }
if (renderOrder === undefined) { renderOrder = 0; }
console.log('culling tiles');
outputArray.length = 0;
var tilemapLayer = layer.tilemapLayer;
var mapData = layer.data;
var mapWidth = layer.width;
var mapHeight = layer.height;
var drawLeft = 0;
var drawRight = mapWidth;
var drawTop = 0;
var drawBottom = mapHeight;
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
{
// Camera world view bounds, snapped for scaled tile size
// Cull Padding values are given in tiles, not pixels
var bounds = getCullBounds(layer,camera);
drawLeft = Math.max(0, bounds.left);
drawRight = Math.min(mapWidth, bounds.right);
drawTop = Math.max(0, bounds.top);
drawBottom = Math.min(mapHeight, bounds.bottom);
}
var x;
var y;
var tile;
if (renderOrder === 0)
{
// right-down
for (y = drawTop; y < drawBottom; y++)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
else if (renderOrder === 1)
{
// left-down
for (y = drawTop; y < drawBottom; y++)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
else if (renderOrder === 2)
{
// right-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
else if (renderOrder === 3)
{
// left-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
tilemapLayer.tilesDrawn = outputArray.length;
tilemapLayer.tilesTotal = mapWidth * mapHeight;
return outputArray;
};
return StandardCullTiles;
};
/**
* Returns the tiles in the given layer that are within the camera's viewport. This is used internally.
*
* @function Phaser.Tilemaps.Components.CullTiles
* @since 3.0.0
* @function Phaser.Tilemaps.Components.IsoCullTiles
* @since 3.50.iso
*
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
@ -19,7 +161,7 @@ var SnapCeil = require('../../math/snap/SnapCeil');
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var CullTiles = function (layer, camera, outputArray, renderOrder)
var IsoCullTiles = function (layer, camera, outputArray, renderOrder)
{
if (outputArray === undefined) { outputArray = []; }
if (renderOrder === undefined) { renderOrder = 0; }
@ -46,97 +188,218 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
{
// Camera world view bounds, snapped for scaled tile size
// Cull Padding values are given in tiles, not pixels
if (layer.orientation === CONST.ORTHOGONAL || layer.orientation === CONST.STAGGERED || layer.orientation === CONST.HEXAGONAL)
{
// Camera world view bounds, snapped for scaled tile size
// Cull Padding values are given in tiles, not pixels
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
var boundsTop;
var boundsBottom;
drawLeft = Math.max(0, boundsLeft);
drawRight = Math.min(mapWidth, boundsRight);
drawTop = Math.max(0, boundsTop);
drawBottom = Math.min(mapHeight, boundsBottom);
if (layer.orientation === CONST.ORTHOGONAL)
{
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
}
else if (layer.orientation === CONST.STAGGERED)
{
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;
}
else if (layer.orientation === CONST.HEXAGONAL)
{
var sidel = layer.hexSideLength;
var rowH = ((tileH - sidel) / 2 + sidel);
boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, rowH, 0, true) - tilemapLayer.cullPaddingY;
boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, rowH, 0, true) + tilemapLayer.cullPaddingY;
}
drawLeft = Math.max(0, boundsLeft);
drawRight = Math.min(mapWidth, boundsRight);
drawTop = Math.max(0, boundsTop);
drawBottom = Math.min(mapHeight, boundsBottom);
}
}
var x;
var y;
var tile;
if (renderOrder === 0)
if (layer.orientation === CONST.ORTHOGONAL || layer.orientation === CONST.STAGGERED || layer.orientation === CONST.HEXAGONAL)
{
// right-down
for (y = drawTop; y < drawBottom; y++)
if (renderOrder === 0)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
// right-down
for (y = drawTop; y < drawBottom; y++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
continue;
}
tile = mapData[y][x];
outputArray.push(tile);
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
else if (renderOrder === 1)
{
// left-down
for (y = drawTop; y < drawBottom; y++)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
else if (renderOrder === 2)
{
// right-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
else if (renderOrder === 3)
{
// left-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
else if (renderOrder === 1)
else if (layer.orientation === CONST.ISOMETRIC)
{
// left-down
for (y = drawTop; y < drawBottom; y++)
if (renderOrder === 0)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
// right-down
for (y = drawTop; y < drawBottom; y++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
continue;
}
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
outputArray.push(tile);
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
}
else if (renderOrder === 2)
{
// right-up
for (y = drawBottom; y >= drawTop; y--)
else if (renderOrder === 1)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
// left-down
for (y = drawTop; y < drawBottom; y++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
continue;
}
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
outputArray.push(tile);
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
}
else if (renderOrder === 3)
{
// left-up
for (y = drawBottom; y >= drawTop; y--)
else if (renderOrder === 2)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
// right-up
for (y = drawBottom; y >= drawTop; y--)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
continue;
}
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
outputArray.push(tile);
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
else if (renderOrder === 3)
{
// left-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
}
@ -147,4 +410,15 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
return outputArray;
};
var CullTiles = function (orientation)
{
switch (orientation)
{
case CONST.ISOMETRIC:
return IsoCullTiles;
default:
return GetStandardCullTiles(GetCullBounds(orientation));
}
};
module.exports = CullTiles;

View file

@ -0,0 +1,149 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CONST = require('../../const.js');
var SnapFloor = require('../../math/snap/SnapFloor');
var SnapCeil = require('../../math/snap/SnapCeil');
/**
* Returns the bounds in the given layer that are within the camera's viewport.
* This is used internally by the cull tiles function.
*
* @function Phaser.Tilemaps.Components.OrthoGetCullBounds
* @since 3.50.iso
*
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var OrthoGetCullBounds = function (layer, camera)
{
var tilemap = layer.tilemapLayer.tilemap;
var tilemapLayer = layer.tilemapLayer;
// We need to use the tile sizes defined for the map as a whole, not the layer,
// in order to calculate the bounds correctly. As different sized tiles may be
// placed on the grid and we cannot trust layer.baseTileWidth to give us the true size.
var tileW = Math.floor(tilemap.tileWidth * tilemapLayer.scaleX);
var tileH = Math.floor(tilemap.tileHeight * tilemapLayer.scaleY);
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
return {
left: boundsLeft,
right: boundsRight,
top: boundsTop,
bottom: boundsBottom
};
};
/**
* Returns the bounds in the given layer that are within the camera's viewport.
* This is used internally by the cull tiles function.
*
* @function Phaser.Tilemaps.Components.HexaGetCullBounds
* @since 3.0.0
*
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var HexaGetCullBounds = function (layer, camera)
{
var tilemap = layer.tilemapLayer.tilemap;
var tilemapLayer = layer.tilemapLayer;
// We need to use the tile sizes defined for the map as a whole, not the layer,
// in order to calculate the bounds correctly. As different sized tiles may be
// placed on the grid and we cannot trust layer.baseTileWidth to give us the true size.
var tileW = Math.floor(tilemap.tileWidth * tilemapLayer.scaleX);
var tileH = Math.floor(tilemap.tileHeight * tilemapLayer.scaleY);
var sidel = layer.hexSideLength;
var rowH = ((tileH - sidel) / 2 + sidel);
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, rowH, 0, true) - tilemapLayer.cullPaddingY;
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, rowH, 0, true) + tilemapLayer.cullPaddingY;
return {
left: boundsLeft,
right: boundsRight,
top: boundsTop,
bottom: boundsBottom
};
};
/**
* Returns the bounds in the given layer that are within the camera's viewport.
* This is used internally by the cull tiles function.
*
* @function Phaser.Tilemaps.Components.StagGetCullBounds
* @since 3.0.0
*
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var StagGetCullBounds = function (layer, camera)
{
var tilemap = layer.tilemapLayer.tilemap;
var tilemapLayer = layer.tilemapLayer;
// We need to use the tile sizes defined for the map as a whole, not the layer,
// in order to calculate the bounds correctly. As different sized tiles may be
// placed on the grid and we cannot trust layer.baseTileWidth to give us the true size.
var tileW = Math.floor(tilemap.tileWidth * tilemapLayer.scaleX);
var tileH = Math.floor(tilemap.tileHeight * tilemapLayer.scaleY);
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH / 2, 0, true) - tilemapLayer.cullPaddingY;
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH / 2, 0, true) + tilemapLayer.cullPaddingY;
return {
left: boundsLeft,
right: boundsRight,
top: boundsTop,
bottom: boundsBottom
};
};
var nullFunc = function ()
{
console.warn('cull bounds are *really* not supported by the current map type');
return null;
};
var GetCullBounds = function (orientation)
{
switch (orientation)
{
case CONST.ORTHOGONAL:
return OrthoGetCullBounds;
case CONST.HEXAGONAL:
return HexaGetCullBounds;
case CONST.STAGGERED:
return StagGetCullBounds;
default:
console.warn('cull bounds are not supported by the current map type');
return nullFunc;
}
};
module.exports = GetCullBounds;

View file

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

View file

@ -8,10 +8,7 @@ var Geom = require('../../geom/');
var GetTilesWithin = require('./GetTilesWithin');
var Intersects = require('../../geom/intersects/');
var NOOP = require('../../utils/NOOP');
var TileToWorldX = require('./TileToWorldX');
var TileToWorldY = require('./TileToWorldY');
var WorldToTileX = require('./WorldToTileX');
var WorldToTileY = require('./WorldToTileY');
var TriangleToRectangle = function (triangle, rect)
{
@ -73,12 +70,14 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
}
// Top left corner of the shapes's bounding box, rounded down to include partial tiles
var xStart = WorldToTileX(shape.left, true, camera, layer);
var yStart = WorldToTileY(shape.top, true, camera, layer);
var pointStart = layer.tilemapLayer.worldToTileXY(shape.left, shape.top, true, undefined, camera);
var xStart = pointStart.x;
var yStart = pointStart.y;
// Bottom right corner of the shapes's bounding box, rounded up to include partial tiles
var xEnd = Math.ceil(WorldToTileX(shape.right, false, camera, layer));
var yEnd = Math.ceil(WorldToTileY(shape.bottom, false, camera, layer));
var pointEnd = layer.tilemapLayer.worldToTileXY(shape.right, shape.bottom, true, undefined, camera);
var xEnd = Math.ceil(pointEnd.x);
var yEnd = Math.ceil(pointEnd.y);
// Tiles within bounding rectangle of shape. Bounds are forced to be at least 1 x 1 tile in size
// to grab tiles for shapes that don't have a height or width (e.g. a horizontal line).
@ -101,10 +100,9 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
for (var i = 0; i < tiles.length; i++)
{
var tile = tiles[i];
tileRect.x = TileToWorldX(tile.x, camera, layer);
tileRect.y = TileToWorldY(tile.y, camera, layer);
var point = layer.tilemapLayer.tileToWorldXY(tile.x, tile.y, undefined, camera, layer);
tileRect.x = point.x;
tileRect.y = point.y;
if (intersectTest(shape, tileRect))
{
results.push(tile);

View file

@ -5,8 +5,6 @@
*/
var GetTilesWithin = require('./GetTilesWithin');
var WorldToTileX = require('./WorldToTileX');
var WorldToTileY = require('./WorldToTileY');
/**
* Gets the tiles in the given rectangular area (in world coordinates) of the layer.
@ -29,13 +27,16 @@ var WorldToTileY = require('./WorldToTileY');
*/
var GetTilesWithinWorldXY = function (worldX, worldY, width, height, filteringOptions, camera, layer)
{
// Top left corner of the rect, rounded down to include partial tiles
var xStart = WorldToTileX(worldX, true, camera, layer);
var yStart = WorldToTileY(worldY, true, camera, layer);
var pointStart = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
var xStart = pointStart.x;
var yStart = pointStart.y;
// Bottom right corner of the rect, rounded up to include partial tiles
var xEnd = Math.ceil(WorldToTileX(worldX + width, false, camera, layer));
var yEnd = Math.ceil(WorldToTileY(worldY + height, false, camera, layer));
var pointEnd = layer.tilemapLayer.worldToTileXY(worldX + width, worldY + height, false, undefined, camera);
var xEnd = Math.ceil(pointEnd.x);
var yEnd = Math.ceil(pointEnd.y);
return GetTilesWithin(xStart, yStart, xEnd - xStart, yEnd - yStart, filteringOptions, layer);
};

View file

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

View file

@ -0,0 +1,149 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CheckIsoBounds = require('./CheckIsoBounds');
/**
* Returns the tiles in the given layer that are within the camera's viewport. This is used internally.
*
* @function Phaser.Tilemaps.Components.IsoCullTiles
* @private
* @since 3.0.0
*
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
* @param {array} [outputArray] - An optional array to store the Tile objects within.
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var IsoCullTiles = function (layer, camera, outputArray, renderOrder)
{
if (outputArray === undefined) { outputArray = []; }
if (renderOrder === undefined) { renderOrder = 0; }
outputArray.length = 0;
var tilemapLayer = layer.tilemapLayer;
var mapData = layer.data;
var mapWidth = layer.width;
var mapHeight = layer.height;
var drawLeft = 0;
var drawRight = mapWidth;
var drawTop = 0;
var drawBottom = mapHeight;
if (!tilemapLayer.skipCull)
{
var x;
var y;
var tile;
if (renderOrder === 0)
{
// right-down
for (y = drawTop; y < drawBottom; y++)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
else if (renderOrder === 1)
{
// left-down
for (y = drawTop; y < drawBottom; y++)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
else if (renderOrder === 2)
{
// right-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawLeft; mapData[y] && x < drawRight; x++)
{
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
else if (renderOrder === 3)
{
// left-up
for (y = drawBottom; y >= drawTop; y--)
{
for (x = drawRight; mapData[y] && x >= drawLeft; x--)
{
if (CheckIsoBounds(x,y,layer,camera))
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
}
}
}
tilemapLayer.tilesDrawn = outputArray.length;
tilemapLayer.tilesTotal = mapWidth * mapHeight;
return outputArray;
};
module.exports = IsoCullTiles;

View file

@ -38,7 +38,7 @@ var PutTileAt = function (tile, tileX, tileY, recalculateFaces, layer)
{
if (layer.data[tileY][tileX] === null)
{
layer.data[tileY][tileX] = new Tile(layer, tile.index, tileX, tileY, tile.width, tile.height);
layer.data[tileY][tileX] = new Tile(layer, tile.index, tileX, tileY, layer.tileWidth, layer.tileHeight);
}
layer.data[tileY][tileX].copy(tile);

View file

@ -5,8 +5,6 @@
*/
var PutTileAt = require('./PutTileAt');
var WorldToTileX = require('./WorldToTileX');
var WorldToTileY = require('./WorldToTileY');
/**
* Puts a tile at the given world coordinates (pixels) in the specified layer. You can pass in either
@ -23,14 +21,14 @@ var WorldToTileY = require('./WorldToTileY');
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @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.Tilemaps.Tile} The Tile object that was created or added to this map.
*/
var PutTileAtWorldXY = function (tile, worldX, worldY, recalculateFaces, camera, layer)
{
var tileX = WorldToTileX(worldX, true, camera, layer);
var tileY = WorldToTileY(worldY, true, camera, layer);
var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera, layer);
var tileX = point.x;
var tileY = point.y;
return PutTileAt(tile, tileX, tileY, recalculateFaces, layer);
};

View file

@ -22,6 +22,7 @@ var PutTileAt = require('./PutTileAt');
* @param {integer} tileY - The y coordinate, in tiles, not pixels.
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
*
*/
var PutTilesAt = function (tilesArray, tileX, tileY, recalculateFaces, layer)
{

View file

@ -41,7 +41,7 @@ var RemoveTileAt = function (tileX, tileY, replaceWithNull, recalculateFaces, la
}
else
{
layer.data[tileY][tileX] = (replaceWithNull) ? null : new Tile(layer, -1, tileX, tileY, tile.width, tile.height);
layer.data[tileY][tileX] = (replaceWithNull) ? null : new Tile(layer, -1, tileX, tileY, layer.tileWidth, layer.tileHeight);
}
// Recalculate faces only if the removed tile was a colliding tile

View file

@ -5,8 +5,7 @@
*/
var RemoveTileAt = require('./RemoveTileAt');
var WorldToTileX = require('./WorldToTileX');
var WorldToTileY = require('./WorldToTileY');
/**
* Removes the tile at the given world coordinates in the specified layer and updates the layer's
@ -21,14 +20,14 @@ var WorldToTileY = require('./WorldToTileY');
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
* @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.Tilemaps.Tile} The Tile object that was removed.
*/
var RemoveTileAtWorldXY = function (worldX, worldY, replaceWithNull, recalculateFaces, camera, layer)
{
var tileX = WorldToTileX(worldX, true, camera, layer);
var tileY = WorldToTileY(worldY, true, camera, layer);
var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera, layer);
var tileX = point.x;
var tileY = point.y;
return RemoveTileAt(tileX, tileY, replaceWithNull, recalculateFaces, layer);
};

View file

@ -4,8 +4,10 @@
* @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.
*
* @function Phaser.Tilemaps.Components.TileToWorldX
@ -17,7 +19,7 @@
*
* @return {number}
*/
var TileToWorldX = function (tileX, camera, layer)
var OrthoTileToWorldX = function (tileX, camera, layer)
{
var tileWidth = layer.baseTileWidth;
var tilemapLayer = layer.tilemapLayer;
@ -33,6 +35,26 @@ var TileToWorldX = function (tileX, camera, layer)
}
return layerWorldX + tileX * tileWidth;
};
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;

View file

@ -4,8 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var TileToWorldX = require('./TileToWorldX');
var TileToWorldY = require('./TileToWorldY');
var CONST = require('../../const.js');
var Vector2 = require('../../math/Vector2');
/**
@ -13,7 +12,8 @@ var Vector2 = require('../../math/Vector2');
* layer's position, scale and scroll. This will return a new Vector2 object or update the given
* `point` object.
*
* @function Phaser.Tilemaps.Components.TileToWorldXY
* @function Phaser.Tilemaps.Components.OrthoTileToWorldXY
* @private
* @since 3.0.0
*
* @param {integer} tileX - The x coordinate, in tiles, not pixels.
@ -23,15 +23,188 @@ var Vector2 = require('../../math/Vector2');
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
*
* @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;
point.x = TileToWorldX(tileX, camera, layer);
point.y = TileToWorldY(tileY, camera, layer);
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 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 - tileY) * (tileWidth / 2);
point.y = layerWorldY + (tileX + tileY) * (tileHeight / 2);
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.y = layerWorldY + tileY * (tileHeight / 2);
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 rowHeight = ((tileHeight - sidel) / 2 + sidel);
// similar to staggered, because Tiled uses the oddr representation.
point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2);
point.y = layerWorldY + tileY * rowHeight;
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;

View file

@ -4,8 +4,11 @@
* @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.
*
* @function Phaser.Tilemaps.Components.TileToWorldY
@ -17,7 +20,7 @@
*
* @return {number}
*/
var TileToWorldY = function (tileY, camera, layer)
var OrthoTileToWorldY = function (tileY, camera, layer)
{
var tileHeight = layer.baseTileHeight;
var tilemapLayer = layer.tilemapLayer;
@ -26,13 +29,104 @@ var TileToWorldY = function (tileY, camera, layer)
if (tilemapLayer)
{
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
return layerWorldY + tileY * tileHeight;
};
/**
* 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)
{
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
return layerWorldY + tileY * (tileHeight / 2);
};
/**
* 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 rowHeight = ((tileHeight - sidel) / 2 + sidel);
// same as staggered
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;

View file

@ -4,8 +4,10 @@
* @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.
*
* @function Phaser.Tilemaps.Components.WorldToTileX
@ -18,8 +20,9 @@
*
* @return {number} The X location in tile units.
*/
var WorldToTileX = function (worldX, snapToFloor, camera, layer)
var OrthoWorldToTileX = function (worldX, snapToFloor, camera, layer)
{
if (snapToFloor === undefined) { snapToFloor = true; }
var tileWidth = layer.baseTileWidth;
@ -39,6 +42,27 @@ var WorldToTileX = function (worldX, snapToFloor, camera, layer)
return snapToFloor
? Math.floor(worldX / tileWidth)
: worldX / tileWidth;
};
var nullFunc = function ()
{
console.warn('With the current map type you have to use the WorldToTileXY function.');
return null;
};
var WorldToTileX = function (orientation)
{
switch (orientation)
{
case CONST.ORTHOGONAL:
return OrthoWorldToTileX;
default:
return nullFunc;
}
};
module.exports = WorldToTileX;

View file

@ -4,16 +4,18 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var WorldToTileX = require('./WorldToTileX');
var WorldToTileY = require('./WorldToTileY');
var CONST = require('../../const.js');
var WorldToTileX = require('./WorldToTileX').func;
var WorldToTileY = require('./WorldToTileY').func;
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
* `point` object.
*
* @function Phaser.Tilemaps.Components.WorldToTileXY
* @function Phaser.Tilemaps.Components.OrthoWorldToTileXY
* @private
* @since 3.0.0
*
* @param {number} worldX - The x coordinate to be converted, in pixels, not tiles.
@ -25,14 +27,208 @@ var Vector2 = require('../../math/Vector2');
*
* @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)
{
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(CONST.ORTHOGONAL)(worldX, snapToFloor, camera, layer);
point.y = WorldToTileY(CONST.ORTHOGONAL)(worldY, snapToFloor, camera, layer);
return point;
};
/**
* 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 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;
}
worldX -= tileWidth / 2;
point.x = snapToFloor
? Math.floor((worldX / (tileWidth / 2) + worldY / (tileHeight / 2)) / 2)
: ((worldX / (tileWidth / 2) + worldY / (tileHeight / 2)) / 2);
point.y = snapToFloor
? Math.floor((worldY / (tileHeight / 2) - worldX / (tileWidth / 2)) / 2)
: ((worldY / (tileHeight / 2) - worldX / (tileWidth / 2)) / 2);
return point;
};
/**
* 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 rowHeight = ((tileHeight - sidel) / 2 + sidel);
// similar to staggered, because Tiled uses the oddr representation.
point.y = snapToFloor
? Math.floor((worldY / rowHeight))
: (worldY / rowHeight);
point.x = snapToFloor
? Math.floor((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;
};
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;

View file

@ -4,11 +4,14 @@
* @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.
*
* @function Phaser.Tilemaps.Components.WorldToTileY
* @function Phaser.Tilemaps.Components.OrthoWorldToTileY
* @private
* @since 3.0.0
*
* @param {number} worldY - The y coordinate to be converted, in pixels, not tiles.
@ -18,13 +21,13 @@
*
* @return {number} The Y location in tile units.
*/
var WorldToTileY = function (worldY, snapToFloor, camera, layer)
var OrthoWorldToTileY = 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; }
@ -34,11 +37,120 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer)
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
tileHeight *= tilemapLayer.scaleY;
}
return snapToFloor
? Math.floor(worldY / tileHeight)
: worldY / tileHeight;
};
/**
* 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)
{
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;
}
return snapToFloor
? Math.floor(worldY / (tileHeight / 2))
: worldY / (tileHeight / 2);
};
/**
* 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 rowHeight = ((tileHeight - sidel) / 2 + sidel);
return snapToFloor
? Math.floor(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;

View file

@ -9,6 +9,7 @@ var Components = require('../../gameobjects/components');
var DynamicTilemapLayerRender = require('./DynamicTilemapLayerRender');
var GameObject = require('../../gameobjects/GameObject');
var TilemapComponents = require('../components');
var Vector2 = require('../../math/Vector2');
/**
* @classdesc
@ -111,7 +112,7 @@ var DynamicTilemapLayer = new Class({
// Link the LayerData with this static tilemap layer
this.layer.tilemapLayer = this;
/**
* The Tileset/s associated with this layer.
*
@ -146,6 +147,18 @@ var DynamicTilemapLayer = new Class({
*/
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.DynamicTilemapLayer#isoCullDistances
* @type {Phaser.Math.Vector2}
* @since 3.50.iso
*/
this.isoCullDistances = new Vector2(1, 1);
/**
* The total number of tiles drawn by the renderer in the last frame.
*
@ -207,7 +220,7 @@ var DynamicTilemapLayer = new Class({
* @type {function}
* @since 3.11.0
*/
this.cullCallback = TilemapComponents.CullTiles;
this.cullCallback = TilemapComponents.CullTiles(this.layer.orientation);
/**
* The rendering (draw) order of the tiles in this layer.
@ -1178,7 +1191,7 @@ var DynamicTilemapLayer = new Class({
*/
tileToWorldX: function (tileX, camera)
{
return TilemapComponents.TileToWorldX(tileX, camera, this.layer);
return this.tilemap.TileToWorldX(tileX, camera, this.layer);
},
/**
@ -1195,7 +1208,7 @@ var DynamicTilemapLayer = new Class({
*/
tileToWorldY: function (tileY, camera)
{
return TilemapComponents.TileToWorldY(tileY, camera, this.layer);
return this.tilemap.TileToWorldY(tileY, camera, this.layer);
},
/**
@ -1215,7 +1228,7 @@ var DynamicTilemapLayer = new Class({
*/
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);
},
/**
@ -1268,7 +1281,7 @@ var DynamicTilemapLayer = new Class({
*/
worldToTileX: function (worldX, snapToFloor, camera)
{
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer);
return this.tilemap.WorldToTileX(worldX, snapToFloor, camera, this.layer);
},
/**
@ -1286,7 +1299,7 @@ var DynamicTilemapLayer = new Class({
*/
worldToTileY: function (worldY, snapToFloor, camera)
{
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer);
return this.tilemap.WorldToTileY(worldY, snapToFloor, camera, this.layer);
},
/**
@ -1307,7 +1320,7 @@ var DynamicTilemapLayer = new Class({
*/
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

@ -88,8 +88,8 @@ var DynamicTilemapLayerCanvasRenderer = function (renderer, src, camera, parentM
if (tileTexCoords)
{
var halfWidth = tile.width / 2;
var halfHeight = tile.height / 2;
var halfWidth = tileset.tileWidth / 2;
var halfHeight = tileset.tileHeight / 2;
ctx.save();
@ -110,9 +110,9 @@ var DynamicTilemapLayerCanvasRenderer = function (renderer, src, camera, parentM
ctx.drawImage(
image,
tileTexCoords.x, tileTexCoords.y,
tile.width, tile.height,
tileset.tileWidth , tileset.Height,
-halfWidth, -halfHeight,
tile.width, tile.height
tileset.tileWidth , tileset.tileHeight
);
ctx.restore();

View file

@ -75,14 +75,14 @@ var DynamicTilemapLayerWebGLRenderer = function (renderer, src, camera)
continue;
}
var frameWidth = tile.width;
var frameHeight = tile.height;
var frameWidth = tileset.tileWidth;
var frameHeight = tileset.tileHeight;
var frameX = tileTexCoords.x;
var frameY = tileTexCoords.y;
var tw = tile.width * 0.5;
var th = tile.height * 0.5;
var tw = tileset.tileWidth * 0.5;
var th = tileset.tileHeight * 0.5;
var tint = getTint(tile.tint, alpha * tile.alpha);

View file

@ -6,6 +6,7 @@
var Class = require('../../utils/Class');
var GetFastValue = require('../../utils/object/GetFastValue');
var CONST = require('../../const.js');
/**
* @classdesc
@ -109,6 +110,15 @@ var LayerData = new Class({
*/
this.baseTileHeight = GetFastValue(config, 'baseTileHeight', this.tileHeight);
/**
* The layer's orientation, necessary to be able to determine a tile's pixelX and pixelY as well as the layer's width and height.
*
* @name Phaser.Tilemaps.LayerData#orientation
* @type {string}
* @since 3.23beta.PR_svipal
*/
this.orientation = GetFastValue(config, 'orientation', CONST.ORTHOGONAL);
/**
* The width in pixels of the entire layer.
*
@ -207,6 +217,16 @@ var LayerData = new Class({
* @since 3.0.0
*/
this.tilemapLayer = GetFastValue(config, 'tilemapLayer', null);
/**
* Optional : Only for hexagonal tilemaps.
* The length of the horizontal sides of the hexagon.
* @name Phaser.Tilemaps.MapData#tiles
* @type {integer}
* @since 3.0.0
*/
this.hexSideLength = GetFastValue(config, 'hexSideLength', 0);
}
});

View file

@ -6,6 +6,7 @@
var Class = require('../../utils/Class');
var GetFastValue = require('../../utils/object/GetFastValue');
var CONST = require('../../const.js');
/**
* @classdesc
@ -116,7 +117,7 @@ var MapData = new Class({
* @type {string}
* @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
@ -212,6 +213,15 @@ var MapData = new Class({
* @since 3.0.0
*/
this.tiles = GetFastValue(config, 'tiles', []);
/**
* Optional : Only for hexagonal tilemaps.
* The length of the horizontal sides of the hexagon.
* @name Phaser.Tilemaps.MapData#tiles
* @type {integer}
* @since 3.0.0
*/
this.hexSideLength = GetFastValue(config, 'hexSideLength', 0);
}
});

View file

@ -34,7 +34,7 @@ var Parse2DArray = function (name, data, tileWidth, tileHeight, insertNull)
tileWidth: tileWidth,
tileHeight: tileHeight
});
var mapData = new MapData({
name: name,
tileWidth: tileWidth,

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CONST = require('../../../const.js');
var Formats = require('../../Formats');
var MapData = require('../../mapdata/MapData');
var ParseTileLayers = require('./ParseTileLayers');
@ -32,12 +33,21 @@ var AssignTileProperties = require('./AssignTileProperties');
*/
var ParseJSONTiled = function (name, json, insertNull)
{
if (json.orientation !== 'orthogonal')
if (json.orientation === 'isometric' || json.orientation === 'staggered')
{
console.warn('Only orthogonal map types are supported in this version of Phaser');
console.warn('Isometric map types are WIP in this version of Phaser');
}
else if (json.orientation === 'hexagonal')
{
console.warn('Hexagonal map types are WIP in this version of Phaser');
}
else if (json.orientation !== 'orthogonal')
{
console.warn('Only orthogonal, hexagonal and isometric 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({
width: json.width,
@ -45,7 +55,7 @@ var ParseJSONTiled = function (name, json, insertNull)
name: name,
tileWidth: json.tilewidth,
tileHeight: json.tileheight,
orientation: json.orientation,
orientation: CONST.fromOrientationString(json.orientation),
format: Formats.TILED_JSON,
version: json.version,
properties: json.properties,
@ -53,6 +63,14 @@ var ParseJSONTiled = function (name, json, insertNull)
infinite: json.infinite
});
if (mapData.orientation === CONST.HEXAGONAL)
{
mapData.hexSideLength = json.hexsidelength;
}
mapData.layers = ParseTileLayers(json, insertNull);
mapData.images = ParseImageLayers(json);

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CONST = require('../../../const.js');
var Base64Decode = require('./Base64Decode');
var GetFastValue = require('../../../utils/object/GetFastValue');
var LayerData = require('../../mapdata/LayerData');
@ -127,9 +128,16 @@ var ParseTileLayers = function (json, insertNull)
tileHeight: json.tileheight,
alpha: (curGroupState.opacity * curl.opacity),
visible: (curGroupState.visible && curl.visible),
properties: GetFastValue(curl, 'properties', [])
properties: GetFastValue(curl, 'properties', []),
orientation: CONST.fromOrientationString(json.orientation)
});
if (layerData.orientation === CONST.HEXAGONAL)
{
layerData.hexSideLength = json.hexsidelength;
}
for (var c = 0; c < curl.height; c++)
{
output.push([ null ]);
@ -200,9 +208,15 @@ var ParseTileLayers = function (json, insertNull)
tileHeight: json.tileheight,
alpha: (curGroupState.opacity * curl.opacity),
visible: (curGroupState.visible && curl.visible),
properties: GetFastValue(curl, 'properties', [])
properties: GetFastValue(curl, 'properties', []),
orientation: CONST.fromOrientationString(json.orientation)
});
if (layerData.orientation === CONST.HEXAGONAL)
{
layerData.hexSideLength = json.hexsidelength;
}
var row = [];
// Loop through the data field in the JSON.

View file

@ -13,6 +13,7 @@ var StaticTilemapLayerRender = require('./StaticTilemapLayerRender');
var TilemapComponents = require('../components');
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
var Utils = require('../../renderer/webgl/Utils');
var Vector2 = require('../../math/Vector2');
/**
* @classdesc
@ -153,6 +154,18 @@ var StaticTilemapLayer = new Class({
*/
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.
*
@ -368,6 +381,7 @@ var StaticTilemapLayer = new Class({
{
this.updateVBOData();
}, this);
},
/**
@ -1359,7 +1373,7 @@ var StaticTilemapLayer = new Class({
*/
tileToWorldX: function (tileX, camera)
{
return TilemapComponents.TileToWorldX(tileX, camera, this.layer);
return this.tilemap.TileToWorldX(tileX, camera, this.layer);
},
/**
@ -1376,7 +1390,7 @@ var StaticTilemapLayer = new Class({
*/
tileToWorldY: function (tileY, camera)
{
return TilemapComponents.TileToWorldY(tileY, camera, this.layer);
return this.tilemap.TileToWorldY(tileY, camera, this.layer);
},
/**
@ -1396,7 +1410,7 @@ var StaticTilemapLayer = new Class({
*/
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);
},
/**
@ -1415,7 +1429,7 @@ var StaticTilemapLayer = new Class({
*/
worldToTileX: function (worldX, snapToFloor, camera)
{
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer);
return this.tilemap.WorldToTileX(worldX, snapToFloor, camera, this.layer);
},
/**
@ -1434,7 +1448,7 @@ var StaticTilemapLayer = new Class({
*/
worldToTileY: function (worldY, snapToFloor, camera)
{
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, this.layer);
return this.tilemap.WorldToTileY(worldY, snapToFloor, camera, this.layer);
},
/**
@ -1456,7 +1470,7 @@ var StaticTilemapLayer = new Class({
*/
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);
},
/**