BlueGit

timfa questward

Files

questward / shared / render / pivot.js

pivot.js

pivot.js 1.3 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 */
32export const Y_SORT_SUBORDER_STRIDE = 16;
33
34export function ySortLayer (sortY, subOrder)
35{
36 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
37 return 10000 + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
38}
39
40/**
41 * Free-flight sprites (e.g. pigeons) must draw above all ground y-sorted
42 * blocks/entities. Bias sits well above {@link ySortLayer} for any in-view tile.
43 */
44export const FLIGHT_SORT_BIAS = 2_000_000;
45
46export function flightSortLayer (sortY, subOrder = 0)
47{
48 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
49 return FLIGHT_SORT_BIAS + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
50}