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