BlueGit

timfa questward

Files

questward / shared / world / doors.js

doors.js

doors.js 4.2 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`).
101 * N-closed draws behind blocks/entities; S-closed in front of them;
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 function doorSortSubOrder (state, defaultSubOrder = 3)
106{
107 const parsed = parseDoorState(state);
108 if (!parsed || parsed.open)
109 { return defaultSubOrder; }
110 if (parsed.facing === DoorFacing.n)
111 { return 1; }
112 return 4;
113}
114
115/**
116 * Passability: traversable when held open for passage (flipped from resting),
117 * or when the mover can open doors (they flip on approach).
118 */
119export function isDoorTraversable (block, { canOpenDoors = false } = {})
120{
121 if (isDoorPassageOpen(block))
122 { return true; }
123 return !!canOpenDoors;
124}
125
126export function listDoorStatesForWood (wood)
127{
128 const states = [];
129 for (const facing of DOOR_FACINGS)
130 {
131 for (const openness of DOOR_OPENNESS)
132 {
133 states.push(composeDoorState(wood, facing, openness === DoorOpenness.open));
134 }
135 }
136 return states;
137}
138
139export function listAllDoorStates ()
140{
141 const states = [];
142 for (const wood of WOOD_TIER_ORDER)
143 { states.push(...listDoorStatesForWood(wood)); }
144 return states;
145}
146
147export function doorPaletteDisplayName (state)
148{
149 const parsed = parseDoorState(state);
150 if (!parsed)
151 { return "Door"; }
152 const wood = MATERIAL_DISPLAY_NAME[parsed.wood] ?? parsed.wood;
153 const face = parsed.facing === DoorFacing.n ? "N" : "S";
154 const open = parsed.open ? "open" : "closed";
155 return `${wood} door (${face} ${open})`;
156}