| 1 | /** Chebyshev distance between tile centres (0 = same tile). */ |
| 2 | export function chebyshevTileDistance (ax, ay, bx, by) |
| 3 | { |
| 4 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 5 | } |
| 6 | |
| 7 | /** Orthogonal or diagonal neighbour — not the same tile. Block interaction range. */ |
| 8 | export function isAdjacentTile (ax, ay, bx, by) |
| 9 | { |
| 10 | return chebyshevTileDistance(ax, ay, bx, by) === 1; |
| 11 | } |
| 12 | |
| 13 | export function isSameTile (ax, ay, bx, by) |
| 14 | { |
| 15 | return ax === bx && ay === by; |
| 16 | } |
| 17 | |
| 18 | /** True when blockType exists on a tile adjacent to (tileX, tileY), not on that tile. */ |
| 19 | export function hasAdjacentBlockType (getBlockTypeAt, tileX, tileY, blockType) |
| 20 | { |
| 21 | for (let dy = -1; dy <= 1; dy++) |
| 22 | { |
| 23 | for (let dx = -1; dx <= 1; dx++) |
| 24 | { |
| 25 | if (dx === 0 && dy === 0) |
| 26 | { continue; } |
| 27 | if (getBlockTypeAt(tileX + dx, tileY + dy) === blockType) |
| 28 | { return true; } |
| 29 | } |
| 30 | } |
| 31 | return false; |
| 32 | } |