BlueGit

timfa questward

Files

questward / shared / world / doors.js

doors.js

doors.js 4.4 KiB Download Raw
1import { WoodTier, WOOD_TIER_ORDER, MATERIAL_DISPLAY_NAME } from "../definitions/materialTiers.js";
2
3/** Facing axes placeable in the editor (E/W reuse N/S art). */
4export const DoorFacing = Object.freeze({
5 n: "n",
6 s: "s"
7});
8
9export const DoorOpenness = Object.freeze({
10 open: "open",
11 closed: "closed"
12});
13
14/**
15 * Visual aliases (documentation / future E-W placement):
16 * - n-open ≈ e-closed
17 * - s-open ≈ w-closed
18 * - s-closed ≈ w-open
19 * - n-closed ≈ e-open
20 */
21export const DOOR_FACING_ALIASES = Object.freeze({
22 "n-open": "e-closed",
23 "s-open": "w-closed",
24 "s-closed": "w-open",
25 "n-closed": "e-open"
26});
27
28export const DOOR_FACINGS = Object.freeze([DoorFacing.n, DoorFacing.s]);
29export const DOOR_OPENNESS = Object.freeze([DoorOpenness.closed, DoorOpenness.open]);
30
31/** Asset stem under `blocks/door/` (without wood tier). */
32export function doorAssetStem (facing, open)
33{
34 const face = facing === DoorFacing.n ? "n" : "s";
35 const openness = open ? "open" : "closed";
36 return `door-${face}-${openness}`;
37}
38
39/**
40 * Authored / runtime door state: `${wood}-${facing}-${open|closed}`.
41 * @returns {{ wood: string, facing: string, open: boolean } | null}
42 */
43export function parseDoorState (state)
44{
45 const text = String(state ?? "");
46 const match = /^(pine|birch|oak|ebony|lignum|heartwood|elder)-(n|s)-(open|closed)$/u.exec(text);
47 if (!match)
48 { return null; }
49 return {
50 wood: match[1],
51 facing: match[2],
52 open: match[3] === DoorOpenness.open
53 };
54}
55
56export function composeDoorState (wood, facing, open)
57{
58 const material = WOOD_TIER_ORDER.includes(wood) ? wood : WoodTier.pine;
59 const face = facing === DoorFacing.n ? DoorFacing.n : DoorFacing.s;
60 const openness = open ? DoorOpenness.open : DoorOpenness.closed;
61 return `${material}-${face}-${openness}`;
62}
63
64export function flipDoorState (state)
65{
66 const parsed = parseDoorState(state);
67 if (!parsed)
68 { return null; }
69 return composeDoorState(parsed.wood, parsed.facing, !parsed.open);
70}
71
72/**
73 * Authored resting state (world placement). Passage "open" is the flip of this,
74 * not the open/closed suffix — e.g. resting `s-open` (≈ W-closed) opens to `s-closed`.
75 */
76export function doorRestingState (block)
77{
78 const resting = block?.config?.restingState;
79 if (parseDoorState(resting))
80 { return resting; }
81 return block?.state ?? null;
82}
83
84/** True when the live state is flipped away from resting (someone held it for passage). */
85export function isDoorPassageOpen (block)
86{
87 const current = block?.state;
88 const resting = doorRestingState(block);
89 if (!parseDoorState(current) || !parseDoorState(resting))
90 { return false; }
91 return current !== resting;
92}
93
94export function isDoorOpenState (state)
95{
96 return !!parseDoorState(state)?.open;
97}
98
99/**
100 * Y-sort sub-order within the door's tile (see `ySortLayer` / `Y_SORT_SUBORDER_STRIDE`).
101 * N-closed draws behind blocks/entities; S-closed in front of entity + equipment;
102 * still shares the tile's sortY so the row above stays behind.
103 * Visual open sprites use the default entity sub-order.
104 */
105export const DOOR_SORT_BEHIND = 1;
106/** Above entity body (3) and equipment overlays (4…). */
107export const DOOR_SORT_FRONT = 12;
108
109export function doorSortSubOrder (state, defaultSubOrder = 3)
110{
111 const parsed = parseDoorState(state);
112 if (!parsed || parsed.open)
113 { return defaultSubOrder; }
114 if (parsed.facing === DoorFacing.n)
115 { return DOOR_SORT_BEHIND; }
116 return DOOR_SORT_FRONT;
117}
118
119/**
120 * Passability: traversable when held open for passage (flipped from resting),
121 * or when the mover can open doors (they flip on approach).
122 */
123export function isDoorTraversable (block, { canOpenDoors = false } = {})
124{
125 if (isDoorPassageOpen(block))
126 { return true; }
127 return !!canOpenDoors;
128}
129
130export function listDoorStatesForWood (wood)
131{
132 const states = [];
133 for (const facing of DOOR_FACINGS)
134 {
135 for (const openness of DOOR_OPENNESS)
136 {
137 states.push(composeDoorState(wood, facing, openness === DoorOpenness.open));
138 }
139 }
140 return states;
141}
142
143export function listAllDoorStates ()
144{
145 const states = [];
146 for (const wood of WOOD_TIER_ORDER)
147 { states.push(...listDoorStatesForWood(wood)); }
148 return states;
149}
150
151export function doorPaletteDisplayName (state)
152{
153 const parsed = parseDoorState(state);
154 if (!parsed)
155 { return "Door"; }
156 const wood = MATERIAL_DISPLAY_NAME[parsed.wood] ?? parsed.wood;
157 const face = parsed.facing === DoorFacing.n ? "N" : "S";
158 const open = parsed.open ? "open" : "closed";
159 return `${wood} door (${face} ${open})`;
160}