| 1 | export function resolveState (states, requested) |
| 2 | { |
| 3 | if (!states?.length) |
| 4 | { return null; } |
| 5 | if (requested && states.includes(requested)) |
| 6 | { return requested; } |
| 7 | return states[0]; |
| 8 | } |
| 9 | |
| 10 | export function cycleState (states, current, direction) |
| 11 | { |
| 12 | if (!states?.length) |
| 13 | { return null; } |
| 14 | const resolved = resolveState(states, current); |
| 15 | const index = states.indexOf(resolved); |
| 16 | const step = direction < 0 ? -1 : 1; |
| 17 | return states[(index + step + states.length) % states.length]; |
| 18 | } |
| 19 | |
| 20 | export function spriteImageName (layer, key, state) |
| 21 | { |
| 22 | return `${layer}:${key}:${state}`; |
| 23 | } |
| 24 | |
| 25 | export function statesForType (catalog, layer, key) |
| 26 | { |
| 27 | const group = layer === "ground" ? catalog?.grounds : catalog?.blocks; |
| 28 | return group?.[key]?.states ?? []; |
| 29 | } |
| 30 | |
| 31 | export function defaultStateForType (catalog, layer, key) |
| 32 | { |
| 33 | const group = layer === "ground" ? catalog?.grounds : catalog?.blocks; |
| 34 | return group?.[key]?.defaultState ?? resolveState(statesForType(catalog, layer, key), null); |
| 35 | } |