| 1 | import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "/shared/definitions/tiles.js"; |
| 2 | import { bloomImageName, resolveState, spriteImageName } from "/shared/definitions/tileSprites.js"; |
| 3 | |
| 4 | /** Resolve ground/block/bloom image names for one tile. */ |
| 5 | export function resolveTileSprites ({ catalog, ground, block }) |
| 6 | { |
| 7 | return { |
| 8 | ground: groundSpriteName(catalog, ground), |
| 9 | groundBloom: groundBloomName(catalog, ground), |
| 10 | block: blockSpriteName(catalog, block), |
| 11 | blockBloom: blockBloomName(catalog, block) |
| 12 | }; |
| 13 | } |
| 14 | |
| 15 | function groundSpriteName (catalog, ground) |
| 16 | { |
| 17 | const entry = normalizeGround(ground); |
| 18 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 19 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 20 | const state = resolveState(states, entry.state); |
| 21 | return spriteImageName("ground", def.key, state); |
| 22 | } |
| 23 | |
| 24 | function blockSpriteName (catalog, block) |
| 25 | { |
| 26 | const entry = normalizeBlock(block); |
| 27 | if (!entry) |
| 28 | { return null; } |
| 29 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 30 | if (!def) |
| 31 | { return null; } |
| 32 | const states = catalog.blocks?.[def.key]?.states ?? []; |
| 33 | const state = resolveState(states, entry.state); |
| 34 | return spriteImageName("block", def.key, state); |
| 35 | } |
| 36 | |
| 37 | function groundBloomName (catalog, ground) |
| 38 | { |
| 39 | const entry = normalizeGround(ground); |
| 40 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 41 | const type = catalog.grounds?.[def.key]; |
| 42 | const state = resolveState(type?.states ?? [], entry.state); |
| 43 | if (!state || !type?.blooms?.[state]) |
| 44 | { return null; } |
| 45 | return bloomImageName("ground", def.key, state); |
| 46 | } |
| 47 | |
| 48 | function blockBloomName (catalog, block) |
| 49 | { |
| 50 | const entry = normalizeBlock(block); |
| 51 | if (!entry) |
| 52 | { return null; } |
| 53 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 54 | if (!def) |
| 55 | { return null; } |
| 56 | const type = catalog.blocks?.[def.key]; |
| 57 | const state = resolveState(type?.states ?? [], entry.state); |
| 58 | if (!state || !type?.blooms?.[state]) |
| 59 | { return null; } |
| 60 | return bloomImageName("block", def.key, state); |
| 61 | } |