BlueGit

timfa questward

Files

questward / shared / render / pivot.js

pivot.js

pivot.js 1.6 KiB Download Raw
1import { PIVOT_FROM_BOTTOM_PX } from "../protocol/constants.js";
2
3export function tilePivot (tileX, tileY, tileSize)
4{
5 return {
6 x: tileX * tileSize + tileSize / 2,
7 y: tileY * tileSize + tileSize / 2
8 };
9}
10
11export function spriteCenterFromPivot (pivotX, pivotY, spriteHeight, zoom)
12{
13 const height = spriteHeight * zoom;
14 return {
15 x: pivotX,
16 y: pivotY - (height / 2 - PIVOT_FROM_BOTTOM_PX * zoom)
17 };
18}
19
20export function spriteDrawSize (spriteWidth, spriteHeight, zoom)
21{
22 return {
23 w: spriteWidth * zoom,
24 h: spriteHeight * zoom
25 };
26}
27
28/**
29 * Within one rounded sortY, layers occupy [0, Y_SORT_SUBORDER_STRIDE).
30 * Keep door front / HP bar above entity body + equipment overlays.
31 *
32 * Callers should pass viewport-relative sortY (worldY − viewportOriginY) so
33 * values stay bounded; absolute world pixels grow with tileY×zoom and collide
34 * with UI bands like LAYER_EDGE.
35 */
36export const Y_SORT_SUBORDER_STRIDE = 16;
37
38/** Bias so northern (negative relative) sortY stays above LAYER_GROUND (0). */
39export const Y_SORT_BASE = 100_000;
40
41export function ySortLayer (sortY, subOrder)
42{
43 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
44 return Y_SORT_BASE + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
45}
46
47/**
48 * Free-flight sprites (e.g. pigeons) must draw above all ground y-sorted
49 * blocks/entities. Bias sits well above {@link ySortLayer} for any in-view tile.
50 */
51export const FLIGHT_SORT_BIAS = 2_000_000;
52
53export function flightSortLayer (sortY, subOrder = 0)
54{
55 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
56 return FLIGHT_SORT_BIAS + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
57}