BlueGit

timfa questward

Files

questward / shared / render / pivot.js

pivot.js

pivot.js 1.8 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 origin-relative sortY (worldY − sortOriginY) so values
33 * stay bounded; absolute world pixels grow with tileY×zoom and collide with UI
34 * bands like LAYER_EDGE. The origin tracks the view, but quantised (the client
35 * uses the chunk row at the viewport centre) so it rarely moves — every sprite
36 * has to be re-layered when it does.
37 */
38export const Y_SORT_SUBORDER_STRIDE = 16;
39
40/** Bias so northern (negative relative) sortY stays above LAYER_GROUND (0). */
41export const Y_SORT_BASE = 100_000;
42
43export function ySortLayer (sortY, subOrder)
44{
45 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
46 return Y_SORT_BASE + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
47}
48
49/**
50 * Free-flight sprites (e.g. pigeons) must draw above all ground y-sorted
51 * blocks/entities. Bias sits well above {@link ySortLayer} for any in-view tile.
52 */
53export const FLIGHT_SORT_BIAS = 2_000_000;
54
55export function flightSortLayer (sortY, subOrder = 0)
56{
57 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
58 return FLIGHT_SORT_BIAS + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
59}