BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 2.0 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});
10
11export const GROUND_DEFINITIONS = Object.freeze({
12 [GroundType.grass]: Object.freeze({
13 id: GroundType.grass,
14 key: "grass",
15 displayName: "Grass",
16 frames: 1,
17 framesPerRow: 1,
18 animationSpeed: 0
19 }),
20 [GroundType.wood]: Object.freeze({
21 id: GroundType.wood,
22 key: "wood",
23 displayName: "Wood",
24 frames: 1,
25 framesPerRow: 1,
26 animationSpeed: 0
27 })
28});
29
30export const BLOCK_DEFINITIONS = Object.freeze({
31 [BlockType.stoneWall]: Object.freeze({
32 id: BlockType.stoneWall,
33 key: "stone-wall",
34 displayName: "Stone wall",
35 passable: false,
36 lightRangeTiles: 0,
37 frames: 1,
38 framesPerRow: 1,
39 animationSpeed: 0
40 }),
41 [BlockType.torch]: Object.freeze({
42 id: BlockType.torch,
43 key: "torch",
44 displayName: "Torch",
45 passable: true,
46 lightRangeTiles: 8,
47 frames: 1,
48 framesPerRow: 1,
49 animationSpeed: 0
50 })
51});
52
53export function groundDefinition (typeId)
54{
55 return GROUND_DEFINITIONS[typeId] ?? null;
56}
57
58export function blockDefinition (typeId)
59{
60 return BLOCK_DEFINITIONS[typeId] ?? null;
61}
62
63export function isBlockPassable (typeId)
64{
65 if (typeId == null)
66 { return true; }
67
68 const def = blockDefinition(typeId);
69 return def ? def.passable : false;
70}
71
72export function blockLightRange (typeId)
73{
74 const def = blockDefinition(typeId);
75 return def ? def.lightRangeTiles : 0;
76}
77
78export function normalizeGround (value, fallbackTypeId = GroundType.grass)
79{
80 if (typeof value === "number")
81 { return { typeId: value, state: null }; }
82 if (value && Number.isInteger(value.typeId))
83 { return { typeId: value.typeId, state: value.state ?? null }; }
84 return { typeId: fallbackTypeId, state: null };
85}
86
87export function normalizeBlock (value)
88{
89 if (!value || !Number.isInteger(value.typeId))
90 { return null; }
91 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
92}
93
94export function setBlockVisualState (block, state)
95{
96 if (!block)
97 { return null; }
98 block.state = state ?? null;
99 return block;
100}