| 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 | /** Editor palette thumb for procedural grounds (`water` → `ground:water:water-preview`). */ |
| 28 | export function palettePreviewImageName (layer, key) |
| 29 | { |
| 30 | return spriteImageName(layer, key, `${key}-preview`); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Companion suffixes peel from the right in fixed order. |
| 35 | * `torch-equip-bloom` → base torch, equip + bloom. |
| 36 | * `torch-bloom` → base torch, bloom. |
| 37 | * `stick-equip` → base stick, equip. |
| 38 | */ |
| 39 | export function parseSpriteStem (stem) |
| 40 | { |
| 41 | let rest = String(stem ?? ""); |
| 42 | let bloom = false; |
| 43 | let equip = false; |
| 44 | let preview = false; |
| 45 | if (rest.endsWith("-preview") && rest.length > "-preview".length) |
| 46 | { |
| 47 | preview = true; |
| 48 | rest = rest.slice(0, -"-preview".length); |
| 49 | } |
| 50 | if (rest.endsWith("-bloom") && rest.length > "-bloom".length) |
| 51 | { |
| 52 | bloom = true; |
| 53 | rest = rest.slice(0, -"-bloom".length); |
| 54 | } |
| 55 | if (rest.endsWith("-equip") && rest.length > "-equip".length) |
| 56 | { |
| 57 | equip = true; |
| 58 | rest = rest.slice(0, -"-equip".length); |
| 59 | } |
| 60 | const companion = bloom || equip || preview; |
| 61 | return { |
| 62 | base: companion ? rest : String(stem ?? ""), |
| 63 | companion, |
| 64 | bloom, |
| 65 | equip, |
| 66 | preview |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | export function isCompanionSpriteStem (stem) |
| 71 | { |
| 72 | return parseSpriteStem(stem).companion; |
| 73 | } |
| 74 | |
| 75 | export function resolveState (states, requested) |
| 76 | { |
| 77 | if (!states?.length) |
| 78 | { return null; } |
| 79 | if (requested && !isCompanionSpriteStem(requested) && states.includes(requested)) |
| 80 | { return requested; } |
| 81 | return states[0]; |
| 82 | } |
| 83 | |
| 84 | export function cycleState (states, current, direction) |
| 85 | { |
| 86 | if (!states?.length) |
| 87 | { return null; } |
| 88 | const resolved = resolveState(states, current); |
| 89 | const index = states.indexOf(resolved); |
| 90 | const step = direction < 0 ? -1 : 1; |
| 91 | return states[(index + step + states.length) % states.length]; |
| 92 | } |
| 93 | |
| 94 | export function catalogGroup (catalog, layer) |
| 95 | { |
| 96 | if (layer === "ground") |
| 97 | { return catalog?.grounds; } |
| 98 | if (layer === "block") |
| 99 | { return catalog?.blocks; } |
| 100 | if (layer === "item") |
| 101 | { return catalog?.items; } |
| 102 | if (layer === "mob") |
| 103 | { return catalog?.mobs; } |
| 104 | if (layer === "character") |
| 105 | { return catalog?.characters; } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | export function statesForType (catalog, layer, key) |
| 110 | { |
| 111 | return (catalogGroup(catalog, layer)?.[key]?.states ?? []) |
| 112 | .filter((state) => !isCompanionSpriteStem(state)); |
| 113 | } |
| 114 | |
| 115 | export function defaultStateForType (catalog, layer, key) |
| 116 | { |
| 117 | const group = catalogGroup(catalog, layer)?.[key]; |
| 118 | return group?.defaultState ?? resolveState(statesForType(catalog, layer, key), null); |
| 119 | } |