[engine] PlanetGarden: How many weeds and petals a day shows; the gesture recognisers
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* The planet garden's gestures (.agents/features/planet-garden.md G2): pure functions of
|
||||
* pointer samples, in the scene's own units. They read the player's hand; every rule about
|
||||
* what care does stays in the engine.
|
||||
*/
|
||||
|
||||
export interface Sample {
|
||||
readonly x: number;
|
||||
readonly y: number;
|
||||
/** Milliseconds. */
|
||||
readonly t: number;
|
||||
}
|
||||
|
||||
export interface Box {
|
||||
readonly x0: number;
|
||||
readonly y0: number;
|
||||
readonly x1: number;
|
||||
readonly y1: number;
|
||||
}
|
||||
|
||||
/** The thresholds, generous on purpose: a missed gesture must never feel like failing. */
|
||||
export const GESTURE = {
|
||||
/** Sideways travel before the can starts to tip, and at which it pours fastest. */
|
||||
tiltStart: 12,
|
||||
tiltFull: 140,
|
||||
/** A pull: this far up, mostly upwards, within this long. */
|
||||
pullUp: 48,
|
||||
pullMs: 2000,
|
||||
/** A sweep: this far sideways, no more than this far up or down. */
|
||||
sweepAcross: 110,
|
||||
sweepDrift: 70,
|
||||
/** A stroke: this much travel, taking at least this long (a tap is quicker and shorter). */
|
||||
strokeTravel: 30,
|
||||
strokeMs: 220,
|
||||
tapTravel: 10
|
||||
} as const;
|
||||
|
||||
const first = (s: readonly Sample[]): Sample => s[0] ?? { x: 0, y: 0, t: 0 };
|
||||
const last = (s: readonly Sample[]): Sample => s[s.length - 1] ?? first(s);
|
||||
const travel = (s: readonly Sample[]): number =>
|
||||
s.reduce((sum, p, i) => (i === 0 ? 0 : sum + Math.hypot(p.x - s[i - 1]!.x, p.y - s[i - 1]!.y)), 0);
|
||||
|
||||
/** How far the can is tipped, 0 to 1, from how far the hand has moved sideways since pressing. */
|
||||
export function tiltOf(start: Sample, now: Sample): number {
|
||||
const dx = Math.abs(now.x - start.x);
|
||||
if (dx <= GESTURE.tiltStart) return 0;
|
||||
return Math.min(1, (dx - GESTURE.tiltStart) / (GESTURE.tiltFull - GESTURE.tiltStart));
|
||||
}
|
||||
|
||||
/** How fast a can tipped by `tilt` pours, as a multiple of a steady hold (the button's pour). */
|
||||
export const pourRate = (tilt: number): number => 0.4 + 1.1 * Math.min(1, Math.max(0, tilt));
|
||||
|
||||
/** Whether the samples pull a weed: up far enough, mostly upwards, quickly enough. */
|
||||
export function isPull(samples: readonly Sample[]): boolean {
|
||||
const a = first(samples), b = last(samples);
|
||||
const up = a.y - b.y;
|
||||
return up >= GESTURE.pullUp && Math.abs(b.x - a.x) <= up && b.t - a.t <= GESTURE.pullMs;
|
||||
}
|
||||
|
||||
/** Whether the samples sweep across everything between `from` and `to` on the x axis. */
|
||||
export function isSweep(samples: readonly Sample[], from: number, to: number): boolean {
|
||||
if (samples.length < 2) return false;
|
||||
const xs = samples.map((p) => p.x), ys = samples.map((p) => p.y);
|
||||
const across = Math.abs(last(samples).x - first(samples).x);
|
||||
const lo = Math.min(...xs), hi = Math.max(...xs);
|
||||
const covers = lo <= Math.min(from, to) && hi >= Math.max(from, to);
|
||||
return across >= GESTURE.sweepAcross && Math.max(...ys) - Math.min(...ys) <= GESTURE.sweepDrift && covers;
|
||||
}
|
||||
|
||||
export const inside = (p: Pick<Sample, 'x' | 'y'>, b: Box): boolean => p.x >= b.x0 && p.x <= b.x1 && p.y >= b.y0 && p.y <= b.y1;
|
||||
|
||||
/** A slow stroke across her petals, a quick tap, or neither. */
|
||||
export function strokeOf(samples: readonly Sample[]): 'stroke' | 'tap' | 'none' {
|
||||
const moved = travel(samples);
|
||||
const took = last(samples).t - first(samples).t;
|
||||
if (moved <= GESTURE.tapTravel) return 'tap';
|
||||
return moved >= GESTURE.strokeTravel && took >= GESTURE.strokeMs ? 'stroke' : 'none';
|
||||
}
|
||||
Reference in New Issue
Block a user