| 1 | export function spriteImageName (layer, key, state) |
| 2 | { |
| 3 | return `${layer}:${key}:${state}`; |
| 4 | } |
| 5 | |
| 6 | export function bloomImageName (layer, key, state) |
| 7 | { |
| 8 | return `${spriteImageName(layer, key, state)}:bloom`; |
| 9 | } |
| 10 | |
| 11 | export function equipImageName (layer, key, state) |
| 12 | { |
| 13 | return `${spriteImageName(layer, key, state)}:equip`; |
| 14 | } |
| 15 | |
| 16 | export function equipBloomImageName (layer, key, state) |
| 17 | { |
| 18 | return `${equipImageName(layer, key, state)}:bloom`; |
| 19 | } |
| 20 | |
| 21 | /** Bloom companion for any registered image name (`item:stick:stick:equip` → `…:equip:bloom`). */ |
| 22 | export function bloomCompanionName (imageName) |
| 23 | { |
| 24 | return `${imageName}:bloom`; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Companion suffixes peel from the right in fixed order. |
| 29 | * `torch-equip-bloom` → base torch, equip + bloom. |
| 30 | * `torch-bloom` → base torch, bloom. |
| 31 | * `stick-equip` → base stick, equip. |
| 32 | */ |
| 33 | export function parseSpriteStem (stem) |
| 34 | { |
| 35 | let rest = String(stem ?? ""); |
| 36 | let bloom = false; |
| 37 | let equip = false; |
| 38 | if (rest.endsWith("-bloom") && rest.length > "-bloom".length) |
| 39 | { |
| 40 | bloom = true; |
| 41 | rest = rest.slice(0, -"-bloom".length); |
| 42 | } |
| 43 | if (rest.endsWith("-equip") && rest.length > "-equip".length) |
| 44 | { |
| 45 | equip = true; |
| 46 | rest = rest.slice(0, -"-equip".length); |
| 47 | } |
| 48 | const companion = bloom || equip; |
| 49 | return { |
| 50 | base: companion ? rest : String(stem ?? ""), |
| 51 | companion, |
| 52 | bloom, |
| 53 | equip |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | export function isCompanionSpriteStem (stem) |
| 58 | { |
| 59 | return parseSpriteStem(stem).companion; |
| 60 | } |
| 61 | |
| 62 | export function resolveState (states, requested) |
| 63 | { |
| 64 | if (!states?.length) |
| 65 | { return null; } |
| 66 | if (requested && !isCompanionSpriteStem(requested) && states.includes(requested)) |
| 67 | { return requested; } |
| 68 | return states[0]; |
| 69 | } |
| 70 | |
| 71 | export function cycleState (states, current, direction) |
| 72 | { |
| 73 | if (!states?.length) |
| 74 | { return null; } |
| 75 | const resolved = resolveState(states, current); |
| 76 | const index = states.indexOf(resolved); |
| 77 | const step = direction < 0 ? -1 : 1; |
| 78 | return states[(index + step + states.length) % states.length]; |
| 79 | } |
| 80 | |
| 81 | export function catalogGroup (catalog, layer) |
| 82 | { |
| 83 | if (layer === "ground") |
| 84 | { return catalog?.grounds; } |
| 85 | if (layer === "block") |
| 86 | { return catalog?.blocks; } |
| 87 | if (layer === "item") |
| 88 | { return catalog?.items; } |
| 89 | if (layer === "mob") |
| 90 | { return catalog?.mobs; } |
| 91 | if (layer === "character") |
| 92 | { return catalog?.characters; } |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | export function statesForType (catalog, layer, key) |
| 97 | { |
| 98 | return (catalogGroup(catalog, layer)?.[key]?.states ?? []) |
| 99 | .filter((state) => !isCompanionSpriteStem(state)); |
| 100 | } |
| 101 | |
| 102 | export function defaultStateForType (catalog, layer, key) |
| 103 | { |
| 104 | const group = catalogGroup(catalog, layer)?.[key]; |
| 105 | return group?.defaultState ?? resolveState(statesForType(catalog, layer, key), null); |
| 106 | } |