BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 4.2 KiB Download Raw
1export const GroundType = Object.freeze({
2 grass: 1,
3 wood: 2,
4 water: 3,
5 deepWater: 4
6});
7
8export const BlockType = Object.freeze({
9 stoneWall: 1,
10 torch: 2,
11 /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */
12 spawner: 3,
13 bank: 4,
14 churchAltar: 5,
15 cauldron: 6
16});
17
18export const GROUND_DEFINITIONS = Object.freeze({
19 [GroundType.grass]: Object.freeze({
20 id: GroundType.grass,
21 key: "grass",
22 displayName: "Grass",
23 passable: true,
24 wades: false,
25 water: false,
26 frames: 1,
27 framesPerRow: 1,
28 animationSpeed: 0
29 }),
30 [GroundType.wood]: Object.freeze({
31 id: GroundType.wood,
32 key: "wood",
33 displayName: "Wood",
34 passable: true,
35 wades: false,
36 water: false,
37 frames: 1,
38 framesPerRow: 1,
39 animationSpeed: 0
40 }),
41 [GroundType.water]: Object.freeze({
42 id: GroundType.water,
43 key: "water",
44 displayName: "Water",
45 passable: true,
46 wades: true,
47 water: true,
48 frames: 4,
49 framesPerRow: 4,
50 animationSpeed: 0.12
51 }),
52 [GroundType.deepWater]: Object.freeze({
53 id: GroundType.deepWater,
54 key: "deep-water",
55 displayName: "Deep water",
56 passable: false,
57 wades: false,
58 water: true,
59 frames: 4,
60 framesPerRow: 4,
61 animationSpeed: 0.12
62 })
63});
64
65export const BLOCK_DEFINITIONS = Object.freeze({
66 [BlockType.stoneWall]: Object.freeze({
67 id: BlockType.stoneWall,
68 key: "stone-wall",
69 displayName: "Stone wall",
70 passable: false,
71 lightRangeTiles: 0,
72 frames: 1,
73 framesPerRow: 1,
74 animationSpeed: 0
75 }),
76 [BlockType.torch]: Object.freeze({
77 id: BlockType.torch,
78 key: "torch",
79 displayName: "Torch",
80 passable: true,
81 lightRangeTiles: 8,
82 frames: 1,
83 framesPerRow: 1,
84 animationSpeed: 0
85 }),
86 [BlockType.spawner]: Object.freeze({
87 id: BlockType.spawner,
88 key: "spawner",
89 displayName: "Spawner",
90 passable: true,
91 lightRangeTiles: 0,
92 frames: 1,
93 framesPerRow: 1,
94 animationSpeed: 0,
95 /** Not shown in the placeable block palette; use the Spawners tab. */
96 editorHidden: true
97 }),
98 [BlockType.bank]: Object.freeze({
99 id: BlockType.bank,
100 key: "bank",
101 displayName: "Bank",
102 passable: false,
103 lightRangeTiles: 0,
104 frames: 1,
105 framesPerRow: 1,
106 animationSpeed: 0
107 }),
108 [BlockType.churchAltar]: Object.freeze({
109 id: BlockType.churchAltar,
110 key: "church-altar",
111 displayName: "Church altar",
112 passable: false,
113 lightRangeTiles: 0,
114 frames: 1,
115 framesPerRow: 1,
116 animationSpeed: 0
117 }),
118 [BlockType.cauldron]: Object.freeze({
119 id: BlockType.cauldron,
120 key: "cauldron",
121 displayName: "Cauldron",
122 passable: false,
123 lightRangeTiles: 0,
124 frames: 1,
125 framesPerRow: 1,
126 animationSpeed: 0
127 })
128});
129
130/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
131export function listPlaceableGrounds ()
132{
133 return Object.values(GROUND_DEFINITIONS);
134}
135
136export function listPlaceableBlocks ()
137{
138 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
139}
140
141export function groundDefinition (typeId)
142{
143 return GROUND_DEFINITIONS[typeId] ?? null;
144}
145
146export function blockDefinition (typeId)
147{
148 return BLOCK_DEFINITIONS[typeId] ?? null;
149}
150
151export function isGroundPassable (typeId)
152{
153 const def = groundDefinition(typeId);
154 if (!def)
155 { return true; }
156 return def.passable !== false;
157}
158
159export function isWaterGround (typeId)
160{
161 return !!groundDefinition(typeId)?.water;
162}
163
164export function isWadeGround (typeId)
165{
166 return !!groundDefinition(typeId)?.wades;
167}
168
169export function isBlockPassable (typeId)
170{
171 if (typeId == null)
172 { return true; }
173
174 const def = blockDefinition(typeId);
175 return def ? def.passable : false;
176}
177
178export function blockLightRange (typeId)
179{
180 const def = blockDefinition(typeId);
181 return def ? def.lightRangeTiles : 0;
182}
183
184export function normalizeGround (value, fallbackTypeId = GroundType.grass)
185{
186 if (typeof value === "number")
187 { return { typeId: value, state: null }; }
188 if (value && Number.isInteger(value.typeId))
189 { return { typeId: value.typeId, state: value.state ?? null }; }
190 return { typeId: fallbackTypeId, state: null };
191}
192
193export function normalizeBlock (value)
194{
195 if (!value || !Number.isInteger(value.typeId))
196 { return null; }
197 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
198}
199
200export function setBlockVisualState (block, state)
201{
202 if (!block)
203 { return null; }
204 block.state = state ?? null;
205 return block;
206}