BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 6.7 KiB Download Raw
1import {
2 ORE_BEARING_MATERIALS,
3 oreBoulderState,
4 MATERIAL_DISPLAY_NAME
5} from "./materialTiers.js";
6import { spriteImageName, resolveState } from "./tileSprites.js";
7
8export const GroundType = Object.freeze({
9 grass: 1,
10 wood: 2,
11 water: 3,
12 deepWater: 4
13});
14
15export const BlockType = Object.freeze({
16 stoneWall: 1,
17 torch: 2,
18 /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */
19 spawner: 3,
20 bank: 4,
21 churchAltar: 5,
22 cauldron: 6,
23 boulder: 7,
24 furnace: 8,
25 anvil: 9,
26 woodworkingBench: 10
27});
28
29export const GROUND_DEFINITIONS = Object.freeze({
30 [GroundType.grass]: Object.freeze({
31 id: GroundType.grass,
32 key: "grass",
33 displayName: "Grass",
34 passable: true,
35 wades: false,
36 water: false,
37 frames: 1,
38 framesPerRow: 1,
39 animationSpeed: 0
40 }),
41 [GroundType.wood]: Object.freeze({
42 id: GroundType.wood,
43 key: "wood",
44 displayName: "Wood",
45 passable: true,
46 wades: false,
47 water: false,
48 frames: 1,
49 framesPerRow: 1,
50 animationSpeed: 0
51 }),
52 [GroundType.water]: Object.freeze({
53 id: GroundType.water,
54 key: "water",
55 displayName: "Water",
56 passable: true,
57 wades: true,
58 water: true,
59 frames: 4,
60 framesPerRow: 4,
61 animationSpeed: 0.12
62 }),
63 [GroundType.deepWater]: Object.freeze({
64 id: GroundType.deepWater,
65 key: "deep-water",
66 displayName: "Deep water",
67 passable: false,
68 wades: false,
69 water: true,
70 frames: 4,
71 framesPerRow: 4,
72 animationSpeed: 0.12
73 })
74});
75
76export const BLOCK_DEFINITIONS = Object.freeze({
77 [BlockType.stoneWall]: Object.freeze({
78 id: BlockType.stoneWall,
79 key: "stone-wall",
80 displayName: "Stone wall",
81 passable: false,
82 lightRangeTiles: 0,
83 frames: 1,
84 framesPerRow: 1,
85 animationSpeed: 0
86 }),
87 [BlockType.torch]: Object.freeze({
88 id: BlockType.torch,
89 key: "torch",
90 displayName: "Torch",
91 passable: true,
92 lightRangeTiles: 8,
93 frames: 1,
94 framesPerRow: 1,
95 animationSpeed: 0
96 }),
97 [BlockType.spawner]: Object.freeze({
98 id: BlockType.spawner,
99 key: "spawner",
100 displayName: "Spawner",
101 passable: true,
102 lightRangeTiles: 0,
103 frames: 1,
104 framesPerRow: 1,
105 animationSpeed: 0,
106 /** Not shown in the placeable block palette; use the Spawners tab. */
107 editorHidden: true
108 }),
109 [BlockType.bank]: Object.freeze({
110 id: BlockType.bank,
111 key: "bank",
112 displayName: "Bank",
113 passable: false,
114 lightRangeTiles: 0,
115 frames: 1,
116 framesPerRow: 1,
117 animationSpeed: 0
118 }),
119 [BlockType.churchAltar]: Object.freeze({
120 id: BlockType.churchAltar,
121 key: "church-altar",
122 displayName: "Church altar",
123 passable: false,
124 lightRangeTiles: 0,
125 frames: 1,
126 framesPerRow: 1,
127 animationSpeed: 0
128 }),
129 [BlockType.cauldron]: Object.freeze({
130 id: BlockType.cauldron,
131 key: "cauldron",
132 displayName: "Cauldron",
133 passable: false,
134 lightRangeTiles: 0,
135 frames: 1,
136 framesPerRow: 1,
137 animationSpeed: 0
138 }),
139 [BlockType.boulder]: Object.freeze({
140 id: BlockType.boulder,
141 key: "boulder",
142 displayName: "Boulder",
143 passable: false,
144 lightRangeTiles: 0,
145 frames: 1,
146 framesPerRow: 1,
147 animationSpeed: 0,
148 /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */
149 gatherable: true
150 }),
151 [BlockType.furnace]: Object.freeze({
152 id: BlockType.furnace,
153 key: "furnace",
154 displayName: "Furnace",
155 passable: false,
156 lightRangeTiles: 0,
157 frames: 1,
158 framesPerRow: 1,
159 animationSpeed: 0,
160 craftingStationId: "furnace"
161 }),
162 [BlockType.anvil]: Object.freeze({
163 id: BlockType.anvil,
164 key: "anvil",
165 displayName: "Anvil",
166 passable: false,
167 lightRangeTiles: 0,
168 frames: 1,
169 framesPerRow: 1,
170 animationSpeed: 0,
171 craftingStationId: "anvil"
172 }),
173 [BlockType.woodworkingBench]: Object.freeze({
174 id: BlockType.woodworkingBench,
175 key: "woodworking-bench",
176 displayName: "Woodworking bench",
177 passable: false,
178 lightRangeTiles: 0,
179 frames: 1,
180 framesPerRow: 1,
181 animationSpeed: 0,
182 craftingStationId: "woodworking-bench"
183 })
184});
185
186/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
187export function listPlaceableGrounds ()
188{
189 return Object.values(GROUND_DEFINITIONS);
190}
191
192export function listPlaceableBlocks ()
193{
194 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
195}
196
197/**
198 * Editor palette cards: one entry per placeable, with boulder expanded into
199 * plain rock + each ore/coal variant (shared BlockType, distinct state).
200 */
201export function listPlaceableBlockPaletteEntries (catalog = null)
202{
203 const entries = [];
204 for (const def of listPlaceableBlocks())
205 {
206 if (def.id === BlockType.boulder)
207 {
208 entries.push({
209 typeId: def.id,
210 state: "boulder",
211 key: def.key,
212 displayName: "Boulder",
213 spriteName: spriteImageName("block", def.key, "boulder")
214 });
215 for (const material of ORE_BEARING_MATERIALS)
216 {
217 const state = oreBoulderState(material);
218 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
219 const isCoal = material === "coal";
220 entries.push({
221 typeId: def.id,
222 state,
223 key: def.key,
224 displayName: isCoal ? "Coal boulder" : `${label} ore boulder`,
225 spriteName: spriteImageName("block", def.key, state)
226 });
227 }
228 continue;
229 }
230 const states = catalog?.blocks?.[def.key]?.states ?? [];
231 const state = resolveState(states, null);
232 entries.push({
233 typeId: def.id,
234 state,
235 key: def.key,
236 displayName: def.displayName,
237 spriteName: state
238 ? spriteImageName("block", def.key, state)
239 : spriteImageName("block", def.key, def.key)
240 });
241 }
242 return entries;
243}
244
245export function groundDefinition (typeId)
246{
247 return GROUND_DEFINITIONS[typeId] ?? null;
248}
249
250export function blockDefinition (typeId)
251{
252 return BLOCK_DEFINITIONS[typeId] ?? null;
253}
254
255export function isGroundPassable (typeId)
256{
257 const def = groundDefinition(typeId);
258 if (!def)
259 { return true; }
260 return def.passable !== false;
261}
262
263export function isWaterGround (typeId)
264{
265 return !!groundDefinition(typeId)?.water;
266}
267
268export function isWadeGround (typeId)
269{
270 return !!groundDefinition(typeId)?.wades;
271}
272
273export function isBlockPassable (typeId)
274{
275 if (typeId == null)
276 { return true; }
277
278 const def = blockDefinition(typeId);
279 return def ? def.passable : false;
280}
281
282export function blockLightRange (typeId)
283{
284 const def = blockDefinition(typeId);
285 return def ? def.lightRangeTiles : 0;
286}
287
288export function normalizeGround (value, fallbackTypeId = GroundType.grass)
289{
290 if (typeof value === "number")
291 { return { typeId: value, state: null }; }
292 if (value && Number.isInteger(value.typeId))
293 { return { typeId: value.typeId, state: value.state ?? null }; }
294 return { typeId: fallbackTypeId, state: null };
295}
296
297export function normalizeBlock (value)
298{
299 if (!value || !Number.isInteger(value.typeId))
300 { return null; }
301 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
302}
303
304export function setBlockVisualState (block, state)
305{
306 if (!block)
307 { return null; }
308 block.state = state ?? null;
309 return block;
310}