import { PIVOT_FROM_BOTTOM_PX } from "../protocol/constants.js"; export function tilePivot (tileX, tileY, tileSize) { return { x: tileX * tileSize + tileSize / 2, y: tileY * tileSize + tileSize / 2 }; } export function spriteCenterFromPivot (pivotX, pivotY, spriteHeight, zoom) { const height = spriteHeight * zoom; return { x: pivotX, y: pivotY - (height / 2 - PIVOT_FROM_BOTTOM_PX * zoom) }; } export function spriteDrawSize (spriteWidth, spriteHeight, zoom) { return { w: spriteWidth * zoom, h: spriteHeight * zoom }; } /** * Within one rounded sortY, layers occupy [0, Y_SORT_SUBORDER_STRIDE). * Keep door front / HP bar above entity body + equipment overlays. * * Callers should pass viewport-relative sortY (worldY − viewportOriginY) so * values stay bounded; absolute world pixels grow with tileY×zoom and collide * with UI bands like LAYER_EDGE. */ export const Y_SORT_SUBORDER_STRIDE = 16; /** Bias so northern (negative relative) sortY stays above LAYER_GROUND (0). */ export const Y_SORT_BASE = 100_000; export function ySortLayer (sortY, subOrder) { const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0)); return Y_SORT_BASE + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot; } /** * Free-flight sprites (e.g. pigeons) must draw above all ground y-sorted * blocks/entities. Bias sits well above {@link ySortLayer} for any in-view tile. */ export const FLIGHT_SORT_BIAS = 2_000_000; export function flightSortLayer (sortY, subOrder = 0) { const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0)); return FLIGHT_SORT_BIAS + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot; }