| 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 | /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */ |
| 10 | spawner: 3, |
| 11 | bank: 4, |
| 12 | churchAltar: 5 |
| 13 | }); |
| 14 | |
| 15 | export const GROUND_DEFINITIONS = Object.freeze({ |
| 16 | [GroundType.grass]: Object.freeze({ |
| 17 | id: GroundType.grass, |
| 18 | key: "grass", |
| 19 | displayName: "Grass", |
| 20 | frames: 1, |
| 21 | framesPerRow: 1, |
| 22 | animationSpeed: 0 |
| 23 | }), |
| 24 | [GroundType.wood]: Object.freeze({ |
| 25 | id: GroundType.wood, |
| 26 | key: "wood", |
| 27 | displayName: "Wood", |
| 28 | frames: 1, |
| 29 | framesPerRow: 1, |
| 30 | animationSpeed: 0 |
| 31 | }) |
| 32 | }); |
| 33 | |
| 34 | export const BLOCK_DEFINITIONS = Object.freeze({ |
| 35 | [BlockType.stoneWall]: Object.freeze({ |
| 36 | id: BlockType.stoneWall, |
| 37 | key: "stone-wall", |
| 38 | displayName: "Stone wall", |
| 39 | passable: false, |
| 40 | lightRangeTiles: 0, |
| 41 | frames: 1, |
| 42 | framesPerRow: 1, |
| 43 | animationSpeed: 0 |
| 44 | }), |
| 45 | [BlockType.torch]: Object.freeze({ |
| 46 | id: BlockType.torch, |
| 47 | key: "torch", |
| 48 | displayName: "Torch", |
| 49 | passable: true, |
| 50 | lightRangeTiles: 8, |
| 51 | frames: 1, |
| 52 | framesPerRow: 1, |
| 53 | animationSpeed: 0 |
| 54 | }), |
| 55 | [BlockType.spawner]: Object.freeze({ |
| 56 | id: BlockType.spawner, |
| 57 | key: "spawner", |
| 58 | displayName: "Spawner", |
| 59 | passable: true, |
| 60 | lightRangeTiles: 0, |
| 61 | frames: 1, |
| 62 | framesPerRow: 1, |
| 63 | animationSpeed: 0, |
| 64 | /** Not shown in the placeable block palette; use the Spawners tab. */ |
| 65 | editorHidden: true |
| 66 | }), |
| 67 | [BlockType.bank]: Object.freeze({ |
| 68 | id: BlockType.bank, |
| 69 | key: "bank", |
| 70 | displayName: "Bank", |
| 71 | passable: false, |
| 72 | lightRangeTiles: 0, |
| 73 | frames: 1, |
| 74 | framesPerRow: 1, |
| 75 | animationSpeed: 0 |
| 76 | }), |
| 77 | [BlockType.churchAltar]: Object.freeze({ |
| 78 | id: BlockType.churchAltar, |
| 79 | key: "church-altar", |
| 80 | displayName: "Church altar", |
| 81 | passable: false, |
| 82 | lightRangeTiles: 0, |
| 83 | frames: 1, |
| 84 | framesPerRow: 1, |
| 85 | animationSpeed: 0 |
| 86 | }) |
| 87 | }); |
| 88 | |
| 89 | /** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */ |
| 90 | export function listPlaceableGrounds () |
| 91 | { |
| 92 | return Object.values(GROUND_DEFINITIONS); |
| 93 | } |
| 94 | |
| 95 | export function listPlaceableBlocks () |
| 96 | { |
| 97 | return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden); |
| 98 | } |
| 99 | |
| 100 | export function groundDefinition (typeId) |
| 101 | { |
| 102 | return GROUND_DEFINITIONS[typeId] ?? null; |
| 103 | } |
| 104 | |
| 105 | export function blockDefinition (typeId) |
| 106 | { |
| 107 | return BLOCK_DEFINITIONS[typeId] ?? null; |
| 108 | } |
| 109 | |
| 110 | export function isBlockPassable (typeId) |
| 111 | { |
| 112 | if (typeId == null) |
| 113 | { return true; } |
| 114 | |
| 115 | const def = blockDefinition(typeId); |
| 116 | return def ? def.passable : false; |
| 117 | } |
| 118 | |
| 119 | export function blockLightRange (typeId) |
| 120 | { |
| 121 | const def = blockDefinition(typeId); |
| 122 | return def ? def.lightRangeTiles : 0; |
| 123 | } |
| 124 | |
| 125 | export function normalizeGround (value, fallbackTypeId = GroundType.grass) |
| 126 | { |
| 127 | if (typeof value === "number") |
| 128 | { return { typeId: value, state: null }; } |
| 129 | if (value && Number.isInteger(value.typeId)) |
| 130 | { return { typeId: value.typeId, state: value.state ?? null }; } |
| 131 | return { typeId: fallbackTypeId, state: null }; |
| 132 | } |
| 133 | |
| 134 | export function normalizeBlock (value) |
| 135 | { |
| 136 | if (!value || !Number.isInteger(value.typeId)) |
| 137 | { return null; } |
| 138 | return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null }; |
| 139 | } |
| 140 | |
| 141 | export function setBlockVisualState (block, state) |
| 142 | { |
| 143 | if (!block) |
| 144 | { return null; } |
| 145 | block.state = state ?? null; |
| 146 | return block; |
| 147 | } |