From bc1fcf7357cf1bd7af32bca8eb969f91d6188ea9 Mon Sep 17 00:00:00 2001 From: Rex Date: Sat, 25 Mar 2023 20:47:45 +0800 Subject: [PATCH] Suport staggerIndex-even with staggerAxis-y and staggerAxis-x --- src/tilemaps/Tile.js | 25 ++++++++++++++++--- .../components/HexagonalTileToWorldXY.js | 21 +++++++++++++--- .../components/HexagonalWorldToTileXY.js | 15 ++++++++--- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/tilemaps/Tile.js b/src/tilemaps/Tile.js index c9f7bad3b..f2b768078 100644 --- a/src/tilemaps/Tile.js +++ b/src/tilemaps/Tile.js @@ -781,19 +781,36 @@ var Tile = new Class({ var len = this.layer.hexSideLength; var rowWidth, rowHeight; - if ((staggerAxis === 'y') && (staggerIndex === 'odd')) + if (staggerAxis === 'y') { rowHeight = ((this.baseHeight - len) / 2 + len); - this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2); + if (staggerIndex === 'odd') + { + this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2); + } + else + { + this.pixelX = this.x * this.baseWidth - this.y % 2 * (this.baseWidth / 2); + } + this.pixelY = this.y * rowHeight; } - else if ((staggerAxis === 'x') && (staggerIndex === 'odd')) + else if (staggerAxis === 'x') { rowWidth = ((this.baseWidth - len) / 2 + len); this.pixelX = this.x * rowWidth; - this.pixelY = this.y * this.baseHeight + this.x % 2 * (this.baseHeight / 2); + + if (staggerIndex === 'odd') + { + this.pixelY = this.y * this.baseHeight + this.x % 2 * (this.baseHeight / 2); + } + else + { + this.pixelY = this.y * this.baseHeight - this.x % 2 * (this.baseHeight / 2); + } + } } diff --git a/src/tilemaps/components/HexagonalTileToWorldXY.js b/src/tilemaps/components/HexagonalTileToWorldXY.js index 196c42e38..369ed352c 100644 --- a/src/tilemaps/components/HexagonalTileToWorldXY.js +++ b/src/tilemaps/components/HexagonalTileToWorldXY.js @@ -52,14 +52,21 @@ var HexagonalTileToWorldXY = function (tileX, tileY, point, camera, layer) var x, y; - if ((this.staggerAxis === 'y') && (this.staggerIndex === 'odd')) + if (this.staggerAxis === 'y') { x = worldX + (tileWidth * tileX) + tileWidth; y = worldY + ((1.5 * tileY) * tileHeightHalf) + tileHeightHalf; if (tileY % 2 === 0) { - x -= tileWidthHalf; + if (this.staggerIndex === 'odd') + { + x -= tileWidthHalf; + } + else + { + x += tileWidthHalf; + } } } else if ((this.staggerAxis === 'x') && (this.staggerIndex === 'odd')) @@ -69,7 +76,15 @@ var HexagonalTileToWorldXY = function (tileX, tileY, point, camera, layer) if (tileX % 2 === 0) { - y -= tileHeightHalf; + if (this.staggerIndex === 'odd') + { + y -= tileHeightHalf; + } + else + { + y += tileHeightHalf; + } + } } diff --git a/src/tilemaps/components/HexagonalWorldToTileXY.js b/src/tilemaps/components/HexagonalWorldToTileXY.js index 1a880d6a2..affe771ef 100644 --- a/src/tilemaps/components/HexagonalWorldToTileXY.js +++ b/src/tilemaps/components/HexagonalWorldToTileXY.js @@ -59,7 +59,7 @@ var HexagonalWorldToTileXY = function (worldX, worldY, snapToFloor, point, camer var q, r, s; // size - if (this.staggerAxis === 'y') + if (layer.staggerAxis === 'y') { // x = b0 * tileWidth // y = tileHeightHalf @@ -99,9 +99,18 @@ var HexagonalWorldToTileXY = function (worldX, worldY, snapToFloor, point, camer ri = -qi - si; } - var y = ri; + var x, y; - var x = (y % 2 === 0) ? (ri / 2) + qi : (ri / 2) + qi - 0.5; + y = ri; + + if (layer.staggerIndex === 'odd') + { + x = (y % 2 === 0) ? (ri / 2) + qi : (ri / 2) + qi - 0.5; + } + else + { + x = (y % 2 === 0) ? (ri / 2) + qi : (ri / 2) + qi + 0.5; + } return point.set(x, y); };