BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 2.4 KiB Download Raw
1export const GroundType = Object.freeze({
2 grass: 1,
3 wood: 2
4});
5
6export const BlockType = Object.freeze({
7 stoneWall: 1,
8 torch: 2,
9 spawner: 3,
10 bank: 4
11});
12
13export const GROUND_DEFINITIONS = Object.freeze({
14 [GroundType.grass]: Object.freeze({
15 id: GroundType.grass,
16 key: "grass",
17 displayName: "Grass",
18 frames: 1,
19 framesPerRow: 1,
20 animationSpeed: 0
21 }),
22 [GroundType.wood]: Object.freeze({
23 id: GroundType.wood,
24 key: "wood",
25 displayName: "Wood",
26 frames: 1,
27 framesPerRow: 1,
28 animationSpeed: 0
29 })
30});
31
32export const BLOCK_DEFINITIONS = Object.freeze({
33 [BlockType.stoneWall]: Object.freeze({
34 id: BlockType.stoneWall,
35 key: "stone-wall",
36 displayName: "Stone wall",
37 passable: false,
38 lightRangeTiles: 0,
39 frames: 1,
40 framesPerRow: 1,
41 animationSpeed: 0
42 }),
43 [BlockType.torch]: Object.freeze({
44 id: BlockType.torch,
45 key: "torch",
46 displayName: "Torch",
47 passable: true,
48 lightRangeTiles: 8,
49 frames: 1,
50 framesPerRow: 1,
51 animationSpeed: 0
52 }),
53 [BlockType.spawner]: Object.freeze({
54 id: BlockType.spawner,
55 key: "spawner",
56 displayName: "Spawner",
57 passable: false,
58 lightRangeTiles: 0,
59 frames: 1,
60 framesPerRow: 1,
61 animationSpeed: 0
62 }),
63 [BlockType.bank]: Object.freeze({
64 id: BlockType.bank,
65 key: "bank",
66 displayName: "Bank",
67 passable: false,
68 lightRangeTiles: 0,
69 frames: 1,
70 framesPerRow: 1,
71 animationSpeed: 0
72 })
73});
74
75export function groundDefinition (typeId)
76{
77 return GROUND_DEFINITIONS[typeId] ?? null;
78}
79
80export function blockDefinition (typeId)
81{
82 return BLOCK_DEFINITIONS[typeId] ?? null;
83}
84
85export function isBlockPassable (typeId)
86{
87 if (typeId == null)
88 { return true; }
89
90 const def = blockDefinition(typeId);
91 return def ? def.passable : false;
92}
93
94export function blockLightRange (typeId)
95{
96 const def = blockDefinition(typeId);
97 return def ? def.lightRangeTiles : 0;
98}
99
100export function normalizeGround (value, fallbackTypeId = GroundType.grass)
101{
102 if (typeof value === "number")
103 { return { typeId: value, state: null }; }
104 if (value && Number.isInteger(value.typeId))
105 { return { typeId: value.typeId, state: value.state ?? null }; }
106 return { typeId: fallbackTypeId, state: null };
107}
108
109export function normalizeBlock (value)
110{
111 if (!value || !Number.isInteger(value.typeId))
112 { return null; }
113 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
114}
115
116export function setBlockVisualState (block, state)
117{
118 if (!block)
119 { return null; }
120 block.state = state ?? null;
121 return block;
122}