sandoniette/src/sim/food.ts
type-two 26921eb4ad Build-out 2: the playable loop — detection stacking, live serve, string picker, 8 days
- Sando is zone-DETECTION now: the stack = food bodies physically resting in
  the plate zone, bottom->top. Hand-stacked and harness-stacked sandos are
  judged identically (sando.kinds() is what the judge reads).
- The full live loop: Game.state + updateLive() — ticket clock with late-anger
  (1 bump/3s overdue), bow-dwell serve (hold still 0.5s over a matching
  standing sando -> scorecard), click -> next day; string cut -> ragdoll ->
  fail card -> click retries the same day with fresh strings. setupDay stages
  bread + fillings per order; sins fire live off cutter.onCut + offstage sweep.
- String unlockables: HEMP/RUBBER/CHAIN/SILK picker on the title (stiffness/
  damping multipliers, registry-persisted). Rubber visibly stretches.
- Days 6-8: Tamago (it rolls), Tofu (fragile), THE TOWER (5 layers) + tofu food.
- samurai_bow (same seed): an S serve swaps in the single approving nod.
- Battery: M1 87f/8.41u/10-10, M2 cv0 clean, M3 both, days SSSSSSSS, live
  serve commits at 31f (~0.5s), death->retry + serve->advance verified.
- Known: embedded preview pane never fires rAF (visibilityState hidden) — live
  loop freezes there by browser design; harness steps manually. Real tabs fine.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 17:06:22 +10:00

85 lines
3.8 KiB
TypeScript

import type { Stage } from '../scenes/stage';
import { centroid, type V } from './geom';
/** toastsim-style material params. resistance raises the cut threshold; moisture
* drives the splat; wobble is jelly-class jiggle (soft, deferred to when a
* jelly food actually ships). */
export interface Material {
resistance: number; // 0..1 skin toughness
moisture: number; // 0..1 wet food splats
wobble: number; // 0..1 jiggle
}
export interface FoodDef {
verts: V[]; // convex, centred on origin
material: Material;
color: number;
face: string; // generated face sprite id (rendered later; gameplay is the silhouette)
}
const circle = (r: number, n: number, squashY = 1): V[] =>
Array.from({ length: n }, (_, i) => {
const a = (i / n) * Math.PI * 2;
return { x: Math.cos(a) * r, y: Math.sin(a) * r * squashY };
});
export const box = (w: number, h: number): V[] => {
const x = w / 2, y = h / 2, c = Math.min(x, y) * 0.4; // chamfered corners → convex octagon
return [
{ x: -x + c, y: -y }, { x: x - c, y: -y }, { x, y: -y + c }, { x, y: y - c },
{ x: x - c, y }, { x: -x + c, y }, { x: -x, y: y - c }, { x: -x, y: -y + c },
];
};
export const FOODS: Record<string, FoodDef> = {
tomato: { verts: circle(34, 12), material: { resistance: 0.25, moisture: 0.85, wobble: 0.15 }, color: 0x151515, face: 'face_tomato' },
bread: { verts: box(78, 52), material: { resistance: 0.35, moisture: 0.05, wobble: 0.0 }, color: 0x1c150e, face: 'face_bread' },
cheese: { verts: box(60, 44), material: { resistance: 0.45, moisture: 0.15, wobble: 0.0 }, color: 0x191510, face: 'face_cheese' },
katsu: { verts: box(86, 46), material: { resistance: 0.55, moisture: 0.1, wobble: 0.0 }, color: 0x171008, face: 'face_katsu' },
egg: { verts: circle(28, 10, 1.15), material: { resistance: 0.3, moisture: 0.4, wobble: 0.1 }, color: 0x121212, face: 'face_egg' },
// ponytail: wobble is a material param only — real jelly-cluster jiggle waits
// until a day needs it to be funny. Tofu cuts easy but splats if pressed.
tofu: { verts: box(64, 46), material: { resistance: 0.12, moisture: 0.6, wobble: 0.4 }, color: 0x1a1a18, face: 'face_egg' },
};
let NEXT = 1;
/** A food body + its material and origin colour. Pieces are Foods too. */
export class Food {
readonly id: string;
constructor(
readonly body: MatterJS.BodyType,
readonly material: Material,
readonly color: number,
readonly face: string,
readonly kind: string,
) {
this.id = `${kind}${NEXT++}`;
}
}
/** Build a food body from centred local verts placed at world (x,y). Registers
* the silhouette on the stage. Shared by spawn and by the cutter's pieces. */
export function makeFood(stage: Stage, local: V[], x: number, y: number, mat: Material, color: number, face: string, kind: string): Food {
const body = stage.matter.add.fromVertices(x, y, local, {
friction: 0.6, frictionAir: 0.01, density: 0.0009, restitution: 0.05,
}) as MatterJS.BodyType;
// fromVertices recentres COM; render verts relative to that centre.
const c = centroid(local);
const rel = local.map((v) => ({ x: v.x - c.x, y: v.y - c.y }));
stage.addPoly(body, rel, color);
// a comical face — whole foods only (pieces are pieces). `p`-suffixed kinds
// are cut pieces; don't give a severed half a startled face.
if (!kind.endsWith('p')) {
const xs = local.map((v) => v.x), ys = local.map((v) => v.y);
stage.addFace(body, Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys));
}
return new Food(body, mat, color, face, kind);
}
/** Spawn a whole food of `kind` at x, resting just above the floor. */
export function spawnFood(stage: Stage, kind: string, x: number, y: number): Food {
const def = FOODS[kind] ?? FOODS.tomato;
return makeFood(stage, def.verts, x, y, def.material, def.color, def.face, kind);
}