| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js"; |
| 4 | |
| 5 | test("a 16×16 sprite pivot sits on the tile centre", () => |
| 6 | { |
| 7 | const pivot = tilePivot(0, 0, 16); |
| 8 | const center = spriteCenterFromPivot(pivot.x, pivot.y, 16, 1); |
| 9 | assert.equal(pivot.x, 8); |
| 10 | assert.equal(pivot.y, 8); |
| 11 | assert.equal(center.x, 8); |
| 12 | assert.equal(center.y, 8); |
| 13 | }); |
| 14 | |
| 15 | test("a taller sprite keeps its feet 8px from the bottom on the tile centre", () => |
| 16 | { |
| 17 | const pivot = tilePivot(0, 0, 16); |
| 18 | const center = spriteCenterFromPivot(pivot.x, pivot.y, 32, 1); |
| 19 | assert.equal(center.x, 8); |
| 20 | assert.equal(center.y, 0); |
| 21 | }); |
| 22 | |
| 23 | test("a 48px-tall tree sprite pivots 8px from its bottom on the tile centre", () => |
| 24 | { |
| 25 | const pivot = tilePivot(0, 0, 16); |
| 26 | const center = spriteCenterFromPivot(pivot.x, pivot.y, 48, 1); |
| 27 | assert.equal(center.x, 8); |
| 28 | assert.equal(center.y, -8); |
| 29 | }); |
| 30 | |
| 31 | test("Y-sort puts a southern tile in front of a northern tile", () => |
| 32 | { |
| 33 | assert.ok(ySortLayer(16, 1) > ySortLayer(0, 1)); |
| 34 | }); |
| 35 | |
| 36 | test("viewport-relative Y-sort stays below UI edge band at high world Y", () => |
| 37 | { |
| 38 | const layerEdge = 900_000; |
| 39 | const focusY = 50_000; |
| 40 | const zoomedTile = 64; |
| 41 | // Player around tileY ≈ 780 at zoom 4 — absolute sort used to exceed LAYER_EDGE. |
| 42 | const worldY = focusY + zoomedTile * 2; |
| 43 | const relative = worldY - focusY; |
| 44 | const layer = ySortLayer(relative, 3); |
| 45 | assert.ok(layer > Y_SORT_BASE); |
| 46 | assert.ok(layer < layerEdge, `expected ${layer} < ${layerEdge}`); |
| 47 | const absoluteWouldOverflow = Y_SORT_BASE + Math.round(worldY) * 16 + 3; |
| 48 | assert.ok(absoluteWouldOverflow > layerEdge); |
| 49 | }); |
| 50 | |
| 51 | test("S-closed door stays in front of entity equipment overlays on the same tile", () => |
| 52 | { |
| 53 | const sortY = 128; |
| 54 | const entityBody = ySortLayer(sortY, 3); |
| 55 | const entityOverlay = ySortLayer(sortY, 3 + 1 + 5); |
| 56 | const doorFront = ySortLayer(sortY, 12); |
| 57 | const doorBehind = ySortLayer(sortY, 1); |
| 58 | assert.ok(doorFront > entityOverlay); |
| 59 | assert.ok(doorFront > entityBody); |
| 60 | assert.ok(entityBody > doorBehind); |
| 61 | assert.ok(ySortLayer(sortY, 14) > doorFront); |
| 62 | }); |
| 63 | |
| 64 | test("flight sort stays above any ground y-sort in view", () => |
| 65 | { |
| 66 | // In-view relative sortY stays within roughly ±screen height in world px. |
| 67 | const farInView = ySortLayer(2_000, 3); |
| 68 | assert.ok(flightSortLayer(0, 0) > farInView); |
| 69 | assert.ok(flightSortLayer(16, 1) > flightSortLayer(0, 1)); |
| 70 | assert.ok(farInView < 900_000); |
| 71 | }); |