game-village-politics-frontend/app/diorama/life.ts

76 lines
3.5 KiB
TypeScript

// Timing for the people and animals in the dioramas, as SMIL value lists. Everything a
// figure does runs on one clock of `dur` seconds, so its walk, steps, tool, load and the
// way it faces stay in step without any script.
export type Tool = 'axe' | 'pick' | 'shovel' | 'sickle' | 'hammer' | 'spear'
export interface Cycle {
dur: number
/** animateMotion: out along the path, work, back, a short stop at home. */
motion: { keyPoints: string; keyTimes: string }
/** Legs: a translate on one leg (the other mirrors it), only while walking. */
step: { values: string; mirrored: string; keyTimes: string }
/** The tool: rests on the shoulder, swings only while working. */
swing: { values: string; keyTimes: string }
/** The load shows on the way home. */
load: { values: string; keyTimes: string }
/** Turns round to walk home. */
face: { values: string; keyTimes: string }
}
const STEP = 0.32
const SWING = 0.55
/** Alternating values every `period` seconds across [from, to] of a cycle of `dur` seconds. */
function alternate(from: number, to: number, dur: number, period: number, a: string, b: string): [number, string][] {
const out: [number, string][] = []
const n = Math.max(1, Math.floor((to - from) / period))
for (let i = 0; i < n; i++) out.push([(from + i * period) / dur, i % 2 ? b : a])
return out
}
/** Builds key lists, making sure they start at 0, end at 1 and only grow. */
function keys(points: [number, string][]): { values: string; keyTimes: string } {
const list = [...points].sort((p, q) => p[0] - q[0])
if (list[0]![0] > 0) list.unshift([0, list[0]![1]])
if (list[list.length - 1]![0] < 1) list.push([1, list[list.length - 1]![1]])
const clean: [number, string][] = []
for (const [t, v] of list) {
const time = Math.min(1, Math.max(0, t))
if (clean.length && time <= clean[clean.length - 1]![0]) clean[clean.length - 1] = [clean[clean.length - 1]![0], v]
else clean.push([time, v])
}
clean[clean.length - 1]![0] = 1
return { values: clean.map((p) => p[1]).join(';'), keyTimes: clean.map((p) => p[0].toFixed(4)).join(';') }
}
/**
* A work cycle: walk out for `walk` seconds, work for `work`, walk home for `walk`, stand
* for `rest`. `facing` is 1 when walking out goes to the right on screen, else -1.
*/
export function workCycle(walk: number, work: number, rest = 1.2, facing: 1 | -1 = 1): Cycle {
const dur = walk * 2 + work + rest
const a = walk / dur
const b = (walk + work) / dur
const c = (walk * 2 + work) / dur
const stepping = [...alternate(0, walk, dur, STEP, '0.7 0', '-0.7 0'), [a, '0 0'] as [number, string], ...alternate(walk + work, walk * 2 + work, dur, STEP, '0.7 0', '-0.7 0'), [c, '0 0'] as [number, string]]
const step = keys(stepping)
const mirrored = step.values
.split(';')
.map((v) => (v.startsWith('-') ? v.slice(1) : v === '0 0' ? v : `-${v}`))
.join(';')
return {
dur,
motion: { keyPoints: '0;1;1;0;0', keyTimes: `0;${a.toFixed(4)};${b.toFixed(4)};${c.toFixed(4)};1` },
step: { ...step, mirrored },
swing: keys([[0, '0'], [a, '0'], ...alternate(walk, walk + work, dur, SWING, '-70', '35'), [b, '0'], [1, '0']]),
load: { values: 'hidden;visible;hidden', keyTimes: `0;${b.toFixed(4)};${c.toFixed(4)}` },
face: { values: `${facing} 1;${-facing} 1;${facing} 1`, keyTimes: `0;${b.toFixed(4)};${c.toFixed(4)}` },
}
}
/** Working on the spot for good: a builder hammering, a farmer cutting. */
export function workInPlace(): { dur: number; values: string; keyTimes: string } {
return { dur: SWING * 2, values: '-70;35;-70', keyTimes: '0;0.5;1' }
}