Back to Arañas Pistoleras
SOURCE / PINNED RELEASE

Made of little things.

Arañas Pistoleras

Release
2578a037ceaf…
Author-recorded commit
e35cc0733a7b…
License
LICENSE
Author’s source reference
nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-7bf0a402584

Archive hash verified: 52cdda8c063eaf88…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/scenes.ts
// The five places. Each scene paints a static background once, then a few
// moving details every frame, and tells the game where spiders may appear.

import { rngFrom, type Spot, type View } from './game.js';

export type SceneSound =
  | 'drip' | 'tick' | 'hoot' | 'thunder' | 'bell' | 'clang' | 'globe' | 'squeak'
  | 'buzz' | 'bonk' | 'chime' | 'lamp' | 'cricket' | 'bat' | 'flicker' | 'pot';

export interface Light {
  x: number;
  y: number;
  r: number;
  /** 0 to 1: how much of the darkness it takes away. */
  power: number;
}

export interface Toy {
  id: string;
  x: number;
  y: number;
  r: number;
}

export interface Scene {
  view: View;
  floorY: number;
  spots: Spot[];
  toys: Toy[];
  paintStatic(ctx: CanvasRenderingContext2D): void;
  /** Moving things behind the spiders. */
  paintLive(ctx: CanvasRenderingContext2D, t: number): void;
  /** Moving things in front of the spiders. */
  paintFront(ctx: CanvasRenderingContext2D, t: number): void;
  step(dt: number, t: number, emit: (s: SceneSound, x: number) => void): void;
  darkness(t: number): { level: number; lights: Light[] };
  /** A toy was clicked; the sound it makes, if any. */
  poke(id: string, t: number): SceneSound | null;
}

type Ctx = CanvasRenderingContext2D;
const TAU = Math.PI * 2;

function rr(ctx: Ctx, x: number, y: number, w: number, h: number, r: number, fill: string): void {
  ctx.fillStyle = fill;
  ctx.beginPath();
  ctx.roundRect(x, y, w, h, r);
  ctx.fill();
}

function box(ctx: Ctx, x: number, y: number, w: number, h: number, fill: string): void {
  ctx.fillStyle = fill;
  ctx.fillRect(x, y, w, h);
}

function disc(ctx: Ctx, x: number, y: number, r: number, fill: string): void {
  ctx.fillStyle = fill;
  ctx.beginPath();
  ctx.arc(x, y, r, 0, TAU);
  ctx.fill();
}

function oval(ctx: Ctx, x: number, y: number, rx: number, ry: number, fill: string): void {
  ctx.fillStyle = fill;
  ctx.beginPath();
  ctx.ellipse(x, y, rx, ry, 0, 0, TAU);
  ctx.fill();
}

function vgrad(ctx: Ctx, y0: number, y1: number, stops: string[]): CanvasGradient {
  const g = ctx.createLinearGradient(0, y0, 0, y1);
  stops.forEach((c, i) => g.addColorStop(i / (stops.length - 1), c));
  return g;
}

function poly(ctx: Ctx, pts: number[], fill: string): void {
  ctx.fillStyle = fill;
  ctx.beginPath();
  ctx.moveTo(pts[0], pts[1]);
  for (let i = 2; i < pts.length; i += 2) ctx.lineTo(pts[i], pts[i + 1]);
  ctx.closePath();
  ctx.fill();
}

/** A web in a corner: threads from (x, y) fanning between two angles. */
function cobweb(ctx: Ctx, x: number, y: number, r: number, a0: number, a1: number, alpha = 0.34): void {
  ctx.save();
  ctx.strokeStyle = `rgba(230,236,255,${alpha})`;
  ctx.lineWidth = 1.6;
  const n = 6;
  for (let i = 0; i <= n; i++) {
    const a = a0 + ((a1 - a0) * i) / n;
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x + Math.cos(a) * r, y + Math.sin(a) * r);
    ctx.stroke();
  }
  for (let ring = 1; ring <= 4; ring++) {
    const rad = (r * ring) / 4.4;
    ctx.beginPath();
    for (let i = 0; i < n; i++) {
      const b0 = a0 + ((a1 - a0) * i) / n;
      const b1 = a0 + ((a1 - a0) * (i + 1)) / n;
      const mid = (b0 + b1) / 2;
      const p0x = x + Math.cos(b0) * rad;
      const p0y = y + Math.sin(b0) * rad;
      if (i === 0) ctx.moveTo(p0x, p0y);
      ctx.quadraticCurveTo(x + Math.cos(mid) * rad * 0.82, y + Math.sin(mid) * rad * 0.82, x + Math.cos(b1) * rad, y + Math.sin(b1) * rad);
    }
    ctx.stroke();
  }
  ctx.restore();
}

function moon(ctx: Ctx, x: number, y: number, r: number): void {
  const halo = ctx.createRadialGradient(x, y, r * 0.8, x, y, r * 3.2);
  halo.addColorStop(0, 'rgba(210,225,255,0.34)');
  halo.addColorStop(1, 'rgba(210,225,255,0)');
  ctx.fillStyle = halo;
  ctx.fillRect(x - r * 3.3, y - r * 3.3, r * 6.6, r * 6.6);
  disc(ctx, x, y, r, '#f2f0d8');
  disc(ctx, x - r * 0.3, y - r * 0.2, r * 0.2, '#dcd9bd');
  disc(ctx, x + r * 0.34, y + r * 0.3, r * 0.26, '#dcd9bd');
  disc(ctx, x + r * 0.2, y - r * 0.46, r * 0.12, '#dcd9bd');
}

/** Dark corners, baked into the background. */
function vignette(ctx: Ctx, v: View, strength = 0.55): void {
  const g = ctx.createRadialGradient(v.w / 2, v.h * 0.48, Math.min(v.w, v.h) * 0.35, v.w / 2, v.h * 0.5, Math.hypot(v.w, v.h) * 0.62);
  g.addColorStop(0, 'rgba(4,5,14,0)');
  g.addColorStop(1, `rgba(4,5,14,${strength})`);
  ctx.fillStyle = g;
  ctx.fillRect(0, 0, v.w, v.h);
}

/** A floor of boards running towards the viewer. */
function planks(ctx: Ctx, v: View, floorY: number, a: string, b: string, line: string): void {
  ctx.fillStyle = vgrad(ctx, floorY, v.h, [b, a]);
  ctx.fillRect(0, floorY, v.w, v.h - floorY);
  ctx.strokeStyle = line;
  ctx.lineWidth = 2;
  const vx = v.w / 2;
  const vy = floorY - (v.h - floorY) * 1.1;
  for (let i = -14; i <= 14; i++) {
    const bx = vx + i * v.w * 0.11;
    const tx = vx + ((bx - vx) * (floorY - vy)) / (v.h - vy);
    ctx.beginPath();
    ctx.moveTo(tx, floorY);
    ctx.lineTo(bx, v.h);
    ctx.stroke();
  }
  ctx.fillStyle = 'rgba(0,0,0,0.28)';
  ctx.fillRect(0, floorY, v.w, 7);
}

function crawlSpots(v: View, floorY: number): Spot[] {
  const y = floorY + (v.h - floorY) * 0.5 - 44;
  return [
    { mode: 'crawl', x: v.w * 0.28, y, from: -1 },
    { mode: 'crawl', x: v.w * 0.72, y: y + 12, from: 1 },
  ];
}

function dropSpots(v: View, xs: number[], ys: number[]): Spot[] {
  return xs.map((x, i) => ({ mode: 'drop' as const, x: v.w * x, y: v.h * ys[i % ys.length] }));
}

function scaleOf(v: View): number {
  return Math.max(0.6, Math.min(1.15, v.w / 1265));
}

// ---------------------------------------------------------------- colegio

function colegio(v: View): Scene {
  const k = scaleOf(v);
  const floorY = v.h * 0.69;
  const rng = rngFrom(11);
  const bw = Math.min(v.w * 0.46, 560 * k);
  const bh = bw * 0.46;
  const bx = v.w / 2 - bw / 2;
  const by = floorY - 190 * k - bh;
  const deskTop = floorY - 72 * k;
  const desks = [0.2, 0.5, 0.8].map((f) => v.w * f);
  const clock = { x: v.w * 0.88, y: Math.max(70 * k, floorY - 400 * k), r: 44 * k };
  const bell = { x: v.w * 0.88, y: clock.y + 120 * k, r: 26 * k };
  const globe = { x: desks[2] + 52 * k, y: deskTop - 48 * k, r: 34 * k };
  const lamps = [v.w * 0.28, v.w * 0.72];
  let flicker = 0;
  let nextFlicker = 5;
  let bellT = -9;
  let spin = 0;
  let spinV = 0;
  let lastTick = -1;

  const spots: Spot[] = [
    ...dropSpots(v, [0.13, 0.36, 0.64, 0.87], [0.33, 0.27, 0.3, 0.36]),
    ...desks.map((x) => ({ mode: 'peek' as const, x, y: 0, dir: 'up' as const, edge: deskTop })),
    // On top of the blackboard, where there is room above it.
    ...(by > 230 ? [{ mode: 'peek' as const, x: v.w / 2, y: 0, dir: 'up' as const, edge: by - 10 * k }] : []),
    ...crawlSpots(v, floorY),
  ];

  return {
    view: v,
    floorY,
    spots,
    toys: [
      { id: 'bell', ...bell, r: bell.r + 22 },
      { id: 'globe', ...globe, r: globe.r + 18 },
    ],
    paintStatic(ctx) {
      ctx.fillStyle = vgrad(ctx, 0, floorY, ['#2e4a52', '#44686a', '#507a74']);
      ctx.fillRect(0, 0, v.w, floorY);
      // Wainscot.
      box(ctx, 0, floorY - 150 * k, v.w, 150 * k, '#35544f');
      box(ctx, 0, floorY - 156 * k, v.w, 8 * k, '#27403c');
      for (let x = 0; x < v.w; x += 46 * k) box(ctx, x, floorY - 148 * k, 2, 148 * k, 'rgba(0,0,0,0.13)');
      planks(ctx, v, floorY, '#6a4a31', '#553a27', 'rgba(0,0,0,0.25)');

      // Window with the last light of the day.
      const ww = 150 * k;
      const wh = 220 * k;
      const wx = v.w * 0.12 - ww / 2;
      const wy = Math.max(60 * k, floorY - 440 * k);
      rr(ctx, wx - 10 * k, wy - 10 * k, ww + 20 * k, wh + 20 * k, 6, '#cfc6ae');
      ctx.fillStyle = vgrad(ctx, wy, wy + wh, ['#2a1f4f', '#6a3566', '#d9703f']);
      ctx.fillRect(wx, wy, ww, wh);
      // A bare branch outside.
      ctx.strokeStyle = '#150f1f';
      ctx.lineCap = 'round';
      ctx.lineWidth = 7 * k;
      ctx.beginPath();
      ctx.moveTo(wx, wy + wh * 0.8);
      ctx.quadraticCurveTo(wx + ww * 0.4, wy + wh * 0.6, wx + ww * 0.9, wy + wh * 0.25);
      ctx.stroke();
      ctx.lineWidth = 3.5 * k;
      ctx.beginPath();
      ctx.moveTo(wx + ww * 0.42, wy + wh * 0.58);
      ctx.lineTo(wx + ww * 0.5, wy + wh * 0.3);
      ctx.moveTo(wx + ww * 0.66, wy + wh * 0.43);
      ctx.lineTo(wx + ww * 0.95, wy + wh * 0.5);
      ctx.stroke();
      box(ctx, wx + ww / 2 - 4 * k, wy, 8 * k, wh, '#cfc6ae');
      box(ctx, wx, wy + wh * 0.45, ww, 8 * k, '#cfc6ae');
      rr(ctx, wx - 18 * k, wy + wh + 8 * k, ww + 36 * k, 12 * k, 4, '#b8ae95');

      // Blackboard.
      rr(ctx, bx - 14 * k, by - 14 * k, bw + 28 * k, bh + 28 * k, 8, '#8a5a33');
      ctx.fillStyle = vgrad(ctx, by, by + bh, ['#24453a', '#1b352d']);
      ctx.fillRect(bx, by, bw, bh);
      ctx.save();
      ctx.beginPath();
      ctx.rect(bx, by, bw, bh);
      ctx.clip();
      // Wiped chalk.
      ctx.strokeStyle = 'rgba(255,255,255,0.05)';
      ctx.lineWidth = 26 * k;
      for (let i = 0; i < 5; i++) {
        ctx.beginPath();
        ctx.moveTo(bx + rng() * bw, by + rng() * bh);
        ctx.quadraticCurveTo(bx + rng() * bw, by + rng() * bh, bx + rng() * bw, by + rng() * bh);
        ctx.stroke();
      }
      ctx.fillStyle = 'rgba(245,245,235,0.88)';
      ctx.strokeStyle = 'rgba(245,245,235,0.88)';
      ctx.font = `${Math.round(bh * 0.2)}px "Comic Sans MS", "Chalkboard SE", "Segoe Print", cursive`;
      ctx.textBaseline = 'middle';
      ctx.fillText('2 + 2 = 4', bx + bw * 0.06, by + bh * 0.2);
      ctx.fillStyle = 'rgba(255,214,120,0.9)';
      ctx.fillText('A B C', bx + bw * 0.08, by + bh * 0.5);
      // A chalk spider, crossed out.
      const sx = bx + bw * 0.72;
      const sy = by + bh * 0.42;
      const sr = bh * 0.14;
      ctx.lineWidth = 3.4 * k;
      ctx.lineCap = 'round';
      ctx.beginPath();
      ctx.arc(sx, sy, sr, 0, TAU);
      ctx.stroke();
      for (let i = 0; i < 4; i++) {
        for (const side of [-1, 1]) {
          const a = -0.6 + i * 0.42;
          ctx.beginPath();
          ctx.moveTo(sx + side * sr * Math.cos(a), sy + sr * Math.sin(a));
          ctx.lineTo(sx + side * sr * 1.9, sy + sr * (Math.sin(a) * 1.4 - 0.5));
          ctx.lineTo(sx + side * sr * 2.5, sy + sr * (Math.sin(a) * 1.8 + 0.5));
          ctx.stroke();
        }
      }
      disc(ctx, sx - sr * 0.35, sy - sr * 0.15, sr * 0.15, 'rgba(245,245,235,0.9)');
      disc(ctx, sx + sr * 0.35, sy - sr * 0.15, sr * 0.15, 'rgba(245,245,235,0.9)');
      ctx.strokeStyle = 'rgba(255,110,110,0.9)';
      ctx.lineWidth = 5 * k;
      ctx.beginPath();
      ctx.moveTo(sx - sr * 2.4, sy - sr * 1.7);
      ctx.lineTo(sx + sr * 2.4, sy + sr * 1.7);
      ctx.moveTo(sx + sr * 2.4, sy - sr * 1.7);
      ctx.lineTo(sx - sr * 2.4, sy + sr * 1.7);
      ctx.stroke();
      // A chalk sun and a little house.
      ctx.strokeStyle = 'rgba(255,214,120,0.85)';
      ctx.lineWidth = 3 * k;
      const ux = bx + bw * 0.42;
      const uy = by + bh * 0.78;
      ctx.beginPath();
      ctx.arc(ux, uy, bh * 0.07, 0, TAU);
      ctx.stroke();
      for (let i = 0; i < 8; i++) {
        const a = (i / 8) * TAU;
        ctx.beginPath();
        ctx.moveTo(ux + Math.cos(a) * bh * 0.1, uy + Math.sin(a) * bh * 0.1);
        ctx.lineTo(ux + Math.cos(a) * bh * 0.15, uy + Math.sin(a) * bh * 0.15);
        ctx.stroke();
      }
      ctx.strokeStyle = 'rgba(160,220,255,0.85)';
      const hx = bx + bw * 0.12;
      const hy = by + bh * 0.9;
      ctx.beginPath();
      ctx.rect(hx, hy - bh * 0.16, bh * 0.2, bh * 0.16);
      ctx.moveTo(hx - bh * 0.03, hy - bh * 0.16);
      ctx.lineTo(hx + bh * 0.1, hy - bh * 0.28);
      ctx.lineTo(hx + bh * 0.23, hy - bh * 0.16);
      ctx.stroke();
      ctx.restore();
      rr(ctx, bx - 4 * k, by + bh + 12 * k, bw + 8 * k, 10 * k, 3, '#6d4526');
      rr(ctx, bx + bw * 0.7, by + bh + 2 * k, 40 * k, 12 * k, 3, '#d8d0c0');
      box(ctx, bx + bw * 0.2, by + bh + 6 * k, 22 * k, 6 * k, '#ffffff');
      box(ctx, bx + bw * 0.27, by + bh + 6 * k, 16 * k, 6 * k, '#ffd678');

      // Children's drawings pinned up.
      const pinY = bell.y + 70 * k;
      const colors = ['#e9d8a6', '#c7e0d0', '#f0c7c0'];
      for (let i = 0; i < 3; i++) {
        const px = v.w * 0.82 + i * 44 * k - 20 * k;
        if (pinY + 60 * k > floorY - 160 * k) break;
        ctx.save();
        ctx.translate(px, pinY);
        ctx.rotate((rng() - 0.5) * 0.2);
        box(ctx, -17 * k, 0, 34 * k, 44 * k, colors[i]);
        ctx.strokeStyle = ['#c0392b', '#2e7d6b', '#5b4fc0'][i];
        ctx.lineWidth = 2.4 * k;
        ctx.beginPath();
        ctx.arc(0, 20 * k, 8 * k, 0, TAU);
        ctx.moveTo(-10 * k, 36 * k);
        ctx.lineTo(10 * k, 36 * k);
        ctx.stroke();
        disc(ctx, 0, 3 * k, 2.6 * k, '#d23');
        ctx.restore();
      }

      // Clock face; its hands are painted live.
      disc(ctx, clock.x, clock.y, clock.r + 7 * k, '#2a2f3a');
      disc(ctx, clock.x, clock.y, clock.r, '#efe9d6');
      for (let i = 0; i < 12; i++) {
        const a = (i / 12) * TAU;
        disc(ctx, clock.x + Math.cos(a) * clock.r * 0.82, clock.y + Math.sin(a) * clock.r * 0.82, (i % 3 ? 1.8 : 3.4) * k, '#2a2f3a');
      }

      // Lamp housings.
      for (const lx of lamps) {
        box(ctx, lx - 3 * k, 0, 6 * k, 20 * k, '#1c2228');
        rr(ctx, lx - 92 * k, 18 * k, 184 * k, 16 * k, 6, '#2b343c');
      }

      // Desks with a chair behind each.
      desks.forEach((dx, i) => {
        const w = 212 * k;
        rr(ctx, dx - 52 * k, deskTop - 74 * k, 104 * k, 70 * k, 12 * k, '#5f7a34');
        box(ctx, dx - 40 * k, deskTop - 10 * k, 8 * k, 30 * k, '#3a4048');
        box(ctx, dx + 32 * k, deskTop - 10 * k, 8 * k, 30 * k, '#3a4048');
        // Legs.
        for (const lx of [-w / 2 + 12 * k, w / 2 - 20 * k]) box(ctx, dx + lx, deskTop + 10 * k, 9 * k, 138 * k, '#444b55');
        box(ctx, dx - w / 2 + 6 * k, deskTop + 142 * k, 36 * k, 7 * k, '#30363e');
        box(ctx, dx + w / 2 - 42 * k, deskTop + 142 * k, 36 * k, 7 * k, '#30363e');
        // Book box and top.
        rr(ctx, dx - w / 2 + 14 * k, deskTop + 12 * k, w - 28 * k, 50 * k, 5, '#8f6238');
        rr(ctx, dx - w / 2 + 26 * k, deskTop + 22 * k, w - 52 * k, 30 * k, 4, '#2a1c12');
        rr(ctx, dx - w / 2, deskTop, w, 17 * k, 5, '#c79657');
        box(ctx, dx - w / 2 + 6 * k, deskTop + 2 * k, w - 12 * k, 3 * k, 'rgba(255,255,255,0.2)');
        if (i === 0) {
          // Books and an apple.
          rr(ctx, dx - 70 * k, deskTop - 14 * k, 70 * k, 14 * k, 3, '#a8323a');
          rr(ctx, dx - 64 * k, deskTop - 26 * k, 60 * k, 12 * k, 3, '#3b6fb5');
          disc(ctx, dx + 44 * k, deskTop - 16 * k, 16 * k, '#c8322c');
          disc(ctx, dx + 39 * k, deskTop - 21 * k, 5 * k, 'rgba(255,255,255,0.35)');
          box(ctx, dx + 43 * k, deskTop - 37 * k, 3 * k, 8 * k, '#3d2a16');
        } else if (i === 1) {
          // A cup of pencils and a paper plane.
          rr(ctx, dx - 62 * k, deskTop - 34 * k, 28 * k, 34 * k, 4, '#3d8a8a');
          const pens = ['#f2c531', '#e2574c', '#6fbf5a'];
          pens.forEach((c, j) => {
            ctx.save();
            ctx.translate(dx - 56 * k + j * 8 * k, deskTop - 32 * k);
            ctx.rotate(-0.18 + j * 0.18);
            box(ctx, -2.5 * k, -30 * k, 5 * k, 30 * k, c);
            ctx.restore();
          });
          poly(ctx, [dx + 10 * k, deskTop - 4 * k, dx + 84 * k, deskTop - 22 * k, dx + 46 * k, deskTop - 2 * k], '#f1efe6');
          poly(ctx, [dx + 46 * k, deskTop - 2 * k, dx + 84 * k, deskTop - 22 * k, dx + 60 * k, deskTop - 1 * k], '#c9c6ba');
        } else {
          // The globe's stand; the ball is painted live.
          box(ctx, globe.x - 3 * k, deskTop - 14 * k, 6 * k, 14 * k, '#6d4526');
          rr(ctx, globe.x - 22 * k, deskTop - 6 * k, 44 * k, 7 * k, 3, '#6d4526');
        }
      });
      cobweb(ctx, 0, 0, 210 * k, 0, Math.PI / 2);
      cobweb(ctx, v.w, 0, 170 * k, Math.PI / 2, Math.PI);
      vignette(ctx, v, 0.5);
    },
    paintLive(ctx, t) {
      // Lamps.
      const on = 1 - flicker;
      for (const lx of lamps) {
        rr(ctx, lx - 84 * k, 30 * k, 168 * k, 8 * k, 4, `rgba(230,255,240,${0.25 + 0.75 * on})`);
        ctx.fillStyle = `rgba(200,255,230,${0.07 * on})`;
        ctx.beginPath();
        ctx.moveTo(lx - 84 * k, 38 * k);
        ctx.lineTo(lx + 84 * k, 38 * k);
        ctx.lineTo(lx + 300 * k, floorY);
        ctx.lineTo(lx - 300 * k, floorY);
        ctx.closePath();
        ctx.fill();
      }
      // Clock hands: it is always a quarter past spider.
      const sec = Math.floor(t);
      ctx.lineCap = 'round';
      ctx.strokeStyle = '#2a2f3a';
      ctx.lineWidth = 5 * k;
      ctx.beginPath();
      ctx.moveTo(clock.x, clock.y);
      ctx.lineTo(clock.x + clock.r * 0.45, clock.y - clock.r * 0.25);
      ctx.stroke();
      ctx.lineWidth = 3.5 * k;
      const ma = (t / 60) * TAU - Math.PI / 2;
      ctx.beginPath();
      ctx.moveTo(clock.x, clock.y);
      ctx.lineTo(clock.x + Math.cos(ma) * clock.r * 0.72, clock.y + Math.sin(ma) * clock.r * 0.72);
      ctx.stroke();
      ctx.strokeStyle = '#c0392b';
      ctx.lineWidth = 2 * k;
      const sa = (sec / 60) * TAU * 5 - Math.PI / 2;
      ctx.beginPath();
      ctx.moveTo(clock.x, clock.y);
      ctx.lineTo(clock.x + Math.cos(sa) * clock.r * 0.8, clock.y + Math.sin(sa) * clock.r * 0.8);
      ctx.stroke();
      disc(ctx, clock.x, clock.y, 4 * k, '#2a2f3a');
      // The bell shakes while it rings.
      const ring = Math.max(0, 1 - (t - bellT) / 1.2);
      ctx.save();
      ctx.translate(bell.x + Math.sin(t * 70) * 4 * k * ring, bell.y);
      disc(ctx, 0, 0, bell.r, '#b3261e');
      disc(ctx, 0, 0, bell.r * 0.72, '#d6402f');
      disc(ctx, 0, 0, bell.r * 0.2, '#f1c9a0');
      box(ctx, -5 * k, bell.r - 2 * k, 10 * k, 12 * k, '#2a2f3a');
      ctx.restore();
      // Globe.
      ctx.save();
      ctx.translate(globe.x, globe.y);
      disc(ctx, 0, 0, globe.r, '#2f79b8');
      ctx.beginPath();
      ctx.arc(0, 0, globe.r, 0, TAU);
      ctx.clip();
      ctx.fillStyle = '#5fae58';
      for (let i = 0; i < 4; i++) {
        const gx = ((((i * 0.5 + spin / TAU) % 2) + 2) % 2) * globe.r * 2 - globe.r * 2;
        ctx.beginPath();
        ctx.ellipse(gx, (i % 2 ? -1 : 1) * globe.r * 0.25, globe.r * 0.36, globe.r * (0.3 + 0.12 * i), 0.4, 0, TAU);
        ctx.fill();
      }
      ctx.restore();
      ctx.strokeStyle = '#d9c9a0';
      ctx.lineWidth = 3 * k;
      ctx.beginPath();
      ctx.arc(globe.x, globe.y, globe.r + 4 * k, -1.2, 1.9);
      ctx.stroke();
    },
    paintFront() {},
    step(dt, t, emit) {
      nextFlicker -= dt;
      if (nextFlicker <= 0) {
        flicker = 1;
        nextFlicker = 6 + Math.random() * 9;
        emit('flicker', v.w / 2);
      }
      // A stuttering recovery.
      flicker = flicker > 0.02 ? Math.max(0, flicker - dt * 2.2) * (Math.sin(t * 50) > 0.2 ? 1 : 0.4) : 0;
      spin += spinV * dt;
      spinV *= Math.pow(0.4, dt);
      const sec = Math.floor(t);
      if (sec !== lastTick) {
        lastTick = sec;
        emit('tick', clock.x);
      }
    },
    darkness() {
      return {
        level: 0.26 + 0.4 * flicker,
        lights: lamps.map((x) => ({ x, y: floorY * 0.45, r: 430 * k, power: 0.5 * (1 - flicker) })),
      };
    },
    poke(id, t) {
      if (id === 'bell') {
        bellT = t;
        return 'bell';
      }
      spinV = 9;
      return 'globe';
    },
  };
}

// ----------------------------------------------------------------- cocina

function cocina(v: View): Scene {
  const k = scaleOf(v);
  const floorY = v.h * 0.69;
  const counterTop = floorY - 96 * k;
  const c0 = v.w * 0.27;
  const c1 = v.w * 0.78;
  const fridge = { x: v.w * 0.13, w: 172 * k, top: Math.max(225, floorY - 345 * k), bottom: floorY + 58 * k };
  const stoveX = c0 + (c1 - c0) * 0.26;
  const sinkX = c0 + (c1 - c0) * 0.72;
  const tableX = v.w * 0.89;
  const tableTop = floorY - 66 * k;
  const rackY = Math.max(70 * k, floorY - 410 * k);
  const pans = [0, 1, 2].map((i) => ({ x: v.w * 0.82 + i * 62 * k, swing: 0, vel: 0, r: (30 - i * 4) * k }));
  const lamp = { x: v.w * 0.5, y: Math.max(90 * k, floorY - 470 * k) };
  let dripT = 0;
  let lid = 0;
  let nextLid = 3;
  const steam: { x: number; y: number; a: number; r: number }[] = [];
  const cabY = Math.max(96 * k, floorY - 410 * k);
  const cab = { x: c0 + 8 * k, w: (c1 - c0) * 0.44, h: 128 * k };

  const spots: Spot[] = [
    ...dropSpots(v, [0.3, 0.44, 0.63, 0.83], [0.3, 0.38, 0.29, 0.36]),
    { mode: 'peek', x: fridge.x, y: 0, dir: 'up', edge: fridge.top },
    { mode: 'peek', x: c0 + (c1 - c0) * 0.5, y: 0, dir: 'up', edge: counterTop },
    { mode: 'peek', x: c1 - 40 * k, y: 0, dir: 'up', edge: counterTop },
    { mode: 'peek', x: tableX, y: 0, dir: 'up', edge: tableTop },
    ...crawlSpots(v, floorY),
  ];

  return {
    view: v,
    floorY,
    spots,
    toys: pans.map((p, i) => ({ id: `pan${i}`, x: p.x, y: rackY + 70 * k, r: 46 * k })),
    paintStatic(ctx) {
      ctx.fillStyle = vgrad(ctx, 0, floorY, ['#3d3040', '#5a4544', '#6d5646']);
      ctx.fillRect(0, 0, v.w, floorY);
      // Tiles behind the counter.
      const tile = 34 * k;
      const ty0 = counterTop - tile * 4;
      for (let y = ty0; y < counterTop; y += tile) {
        for (let x = 0; x < v.w; x += tile) {
          const odd = (Math.round(x / tile) + Math.round((y - ty0) / tile)) % 5 === 0;
          box(ctx, x + 1, y + 1, tile - 2, tile - 2, odd ? '#5e8a84' : '#b9ad92');
        }
      }
      box(ctx, 0, ty0 - 6 * k, v.w, 6 * k, '#7a6a50');
      // Checkered floor running towards the viewer.
      const vx = v.w / 2;
      const depth = v.h - floorY;
      const rows = 5;
      for (let r = 0; r < rows; r++) {
        const y0 = floorY + depth * Math.pow(r / rows, 1.7);
        const y1 = floorY + depth * Math.pow((r + 1) / rows, 1.7);
        const s0 = 0.5 + (y0 - floorY) / depth;
        const s1 = 0.5 + (y1 - floorY) / depth;
        for (let c = -9; c < 9; c++) {
          const cw = v.w * 0.13;
          ctx.fillStyle = (r + c) & 1 ? '#3a3542' : '#93896f';
          ctx.beginPath();
          ctx.moveTo(vx + c * cw * s0, y0);
          ctx.lineTo(vx + (c + 1) * cw * s0, y0);
          ctx.lineTo(vx + (c + 1) * cw * s1, y1);
          ctx.lineTo(vx + c * cw * s1, y1);
          ctx.closePath();
          ctx.fill();
        }
      }
      ctx.fillStyle = vgrad(ctx, floorY, v.h, ['rgba(10,8,20,0.5)', 'rgba(10,8,20,0)']);
      ctx.fillRect(0, floorY, v.w, depth);

      // Window over the sink, with the moon.
      const ww = 170 * k;
      const wh = 150 * k;
      const wx = sinkX - ww / 2;
      const wy = cabY - 6 * k;
      rr(ctx, wx - 9 * k, wy - 9 * k, ww + 18 * k, wh + 18 * k, 6, '#d9cfb4');
      ctx.fillStyle = vgrad(ctx, wy, wy + wh, ['#0e1638', '#263c6a']);
      ctx.fillRect(wx, wy, ww, wh);
      ctx.save();
      ctx.beginPath();
      ctx.rect(wx, wy, ww, wh);
      ctx.clip();
      moon(ctx, wx + ww * 0.62, wy + wh * 0.4, 26 * k);
      ctx.restore();
      box(ctx, wx + ww / 2 - 3 * k, wy, 6 * k, wh, '#d9cfb4');
      // Checked curtains.
      for (const side of [0, 1]) {
        const cx = side ? wx + ww - 44 * k : wx;
        ctx.fillStyle = '#b5413b';
        ctx.beginPath();
        ctx.moveTo(cx, wy);
        ctx.lineTo(cx + 44 * k, wy);
        ctx.quadraticCurveTo(cx + (side ? 44 : 0) * k, wy + wh * 0.5, cx + (side ? 26 : 18) * k, wy + wh);
        ctx.lineTo(cx + (side ? 44 * k : 0), wy + wh);
        ctx.closePath();
        ctx.fill();
        ctx.fillStyle = 'rgba(255,255,255,0.3)';
        for (let i = 0; i < 4; i++) ctx.fillRect(cx, wy + 8 * k + i * 36 * k, 44 * k, 8 * k);
      }

      // Wall cupboards; one door stands open a crack.
      rr(ctx, cab.x, cabY, cab.w, cab.h, 6, '#7c5a3c');
      const doors = 3;
      for (let i = 0; i < doors; i++) {
        const dx = cab.x + 6 * k + (i * (cab.w - 12 * k)) / doors;
        const dw = (cab.w - 12 * k) / doors - 6 * k;
        if (i === 1) {
          rr(ctx, dx, cabY + 6 * k, dw, cab.h - 12 * k, 4, '#0d0a10');
          rr(ctx, dx + dw * 0.55, cabY + 6 * k, dw * 0.5, cab.h - 12 * k, 4, '#946d48');
        } else {
          rr(ctx, dx, cabY + 6 * k, dw, cab.h - 12 * k, 4, '#946d48');
          rr(ctx, dx + 8 * k, cabY + 14 * k, dw - 16 * k, cab.h - 28 * k, 3, '#876140');
          disc(ctx, dx + (i ? 10 * k : dw - 10 * k), cabY + cab.h * 0.7, 4 * k, '#d9c9a0');
        }
      }

      // Fridge with magnets and a drawing.
      rr(ctx, fridge.x - fridge.w / 2, fridge.top, fridge.w, fridge.bottom - fridge.top, 14 * k, '#c9d3d6');
      rr(ctx, fridge.x - fridge.w / 2, fridge.top, fridge.w * 0.12, fridge.bottom - fridge.top, 6 * k, '#aab6ba');
      const split = fridge.top + (fridge.bottom - fridge.top) * 0.34;
      box(ctx, fridge.x - fridge.w / 2, split, fridge.w, 5 * k, '#8d999d');
      rr(ctx, fridge.x + fridge.w * 0.3, split - 62 * k, 9 * k, 48 * k, 4, '#7c888c');
      rr(ctx, fridge.x + fridge.w * 0.3, split + 20 * k, 9 * k, 80 * k, 4, '#7c888c');
      ctx.save();
      ctx.translate(fridge.x - 14 * k, split + 60 * k);
      ctx.rotate(-0.06);
      box(ctx, -34 * k, 0, 68 * k, 80 * k, '#fbf6e4');
      ctx.strokeStyle = '#7a3fb0';
      ctx.lineWidth = 3 * k;
      ctx.lineCap = 'round';
      ctx.beginPath();
      ctx.arc(0, 38 * k, 13 * k, 0, TAU);
      for (let i = 0; i < 4; i++) {
        ctx.moveTo(-12 * k, 30 * k + i * 5 * k);
        ctx.lineTo(-27 * k, 22 * k + i * 11 * k);
        ctx.moveTo(12 * k, 30 * k + i * 5 * k);
        ctx.lineTo(27 * k, 22 * k + i * 11 * k);
      }
      ctx.stroke();
      disc(ctx, -5 * k, 35 * k, 2.4 * k, '#d23');
      disc(ctx, 5 * k, 35 * k, 2.4 * k, '#d23');
      disc(ctx, 0, 5 * k, 5 * k, '#e2574c');
      ctx.restore();
      disc(ctx, fridge.x - 40 * k, fridge.top + 40 * k, 8 * k, '#f2c531');
      disc(ctx, fridge.x + 10 * k, fridge.top + 64 * k, 7 * k, '#6fbf5a');

      // Counter: cupboards, stove and sink.
      rr(ctx, c0, counterTop, c1 - c0, floorY + 56 * k - counterTop, 4, '#7c5a3c');
      const units = 4;
      for (let i = 0; i < units; i++) {
        const ux = c0 + 8 * k + (i * (c1 - c0 - 16 * k)) / units;
        const uw = (c1 - c0 - 16 * k) / units - 8 * k;
        if (i === 1) continue;
        rr(ctx, ux, counterTop + 26 * k, uw, floorY + 44 * k - counterTop - 26 * k, 4, '#946d48');
        disc(ctx, ux + uw / 2, counterTop + 44 * k, 4.5 * k, '#d9c9a0');
      }
      rr(ctx, c0 - 8 * k, counterTop, c1 - c0 + 16 * k, 16 * k, 4, '#d8d2c2');
      box(ctx, c0 - 8 * k, counterTop + 13 * k, c1 - c0 + 16 * k, 3 * k, '#a59f8f');
      // Oven.
      const ow = (c1 - c0) / units - 4 * k;
      const ox = stoveX - ow / 2;
      rr(ctx, ox, counterTop + 16 * k, ow, floorY + 44 * k - counterTop - 16 * k, 4, '#30333c');
      rr(ctx, ox + 12 * k, counterTop + 52 * k, ow - 24 * k, 62 * k, 6, '#12131a');
      rr(ctx, ox + 10 * k, counterTop + 38 * k, ow - 20 * k, 6 * k, 3, '#9aa3ad');
      for (let i = 0; i < 4; i++) disc(ctx, ox + 18 * k + (i * (ow - 36 * k)) / 3, counterTop + 27 * k, 5 * k, '#c9d2e0');
      // Burner and pot; the lid and steam are live.
      rr(ctx, stoveX - 46 * k, counterTop - 6 * k, 92 * k, 7 * k, 3, '#1b1d24');
      rr(ctx, stoveX - 40 * k, counterTop - 58 * k, 80 * k, 52 * k, 8 * k, '#b8493c');
      rr(ctx, stoveX - 40 * k, counterTop - 58 * k, 16 * k, 52 * k, 6 * k, '#d76a59');
      rr(ctx, stoveX - 56 * k, counterTop - 46 * k, 18 * k, 8 * k, 4, '#2b2e36');
      rr(ctx, stoveX + 38 * k, counterTop - 46 * k, 18 * k, 8 * k, 4, '#2b2e36');
      // Sink and tap.
      rr(ctx, sinkX - 62 * k, counterTop - 4 * k, 124 * k, 12 * k, 4, '#8f9aa6');
      ctx.strokeStyle = '#aab4c0';
      ctx.lineWidth = 8 * k;
      ctx.lineCap = 'round';
      ctx.beginPath();
      ctx.moveTo(sinkX + 34 * k, counterTop - 2 * k);
      ctx.lineTo(sinkX + 34 * k, counterTop - 52 * k);
      ctx.quadraticCurveTo(sinkX + 34 * k, counterTop - 74 * k, sinkX + 8 * k, counterTop - 70 * k);
      ctx.lineTo(sinkX + 2 * k, counterTop - 56 * k);
      ctx.stroke();
      // A stack of plates and a jar of spoons.
      for (let i = 0; i < 4; i++) rr(ctx, c0 + 14 * k, counterTop - 8 * k - i * 6 * k, 62 * k, 6 * k, 3, i % 2 ? '#e9e4d6' : '#cfc9ba');
      rr(ctx, sinkX - 110 * k, counterTop - 42 * k, 30 * k, 42 * k, 5, '#5e8a84');
      ctx.strokeStyle = '#c9a36a';
      ctx.lineWidth = 5 * k;
      for (let i = 0; i < 3; i++) {
        ctx.beginPath();
        ctx.moveTo(sinkX - 102 * k + i * 7 * k, counterTop - 40 * k);
        ctx.lineTo(sinkX - 110 * k + i * 12 * k, counterTop - 78 * k);
        ctx.stroke();
      }

      // Rack for the pans.
      box(ctx, pans[0].x - 44 * k, rackY, pans[2].x - pans[0].x + 88 * k, 7 * k, '#4a4f58');
      box(ctx, pans[0].x - 38 * k, rackY - 22 * k, 5 * k, 24 * k, '#4a4f58');
      box(ctx, pans[2].x + 33 * k, rackY - 22 * k, 5 * k, 24 * k, '#4a4f58');

      // Table with a cloth and a cake.
      const tw = 190 * k;
      box(ctx, tableX - tw / 2 + 14 * k, tableTop, 10 * k, floorY + 70 * k - tableTop, '#5a3d29');
      box(ctx, tableX + tw / 2 - 24 * k, tableTop, 10 * k, floorY + 70 * k - tableTop, '#5a3d29');
      ctx.fillStyle = '#e6dfcd';
      ctx.beginPath();
      ctx.moveTo(tableX - tw / 2, tableTop);
      ctx.lineTo(tableX + tw / 2, tableTop);
      ctx.lineTo(tableX + tw / 2 + 6 * k, tableTop + 62 * k);
      for (let i = 5; i >= 0; i--) {
        const sx = tableX - tw / 2 - 6 * k + ((tw + 12 * k) * i) / 5;
        ctx.quadraticCurveTo(sx + (tw + 12 * k) / 10, tableTop + 78 * k, sx, tableTop + 62 * k);
      }
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = 'rgba(181,65,59,0.5)';
      for (let i = 0; i < 5; i++) ctx.fillRect(tableX - tw / 2 + 10 * k + i * 38 * k, tableTop + 2 * k, 12 * k, 58 * k);
      rr(ctx, tableX - 50 * k, tableTop - 10 * k, 100 * k, 10 * k, 4, '#e9e4d6');
      rr(ctx, tableX - 38 * k, tableTop - 46 * k, 76 * k, 38 * k, 8 * k, '#f4d9d0');
      rr(ctx, tableX - 38 * k, tableTop - 50 * k, 76 * k, 14 * k, 7 * k, '#e2574c');
      disc(ctx, tableX, tableTop - 56 * k, 7 * k, '#c8322c');

      // Lamp cord and shade.
      box(ctx, lamp.x - 2 * k, 0, 4 * k, lamp.y - 30 * k, '#1b1d24');
      poly(ctx, [lamp.x - 22 * k, lamp.y - 32 * k, lamp.x + 22 * k, lamp.y - 32 * k, lamp.x + 62 * k, lamp.y + 8 * k, lamp.x - 62 * k, lamp.y + 8 * k], '#d8a03a');
      oval(ctx, lamp.x, lamp.y + 8 * k, 62 * k, 9 * k, '#fff0b8');
      cobweb(ctx, 0, 0, 190 * k, 0, Math.PI / 2);
      cobweb(ctx, v.w, 0, 220 * k, Math.PI / 2, Math.PI);
      vignette(ctx, v, 0.5);
    },
    paintLive(ctx, t) {
      // Eyes in the open cupboard.
      const dw = (cab.w - 12 * k) / 3;
      const ex = cab.x + 6 * k + dw * 1.25;
      const ey = cabY + cab.h * 0.5;
      if ((t * 0.37) % 1 < 0.93) {
        disc(ctx, ex, ey, 5 * k, '#ffe14a');
        disc(ctx, ex + 20 * k, ey, 5 * k, '#ffe14a');
        disc(ctx, ex + Math.sin(t) * 2 * k, ey, 2 * k, '#0d0a10');
        disc(ctx, ex + 20 * k + Math.sin(t) * 2 * k, ey, 2 * k, '#0d0a10');
      }
      // Gas flame.
      for (let i = 0; i < 5; i++) {
        const fx = stoveX - 30 * k + i * 15 * k;
        const fh = (7 + Math.sin(t * 17 + i * 2) * 2.5) * k;
        poly(ctx, [fx - 5 * k, counterTop - 6 * k, fx + 5 * k, counterTop - 6 * k, fx, counterTop - 6 * k - fh], '#59c7ff');
      }
      // Steam.
      for (const s of steam) {
        ctx.fillStyle = `rgba(235,240,245,${s.a * 0.3})`;
        ctx.beginPath();
        ctx.arc(s.x, s.y, s.r, 0, TAU);
        ctx.fill();
      }
      // The lid rattles.
      const hop = lid > 0 ? Math.abs(Math.sin(lid * 30)) * 6 * k : 0;
      ctx.save();
      ctx.translate(stoveX, counterTop - 58 * k - hop);
      ctx.rotate(lid > 0 ? Math.sin(lid * 41) * 0.06 : 0);
      rr(ctx, -44 * k, -8 * k, 88 * k, 10 * k, 5, '#8f2f26');
      disc(ctx, 0, -12 * k, 7 * k, '#2b2e36');
      ctx.restore();
      // Drip from the tap.
      const d = dripT % 2.6;
      if (d < 0.5) {
        oval(ctx, sinkX + 2 * k, counterTop - 52 * k + d * d * 190 * k, 3 * k, 5 * k, '#bfe6ff');
      } else if (d > 2.2) {
        oval(ctx, sinkX + 2 * k, counterTop - 52 * k, 3 * k * ((d - 2.2) / 0.4), 4 * k * ((d - 2.2) / 0.4), '#bfe6ff');
      }
      // Pans on their hooks.
      pans.forEach((p, i) => {
        ctx.save();
        ctx.translate(p.x, rackY + 6 * k);
        ctx.rotate(p.swing);
        box(ctx, -2 * k, 0, 4 * k, 16 * k, '#9aa3ad');
        rr(ctx, -5 * k, 14 * k, 10 * k, 46 * k, 4, '#2b2e36');
        disc(ctx, 0, 60 * k + p.r, p.r, i === 1 ? '#b8733a' : '#3a3f4b');
        disc(ctx, 0, 60 * k + p.r, p.r * 0.78, i === 1 ? '#d99450' : '#565d6b');
        ctx.fillStyle = 'rgba(255,255,255,0.18)';
        ctx.beginPath();
        ctx.arc(0, 60 * k + p.r, p.r * 0.6, 3.6, 4.9);
        ctx.lineTo(0, 60 * k + p.r);
        ctx.fill();
        ctx.restore();
      });
      // Lamp light.
      ctx.fillStyle = 'rgba(255,225,150,0.07)';
      ctx.beginPath();
      ctx.moveTo(lamp.x - 60 * k, lamp.y + 8 * k);
      ctx.lineTo(lamp.x + 60 * k, lamp.y + 8 * k);
      ctx.lineTo(lamp.x + 380 * k, floorY + 40 * k);
      ctx.lineTo(lamp.x - 380 * k, floorY + 40 * k);
      ctx.closePath();
      ctx.fill();
    },
    paintFront() {},
    step(dt, t, emit) {
      const before = dripT % 2.6;
      dripT += dt;
      if (before < 0.5 && dripT % 2.6 >= 0.5) emit('drip', sinkX);
      nextLid -= dt;
      if (nextLid <= 0) {
        lid = 0.7;
        nextLid = 5 + Math.random() * 6;
        emit('pot', stoveX);
      }
      lid = Math.max(0, lid - dt);
      if (Math.random() < dt * 5) steam.push({ x: stoveX + (Math.random() - 0.5) * 50 * k, y: counterTop - 66 * k, a: 1, r: (8 + Math.random() * 8) * k });
      for (let i = steam.length - 1; i >= 0; i--) {
        const s = steam[i];
        s.y -= 40 * k * dt;
        s.x += Math.sin(t * 2 + i) * 8 * dt;
        s.r += 9 * k * dt;
        s.a -= dt * 0.55;
        if (s.a <= 0) steam.splice(i, 1);
      }
      for (const p of pans) {
        p.vel += -p.swing * 38 * dt;
        p.vel *= Math.pow(0.35, dt);
        p.swing += p.vel * dt;
      }
    },
    darkness() {
      return {
        level: 0.36,
        lights: [
          { x: lamp.x, y: lamp.y + 150 * k, r: 470 * k, power: 0.75 },
          { x: stoveX, y: counterTop - 10 * k, r: 110 * k, power: 0.35 },
        ],
      };
    },
    poke(id) {
      const p = pans[Number(id.slice(3))];
      if (!p) return null;
      p.vel += (Math.random() < 0.5 ? -1 : 1) * 3.2;
      return 'clang';
    },
  };
}

// ------------------------------------------------------------- dormitorio

function dormitorio(v: View): Scene {
  const k = scaleOf(v);
  const floorY = v.h * 0.69;
  const rng = rngFrom(33);
  const ward = { x: v.w * 0.12, w: 184 * k, top: Math.max(225, floorY - 400 * k), bottom: floorY + 52 * k };
  const bed = { x0: v.w * 0.47, x1: v.w * 0.86, top: floorY - 74 * k, bottom: floorY + 76 * k };
  const win = { x: v.w * 0.34, y: Math.max(90 * k, floorY - 420 * k), w: 170 * k, h: 240 * k };
  const stand = { x: Math.min(v.w - 60 * k, v.w * 0.935), top: floorY - 40 * k };
  const lampPos = { x: stand.x, y: stand.top - 62 * k };
  const teddy = { x: v.w * 0.33, y: floorY + 62 * k };
  const mobile = { x: v.w * 0.66, y: 0, len: Math.max(70 * k, floorY - 460 * k) };
  let lampOn = true;
  let squeak = 0;
  const stars: [number, number, number][] = [];
  for (let i = 0; i < 46; i++) stars.push([rng() * v.w, rng() * (floorY - 30), rng()]);

  const spots: Spot[] = [
    ...dropSpots(v, [0.22, 0.5, 0.7, 0.88], [0.28, 0.34, 0.26, 0.33]),
    { mode: 'peek', x: ward.x, y: 0, dir: 'up', edge: ward.top },
    { mode: 'peek', x: 0, y: floorY - 150 * k, dir: 'right', edge: ward.x + ward.w / 2 },
    { mode: 'peek', x: (bed.x0 + bed.x1) / 2 - 30 * k, y: 0, dir: 'up', edge: bed.top - 4 * k },
    ...crawlSpots(v, floorY),
  ];

  return {
    view: v,
    floorY,
    spots,
    toys: [
      { id: 'lamp', x: lampPos.x, y: lampPos.y, r: 58 * k },
      { id: 'teddy', x: teddy.x, y: teddy.y - 34 * k, r: 54 * k },
    ],
    paintStatic(ctx) {
      ctx.fillStyle = vgrad(ctx, 0, floorY, ['#1c2350', '#2a3368', '#354078']);
      ctx.fillRect(0, 0, v.w, floorY);
      // Wallpaper of little stars and moons.
      for (const [sx, sy, sr] of stars) {
        ctx.fillStyle = `rgba(200,210,255,${0.1 + sr * 0.12})`;
        if (sr > 0.75) {
          ctx.beginPath();
          ctx.arc(sx, sy, 8 * k, 0.6, 3.9);
          ctx.arc(sx + 4 * k, sy - 2 * k, 6.5 * k, 3.6, 0.9, true);
          ctx.fill();
        } else {
          ctx.beginPath();
          for (let i = 0; i < 10; i++) {
            const a = (i / 10) * TAU - Math.PI / 2;
            const r = (i % 2 ? 2.6 : 6.5) * k;
            ctx.lineTo(sx + Math.cos(a) * r, sy + Math.sin(a) * r);
          }
          ctx.fill();
        }
      }
      box(ctx, 0, floorY - 22 * k, v.w, 22 * k, '#1a2046');
      planks(ctx, v, floorY, '#4c372a', '#3a2a22', 'rgba(0,0,0,0.3)');
      // Rug.
      const rugY = floorY + (v.h - floorY) * 0.62;
      oval(ctx, v.w * 0.52, rugY, v.w * 0.3, (v.h - floorY) * 0.3, '#7a3f5a');
      oval(ctx, v.w * 0.52, rugY, v.w * 0.24, (v.h - floorY) * 0.23, '#9a5672');
      oval(ctx, v.w * 0.52, rugY, v.w * 0.15, (v.h - floorY) * 0.14, '#7a3f5a');

      // Window and moon; the curtains are live.
      rr(ctx, win.x - win.w / 2 - 10 * k, win.y - 10 * k, win.w + 20 * k, win.h + 20 * k, 6, '#e6e0d0');
      ctx.fillStyle = vgrad(ctx, win.y, win.y + win.h, ['#0a1030', '#1d3160']);
      ctx.fillRect(win.x - win.w / 2, win.y, win.w, win.h);
      ctx.save();
      ctx.beginPath();
      ctx.rect(win.x - win.w / 2, win.y, win.w, win.h);
      ctx.clip();
      moon(ctx, win.x + 12 * k, win.y + win.h * 0.34, 40 * k);
      for (let i = 0; i < 12; i++) disc(ctx, win.x - win.w / 2 + rng() * win.w, win.y + rng() * win.h, 1.6 * k, 'rgba(255,255,255,0.8)');
      // A tree far away.
      poly(ctx, [win.x - win.w / 2, win.y + win.h, win.x - win.w * 0.2, win.y + win.h * 0.72, win.x + win.w * 0.1, win.y + win.h * 0.85, win.x + win.w / 2, win.y + win.h * 0.7, win.x + win.w / 2, win.y + win.h], '#0a1124');
      ctx.restore();
      box(ctx, win.x - 4 * k, win.y, 8 * k, win.h, '#e6e0d0');
      box(ctx, win.x - win.w / 2, win.y + win.h * 0.5, win.w, 8 * k, '#e6e0d0');
      // Moonlight on the floor.
      ctx.fillStyle = 'rgba(190,210,255,0.1)';
      ctx.beginPath();
      ctx.moveTo(win.x - win.w / 2, floorY);
      ctx.lineTo(win.x + win.w / 2, floorY);
      ctx.lineTo(win.x + win.w * 1.5, v.h);
      ctx.lineTo(win.x - win.w * 0.2, v.h);
      ctx.closePath();
      ctx.fill();

      // A family portrait. Of spiders.
      const px = Math.min(v.w * 0.64, bed.x0 + 110 * k);
      const py = Math.max(win.y + 20 * k, floorY - 330 * k);
      rr(ctx, px - 46 * k, py, 92 * k, 74 * k, 5, '#8a5a33');
      box(ctx, px - 38 * k, py + 8 * k, 76 * k, 58 * k, '#d9cfb4');
      for (const [ox, s] of [[-18, 1], [8, 0.8], [26, 0.55]]) {
        disc(ctx, px + ox * k, py + 44 * k, 11 * k * s, '#2a1b3d');
        ctx.strokeStyle = '#2a1b3d';
        ctx.lineWidth = 2 * k;
        for (let i = 0; i < 4; i++) {
          ctx.beginPath();
          ctx.moveTo(px + ox * k - 9 * k * s, py + 40 * k + i * 3 * k * s);
          ctx.lineTo(px + ox * k - 18 * k * s, py + 34 * k + i * 7 * k * s);
          ctx.moveTo(px + ox * k + 9 * k * s, py + 40 * k + i * 3 * k * s);
          ctx.lineTo(px + ox * k + 18 * k * s, py + 34 * k + i * 7 * k * s);
          ctx.stroke();
        }
        disc(ctx, px + ox * k - 4 * k * s, py + 42 * k, 2.2 * k * s, '#fff');
        disc(ctx, px + ox * k + 4 * k * s, py + 42 * k, 2.2 * k * s, '#fff');
      }

      // Wardrobe, one door ajar.
      rr(ctx, ward.x - ward.w / 2, ward.top, ward.w, ward.bottom - ward.top, 8, '#5b3a2a');
      rr(ctx, ward.x - ward.w / 2 - 8 * k, ward.top - 6 * k, ward.w + 16 * k, 16 * k, 5, '#6d4732');
      rr(ctx, ward.x - ward.w / 2 + 10 * k, ward.top + 22 * k, ward.w / 2 - 14 * k, ward.bottom - ward.top - 60 * k, 5, '#734a34');
      rr(ctx, ward.x + 4 * k, ward.top + 22 * k, ward.w / 2 - 14 * k, ward.bottom - ward.top - 60 * k, 5, '#0b0812');
      rr(ctx, ward.x + 24 * k, ward.top + 22 * k, ward.w / 2 - 34 * k, ward.bottom - ward.top - 60 * k, 5, '#734a34');
      disc(ctx, ward.x - 10 * k, ward.top + (ward.bottom - ward.top) * 0.5, 6 * k, '#d9c9a0');
      disc(ctx, ward.x + 34 * k, ward.top + (ward.bottom - ward.top) * 0.5, 6 * k, '#d9c9a0');
      box(ctx, ward.x - ward.w / 2 + 10 * k, ward.bottom - 8 * k, 14 * k, 14 * k, '#3a251b');
      box(ctx, ward.x + ward.w / 2 - 24 * k, ward.bottom - 8 * k, 14 * k, 14 * k, '#3a251b');

      // Bed.
      const bw = bed.x1 - bed.x0;
      rr(ctx, bed.x1 - 26 * k, bed.top - 120 * k, 40 * k, bed.bottom - bed.top + 120 * k, 14 * k, '#6d4732');
      rr(ctx, bed.x0 - 10 * k, bed.top - 20 * k, 26 * k, bed.bottom - bed.top + 20 * k, 10 * k, '#6d4732');
      box(ctx, bed.x0, bed.bottom - 36 * k, bw, 36 * k, '#07060c');
      rr(ctx, bed.x0, bed.top, bw, 70 * k, 14 * k, '#e9e4f2');
      rr(ctx, bed.x1 - 132 * k, bed.top - 30 * k, 110 * k, 44 * k, 20 * k, '#ffffff');
      // Patchwork blanket.
      ctx.save();
      ctx.beginPath();
      ctx.roundRect(bed.x0 - 4 * k, bed.top - 4 * k, bw * 0.72, bed.bottom - bed.top - 30 * k, 16 * k);
      ctx.clip();
      const patch = ['#c6504a', '#e0a13a', '#4f8f7a', '#5a6fc4', '#a05aa8'];
      const ps = 44 * k;
      for (let y = bed.top - 4 * k, r = 0; y < bed.bottom; y += ps, r++) {
        for (let x = bed.x0 - 4 * k, c = 0; x < bed.x0 + bw * 0.72; x += ps, c++) {
          box(ctx, x, y, ps, ps, patch[(r * 2 + c * 3) % patch.length]);
          box(ctx, x, y, ps, 2, 'rgba(0,0,0,0.2)');
          box(ctx, x, y, 2, ps, 'rgba(0,0,0,0.2)');
        }
      }
      ctx.restore();

      // Nightstand; the lamp is live.
      rr(ctx, stand.x - 44 * k, stand.top, 88 * k, floorY + 60 * k - stand.top, 5, '#5b3a2a');
      rr(ctx, stand.x - 36 * k, stand.top + 14 * k, 72 * k, 30 * k, 4, '#734a34');
      disc(ctx, stand.x, stand.top + 29 * k, 5 * k, '#d9c9a0');

      // Toy chest, blocks and a ball.
      const tx = teddy.x - 150 * k;
      if (tx > ward.x + ward.w / 2 - 30 * k) {
        rr(ctx, tx - 66 * k, floorY + 8 * k, 132 * k, 74 * k, 8, '#3f6fb5');
        rr(ctx, tx - 70 * k, floorY - 4 * k, 140 * k, 20 * k, 8, '#5486cf');
        disc(ctx, tx, floorY + 44 * k, 12 * k, '#f2c531');
      }
      box(ctx, teddy.x + 70 * k, floorY + 70 * k, 30 * k, 30 * k, '#e2574c');
      box(ctx, teddy.x + 104 * k, floorY + 76 * k, 26 * k, 26 * k, '#6fbf5a');
      box(ctx, teddy.x + 86 * k, floorY + 46 * k, 26 * k, 26 * k, '#f2c531');
      cobweb(ctx, 0, 0, 200 * k, 0, Math.PI / 2);
      cobweb(ctx, v.w, 0, 230 * k, Math.PI / 2, Math.PI);
      vignette(ctx, v, 0.55);
    },
    paintLive(ctx, t) {
      // Curtains breathe with the night air.
      for (const side of [-1, 1]) {
        const sway = Math.sin(t * 0.9 + side) * 9 * k;
        const x0 = win.x + (side * win.w) / 2 + side * 12 * k;
        ctx.fillStyle = '#7b5fb8';
        ctx.beginPath();
        ctx.moveTo(x0, win.y - 16 * k);
        ctx.lineTo(x0 - side * 58 * k, win.y - 16 * k);
        ctx.quadraticCurveTo(x0 - side * (70 * k + sway), win.y + win.h * 0.55, x0 - side * (40 * k + sway * 1.6), win.y + win.h + 30 * k);
        ctx.lineTo(x0 + side * 4 * k, win.y + win.h + 30 * k);
        ctx.closePath();
        ctx.fill();
        ctx.strokeStyle = 'rgba(255,255,255,0.14)';
        ctx.lineWidth = 3 * k;
        ctx.beginPath();
        ctx.moveTo(x0 - side * 26 * k, win.y - 10 * k);
        ctx.quadraticCurveTo(x0 - side * (36 * k + sway), win.y + win.h * 0.5, x0 - side * (22 * k + sway), win.y + win.h + 24 * k);
        ctx.stroke();
      }
      box(ctx, win.x - win.w / 2 - 30 * k, win.y - 24 * k, win.w + 60 * k, 9 * k, '#3a251b');
      // Somebody under the bed.
      const blink = (t * 0.29) % 1 > 0.94;
      if (!blink) {
        for (const [ox, c] of [[0.28, '#ff5a4a'], [0.62, '#ffe14a']] as [number, string][]) {
          const ex = bed.x0 + (bed.x1 - bed.x0) * ox + Math.sin(t * 0.5 + ox * 9) * 8 * k;
          const ey = bed.bottom - 18 * k;
          disc(ctx, ex, ey, 5 * k, c);
          disc(ctx, ex + 18 * k, ey, 5 * k, c);
          disc(ctx, ex, ey, 2 * k, '#07060c');
          disc(ctx, ex + 18 * k, ey, 2 * k, '#07060c');
        }
      }
      // Bedside lamp.
      box(ctx, lampPos.x - 4 * k, lampPos.y + 20 * k, 8 * k, 42 * k, '#c9a36a');
      rr(ctx, lampPos.x - 20 * k, stand.top - 8 * k, 40 * k, 8 * k, 4, '#c9a36a');
      poly(ctx, [lampPos.x - 20 * k, lampPos.y - 26 * k, lampPos.x + 20 * k, lampPos.y - 26 * k, lampPos.x + 38 * k, lampPos.y + 24 * k, lampPos.x - 38 * k, lampPos.y + 24 * k], lampOn ? '#ffd98a' : '#6a5a7a');
      if (lampOn) {
        const g = ctx.createRadialGradient(lampPos.x, lampPos.y, 6 * k, lampPos.x, lampPos.y, 170 * k);
        g.addColorStop(0, 'rgba(255,220,140,0.34)');
        g.addColorStop(1, 'rgba(255,220,140,0)');
        ctx.fillStyle = g;
        ctx.fillRect(lampPos.x - 170 * k, lampPos.y - 170 * k, 340 * k, 340 * k);
      }
      // Teddy; he squeaks when squeezed.
      const sq = Math.max(0, squeak);
      ctx.save();
      ctx.translate(teddy.x, teddy.y);
      ctx.scale(1 + sq * 0.25, 1 - sq * 0.25);
      disc(ctx, -34 * k, 2 * k, 14 * k, '#a8743f');
      disc(ctx, 34 * k, 2 * k, 14 * k, '#a8743f');
      oval(ctx, 0, 0, 34 * k, 38 * k, '#b98548');
      oval(ctx, 0, 6 * k, 20 * k, 24 * k, '#d9ab72');
      disc(ctx, -22 * k, -64 * k, 12 * k, '#a8743f');
      disc(ctx, 22 * k, -64 * k, 12 * k, '#a8743f');
      disc(ctx, 0, -46 * k, 28 * k, '#b98548');
      oval(ctx, 0, -38 * k, 13 * k, 10 * k, '#d9ab72');
      disc(ctx, -10 * k, -52 * k, 3.4 * k, '#1b1420');
      disc(ctx, 10 * k, -52 * k, 3.4 * k, '#1b1420');
      disc(ctx, 0, -41 * k, 3.6 * k, '#1b1420');
      disc(ctx, -22 * k, 34 * k, 13 * k, '#a8743f');
      disc(ctx, 22 * k, 34 * k, 13 * k, '#a8743f');
      ctx.restore();
      // Star mobile.
      if (mobile.len > 60 * k) {
        const a = Math.sin(t * 0.8) * 0.08;
        ctx.save();
        ctx.translate(mobile.x, 0);
        ctx.rotate(a);
        ctx.strokeStyle = 'rgba(230,236,255,0.6)';
        ctx.lineWidth = 2 * k;
        ctx.beginPath();
        ctx.moveTo(0, 0);
        ctx.lineTo(0, mobile.len);
        ctx.moveTo(-60 * k, mobile.len);
        ctx.lineTo(60 * k, mobile.len);
        ctx.stroke();
        const cols = ['#ffe58a', '#a8d8ff', '#ffb0c8'];
        [-60, 0, 60].forEach((ox, i) => {
          const dy = (26 + i * 10) * k;
          ctx.beginPath();
          ctx.moveTo(ox * k, mobile.len);
          ctx.lineTo(ox * k, mobile.len + dy);
          ctx.stroke();
          ctx.fillStyle = cols[i];
          ctx.beginPath();
          for (let j = 0; j < 10; j++) {
            const sa = (j / 10) * TAU - Math.PI / 2 + t * (i - 1) * 0.6;
            const r = (j % 2 ? 6 : 14) * k;
            ctx.lineTo(ox * k + Math.cos(sa) * r, mobile.len + dy + 12 * k + Math.sin(sa) * r);
          }
          ctx.fill();
        });
        ctx.restore();
      }
    },
    paintFront() {},
    step(dt) {
      squeak = Math.max(0, squeak - dt * 3);
    },
    darkness() {
      const lights: Light[] = [{ x: win.x, y: win.y + win.h * 0.6, r: 330 * k, power: 0.5 }];
      if (lampOn) lights.push({ x: lampPos.x, y: lampPos.y + 30 * k, r: 420 * k, power: 0.8 });
      return { level: lampOn ? 0.46 : 0.6, lights };
    },
    poke(id) {
      if (id === 'lamp') {
        lampOn = !lampOn;
        return 'lamp';
      }
      squeak = 1;
      return 'squeak';
    },
  };
}

// ----------------------------------------------------------------- bodega

function bodega(v: View): Scene {
  const k = scaleOf(v);
  const floorY = v.h * 0.69;
  const rng = rngFrom(44);
  const shelf = { x0: v.w * 0.04, x1: v.w * 0.33, top: Math.max(225, floorY - 330 * k), bottom: floorY + 40 * k };
  const barrels = [
    { x: v.w * 0.55, w: 150 * k, top: floorY - 118 * k, bottom: floorY + 66 * k },
    { x: v.w * 0.71, w: 128 * k, top: floorY - 70 * k, bottom: floorY + 88 * k },
  ];
  const crates = { x: v.w * 0.9, w: 150 * k, top: floorY - 150 * k, bottom: floorY + 70 * k };
  const bulb = { x: v.w * 0.45, len: Math.max(110 * k, floorY - 380 * k) };
  const pipeY = 64 * k;
  const dripX = v.w * 0.64;
  let swing = 0.1;
  let swingV = 0;
  let buzz = 0;
  let dripT = 1;

  const bulbAt = (): { x: number; y: number } => ({ x: bulb.x + Math.sin(swing) * bulb.len, y: 36 * k + Math.cos(swing) * bulb.len });

  const spots: Spot[] = [
    ...dropSpots(v, [0.2, 0.38, 0.62, 0.83], [0.3, 0.36, 0.27, 0.33]),
    { mode: 'peek', x: (shelf.x0 + shelf.x1) / 2, y: 0, dir: 'up', edge: shelf.top },
    { mode: 'peek', x: barrels[0].x, y: 0, dir: 'up', edge: barrels[0].top },
    { mode: 'peek', x: barrels[1].x, y: 0, dir: 'up', edge: barrels[1].top },
    { mode: 'peek', x: crates.x, y: 0, dir: 'up', edge: crates.top },
    ...crawlSpots(v, floorY),
  ];

  return {
    view: v,
    floorY,
    spots,
    toys: [
      { id: 'bulb', x: bulb.x, y: 36 * k + bulb.len, r: 60 * k },
      { id: 'barrel', x: barrels[0].x, y: (barrels[0].top + barrels[0].bottom) / 2 + 20 * k, r: 60 * k },
    ],
    paintStatic(ctx) {
      box(ctx, 0, 0, v.w, floorY, '#3f3136');
      // Bricks.
      const bh = 26 * k;
      const bw = 62 * k;
      for (let y = 0, row = 0; y < floorY; y += bh, row++) {
        for (let x = -(row % 2) * bw * 0.5; x < v.w; x += bw) {
          const tone = rng();
          ctx.fillStyle = tone > 0.8 ? '#5a4148' : tone > 0.4 ? '#4c383d' : '#44323a';
          ctx.fillRect(x + 1.5, y + 1.5, bw - 3, bh - 3);
        }
      }
      ctx.fillStyle = vgrad(ctx, 0, floorY, ['rgba(8,6,14,0.6)', 'rgba(8,6,14,0.1)', 'rgba(8,6,14,0.35)']);
      ctx.fillRect(0, 0, v.w, floorY);
      // Damp patches.
      for (let i = 0; i < 5; i++) oval(ctx, rng() * v.w, floorY - rng() * 120 * k, (40 + rng() * 60) * k, (20 + rng() * 30) * k, 'rgba(40,70,60,0.2)');
      // Stone floor.
      ctx.fillStyle = vgrad(ctx, floorY, v.h, ['#2c2b33', '#3d3c46']);
      ctx.fillRect(0, floorY, v.w, v.h - floorY);
      ctx.strokeStyle = 'rgba(0,0,0,0.32)';
      ctx.lineWidth = 2.5;
      const depth = v.h - floorY;
      for (let r = 1; r < 4; r++) {
        const y = floorY + depth * Math.pow(r / 4, 1.6);
        ctx.beginPath();
        ctx.moveTo(0, y);
        ctx.lineTo(v.w, y);
        ctx.stroke();
        for (let x = (r % 2) * 90 * k; x < v.w; x += 180 * k * (0.7 + r * 0.2)) {
          ctx.beginPath();
          ctx.moveTo(x, y);
          ctx.lineTo(x + (x - v.w / 2) * 0.12, floorY + depth * Math.pow((r + 1) / 4, 1.6));
          ctx.stroke();
        }
      }
      box(ctx, 0, floorY, v.w, 7, 'rgba(0,0,0,0.4)');

      // A barred window high up, with a ray of moonlight.
      const wx = v.w * 0.8;
      const wy = Math.max(86 * k, floorY - 430 * k);
      rr(ctx, wx - 58 * k, wy - 8 * k, 116 * k, 76 * k, 8, '#221a20');
      ctx.fillStyle = vgrad(ctx, wy, wy + 60 * k, ['#12204a', '#2a4472']);
      ctx.fillRect(wx - 50 * k, wy, 100 * k, 60 * k);
      disc(ctx, wx + 18 * k, wy + 24 * k, 14 * k, '#f2f0d8');
      for (let i = 0; i < 4; i++) box(ctx, wx - 40 * k + i * 26 * k, wy, 6 * k, 60 * k, '#15121a');
      ctx.fillStyle = 'rgba(180,205,255,0.07)';
      ctx.beginPath();
      ctx.moveTo(wx - 50 * k, wy + 60 * k);
      ctx.lineTo(wx + 50 * k, wy + 60 * k);
      ctx.lineTo(wx - 60 * k, v.h);
      ctx.lineTo(wx - 330 * k, v.h);
      ctx.closePath();
      ctx.fill();

      // Beams and a pipe.
      box(ctx, 0, 0, v.w, 30 * k, '#2a1c14');
      for (let x = v.w * 0.1; x < v.w; x += v.w * 0.27) box(ctx, x, 0, 26 * k, 42 * k, '#38261a');
      box(ctx, 0, pipeY, v.w, 14 * k, '#5a6a62');
      box(ctx, 0, pipeY, v.w, 4 * k, '#7d9086');
      for (const px of [v.w * 0.22, dripX, v.w * 0.92]) rr(ctx, px - 9 * k, pipeY - 4 * k, 18 * k, 22 * k, 3, '#46544d');
      disc(ctx, v.w * 0.22, pipeY + 7 * k, 14 * k, '#a8322a');
      disc(ctx, v.w * 0.22, pipeY + 7 * k, 6 * k, '#5a1c18');
      // Puddle under the leak.
      oval(ctx, dripX, floorY + depth * 0.18, 46 * k, 9 * k, 'rgba(120,170,200,0.3)');

      // Shelves with jars.
      const sw = shelf.x1 - shelf.x0;
      box(ctx, shelf.x0, shelf.top, 12 * k, shelf.bottom - shelf.top, '#3a281c');
      box(ctx, shelf.x1 - 12 * k, shelf.top, 12 * k, shelf.bottom - shelf.top, '#3a281c');
      const jarCols = ['#7bbf5a', '#d9574a', '#e8c23a', '#c98bd8', '#6fb8d0'];
      const boards = 3;
      for (let b = 0; b <= boards; b++) {
        const y = shelf.top + (b * (shelf.bottom - shelf.top - 30 * k)) / boards;
        rr(ctx, shelf.x0 - 6 * k, y, sw + 12 * k, 13 * k, 3, '#5b4030');
        if (b === 0) continue;
        let x = shelf.x0 + 22 * k;
        while (x < shelf.x1 - 50 * k) {
          const kind = rng();
          const w = (26 + rng() * 14) * k;
          const h = (34 + rng() * 26) * k;
          if (kind < 0.6) {
            const c = jarCols[Math.floor(rng() * jarCols.length)];
            ctx.globalAlpha = 0.85;
            rr(ctx, x, y - h, w, h, 6 * k, c);
            ctx.globalAlpha = 1;
            rr(ctx, x + 3 * k, y - h - 6 * k, w - 6 * k, 8 * k, 3, '#c9c2ae');
            box(ctx, x + 4 * k, y - h + 6 * k, 4 * k, h - 14 * k, 'rgba(255,255,255,0.35)');
          } else if (kind < 0.85) {
            rr(ctx, x, y - h * 0.8, w * 1.3, h * 0.8, 3, '#8a6a48');
            box(ctx, x, y - h * 0.5, w * 1.3, 4 * k, '#5b4030');
          } else {
            rr(ctx, x, y - h * 0.7, w, h * 0.7, 4, '#4f6fa0');
            box(ctx, x, y - h * 0.7, w, 6 * k, '#c9d2e0');
          }
          x += w * 1.4 + 6 * k;
        }
      }

      // Barrels.
      for (const b of barrels) {
        const h = b.bottom - b.top;
        ctx.fillStyle = '#7a4f2c';
        ctx.beginPath();
        ctx.moveTo(b.x - b.w * 0.42, b.top);
        ctx.quadraticCurveTo(b.x - b.w * 0.62, b.top + h / 2, b.x - b.w * 0.42, b.bottom);
        ctx.lineTo(b.x + b.w * 0.42, b.bottom);
        ctx.quadraticCurveTo(b.x + b.w * 0.62, b.top + h / 2, b.x + b.w * 0.42, b.top);
        ctx.closePath();
        ctx.fill();
        ctx.save();
        ctx.clip();
        for (let i = -3; i <= 3; i++) box(ctx, b.x + i * b.w * 0.15 - 1.5, b.top, 3, h, 'rgba(0,0,0,0.22)');
        box(ctx, b.x - b.w, b.top + h * 0.14, b.w * 2, 12 * k, '#3a3f4b');
        box(ctx, b.x - b.w, b.top + h * 0.76, b.w * 2, 12 * k, '#3a3f4b');
        box(ctx, b.x - b.w * 0.5, b.top, b.w * 0.22, h, 'rgba(255,255,255,0.08)');
        ctx.restore();
        oval(ctx, b.x, b.top, b.w * 0.42, 9 * k, '#93643a');
      }

      // Crates, a sack and a mop.
      const cw = crates.w;
      const ch = (crates.bottom - crates.top) / 2;
      for (let i = 0; i < 2; i++) {
        const x = crates.x - cw / 2 + (i ? 0 : 14 * k);
        const y = crates.top + i * ch;
        const w = cw - (i ? 0 : 28 * k);
        rr(ctx, x, y, w, ch, 4, '#8a6a48');
        ctx.strokeStyle = '#5b4030';
        ctx.lineWidth = 7 * k;
        ctx.strokeRect(x + 5 * k, y + 5 * k, w - 10 * k, ch - 10 * k);
        ctx.beginPath();
        ctx.moveTo(x + 8 * k, y + 8 * k);
        ctx.lineTo(x + w - 8 * k, y + ch - 8 * k);
        ctx.stroke();
      }
      const sx = v.w * 0.4;
      ctx.fillStyle = '#a8946a';
      ctx.beginPath();
      ctx.moveTo(sx - 50 * k, floorY + 60 * k);
      ctx.quadraticCurveTo(sx - 64 * k, floorY - 30 * k, sx - 14 * k, floorY - 40 * k);
      ctx.lineTo(sx - 22 * k, floorY - 62 * k);
      ctx.lineTo(sx + 20 * k, floorY - 62 * k);
      ctx.lineTo(sx + 14 * k, floorY - 40 * k);
      ctx.quadraticCurveTo(sx + 62 * k, floorY - 30 * k, sx + 50 * k, floorY + 60 * k);
      ctx.closePath();
      ctx.fill();
      box(ctx, sx - 18 * k, floorY - 44 * k, 36 * k, 6 * k, '#6d5a3a');
      cobweb(ctx, 0, 30 * k, 260 * k, 0, Math.PI / 2, 0.42);
      cobweb(ctx, v.w, 30 * k, 240 * k, Math.PI / 2, Math.PI, 0.42);
      cobweb(ctx, shelf.x1, shelf.top + 13 * k, 90 * k, Math.PI / 2, Math.PI, 0.3);
      vignette(ctx, v, 0.6);
    },
    paintLive(ctx, t) {
      // A rat watching from a hole.
      const hx = v.w * 0.47;
      const hy = floorY - 16 * k;
      ctx.fillStyle = '#0a0810';
      ctx.beginPath();
      ctx.arc(hx, hy + 16 * k, 24 * k, Math.PI, 0);
      ctx.fill();
      if ((t * 0.23) % 1 < 0.9) {
        disc(ctx, hx - 7 * k, hy + 4 * k, 3 * k, '#ff8a7a');
        disc(ctx, hx + 7 * k, hy + 4 * k, 3 * k, '#ff8a7a');
      }
      // Drip.
      const d = dripT % 3.4;
      if (d < 0.75) oval(ctx, dripX, pipeY + 18 * k + d * d * (floorY - pipeY) * 1.9, 3 * k, 6 * k, '#bfe6ff');
      // The bulb on its cord.
      const b = bulbAt();
      const lit = buzz > 0 ? (Math.sin(t * 60) > 0 ? 1 : 0.3) : 1;
      ctx.strokeStyle = '#15121a';
      ctx.lineWidth = 3 * k;
      ctx.beginPath();
      ctx.moveTo(bulb.x, 30 * k);
      ctx.lineTo(b.x, b.y - 18 * k);
      ctx.stroke();
      ctx.save();
      ctx.translate(b.x, b.y);
      ctx.rotate(-swing);
      rr(ctx, -9 * k, -24 * k, 18 * k, 16 * k, 3, '#3a3f4b');
      disc(ctx, 0, 0, 15 * k, `rgba(255,236,160,${0.5 + 0.5 * lit})`);
      ctx.restore();
      const g = ctx.createRadialGradient(b.x, b.y, 4 * k, b.x, b.y, 150 * k);
      g.addColorStop(0, `rgba(255,225,140,${0.4 * lit})`);
      g.addColorStop(1, 'rgba(255,225,140,0)');
      ctx.fillStyle = g;
      ctx.fillRect(b.x - 150 * k, b.y - 150 * k, 300 * k, 300 * k);
    },
    paintFront(ctx, t) {
      // Dust in the light.
      const b = bulbAt();
      ctx.fillStyle = 'rgba(255,240,200,0.3)';
      for (let i = 0; i < 14; i++) {
        const a = i * 2.4 + t * 0.1;
        const r = (40 + ((i * 53) % 190)) * k;
        const x = b.x + Math.cos(a) * r;
        const y = b.y + 60 * k + Math.sin(a * 1.3 + t * 0.2) * r * 0.7;
        ctx.fillRect(x, y, 2.4 * k, 2.4 * k);
      }
    },
    step(dt, _t, emit) {
      swingV += -swing * 5.5 * dt;
      swingV *= Math.pow(0.82, dt);
      swing += swingV * dt;
      // Never quite still: a draught keeps it moving.
      if (Math.abs(swing) < 0.05 && Math.abs(swingV) < 0.08) swingV += (Math.random() - 0.5) * 0.5;
      buzz = Math.max(0, buzz - dt);
      const before = dripT % 3.4;
      dripT += dt;
      if (before < 0.75 && dripT % 3.4 >= 0.75) emit('drip', dripX);
    },
    darkness(t) {
      const b = bulbAt();
      const lit = buzz > 0 ? (Math.sin(t * 60) > 0 ? 1 : 0.25) : 1;
      return {
        level: 0.58,
        lights: [
          { x: b.x, y: b.y + 110 * k, r: 520 * k, power: 0.92 * lit },
          { x: v.w * 0.72, y: floorY - 40 * k, r: 260 * k, power: 0.3 },
        ],
      };
    },
    poke(id) {
      if (id === 'bulb') {
        swingV += (swing >= 0 ? 1 : -1) * 1.9;
        buzz = 0.5;
        return 'buzz';
      }
      return 'bonk';
    },
  };
}

// ----------------------------------------------------------------- bosque

function bosque(v: View): Scene {
  const k = scaleOf(v);
  const floorY = v.h * 0.69;
  const rng = rngFrom(55);
  const moonPos = { x: v.w * 0.68, y: v.h * 0.2, r: 78 * k };
  const trunkL = { x: v.w * 0.05, w: 130 * k };
  const trunkR = { x: v.w * 0.95, w: 120 * k };
  const bushL = { x: v.w * 0.21, w: 220 * k, top: floorY - 62 * k };
  const bushR = { x: v.w * 0.8, w: 200 * k, top: floorY - 50 * k };
  const log = { x: v.w * 0.5, w: 250 * k, top: floorY + 4 * k };
  const owl = { x: trunkR.x - 6 * k, y: Math.max(v.h * 0.3, floorY - 330 * k) };
  const shrooms = [v.w * 0.36, v.w * 0.63, v.w * 0.67].map((x, i) => ({ x, y: floorY + (v.h - floorY) * (0.5 + i * 0.12), s: (1 - i * 0.2) * k }));
  const stars: [number, number, number][] = [];
  for (let i = 0; i < 90; i++) stars.push([rng() * v.w, rng() * floorY * 0.85, rng()]);
  const flies = Array.from({ length: 12 }, () => ({ x: rng() * v.w, y: floorY - 160 * k + rng() * 300 * k, p: rng() * TAU }));
  let flash = 0;
  let nextFlash = 7 + Math.random() * 6;
  let bolt: number[] = [];
  let thunderIn = -1;
  let bat = -1;
  let nextBat = 4;
  let glowT = -9;
  let hootT = -9;
  let nextCricket = 2;

  const spots: Spot[] = [
    ...dropSpots(v, [0.2, 0.4, 0.6, 0.8], [0.3, 0.37, 0.28, 0.35]),
    { mode: 'peek', x: bushL.x, y: 0, dir: 'up', edge: bushL.top },
    { mode: 'peek', x: bushR.x, y: 0, dir: 'up', edge: bushR.top },
    { mode: 'peek', x: log.x, y: 0, dir: 'up', edge: log.top },
    { mode: 'peek', x: 0, y: floorY - 190 * k, dir: 'right', edge: trunkL.x + trunkL.w / 2 },
    { mode: 'peek', x: 0, y: floorY - 230 * k, dir: 'left', edge: trunkR.x - trunkR.w / 2 },
    ...crawlSpots(v, floorY),
  ];

  function pines(ctx: Ctx, baseY: number, h: number, step: number, color: string, seed: number): void {
    const r = rngFrom(seed);
    ctx.fillStyle = color;
    for (let x = -step; x < v.w + step; x += step * (0.6 + r() * 0.6)) {
      const th = h * (0.6 + r() * 0.6);
      const tw = th * 0.42;
      ctx.beginPath();
      ctx.moveTo(x, baseY - th);
      for (let i = 1; i <= 4; i++) {
        ctx.lineTo(x + (tw * i) / 4, baseY - th + (th * i) / 4);
        ctx.lineTo(x + (tw * i) / 4 - tw * 0.12, baseY - th + (th * i) / 4);
      }
      ctx.lineTo(x - tw + tw * 0.12, baseY);
      for (let i = 4; i >= 1; i--) {
        ctx.lineTo(x - (tw * i) / 4 + tw * 0.12, baseY - th + (th * i) / 4);
        ctx.lineTo(x - (tw * i) / 4, baseY - th + (th * i) / 4);
      }
      ctx.closePath();
      ctx.fill();
    }
    ctx.fillRect(0, baseY - 2, v.w, 6);
  }

  function trunk(ctx: Ctx, tr: { x: number; w: number }, lean: number): void {
    ctx.fillStyle = '#0d1116';
    ctx.beginPath();
    ctx.moveTo(tr.x - tr.w * 0.62, v.h);
    ctx.quadraticCurveTo(tr.x - tr.w * 0.44, floorY, tr.x - tr.w / 2 + lean * 10 * k, 0);
    ctx.lineTo(tr.x + tr.w / 2 + lean * 10 * k, 0);
    ctx.quadraticCurveTo(tr.x + tr.w * 0.44, floorY, tr.x + tr.w * 0.66, v.h);
    ctx.closePath();
    ctx.fill();
    ctx.strokeStyle = 'rgba(70,95,110,0.4)';
    ctx.lineWidth = 3 * k;
    for (let i = 0; i < 6; i++) {
      const bx = tr.x - tr.w * 0.35 + rng() * tr.w * 0.7;
      const y0 = rng() * v.h * 0.8;
      ctx.beginPath();
      ctx.moveTo(bx, y0);
      ctx.quadraticCurveTo(bx + 8 * k, y0 + 40 * k, bx - 4 * k, y0 + 90 * k);
      ctx.stroke();
    }
    // Moonlit edge.
    ctx.strokeStyle = 'rgba(150,185,215,0.3)';
    ctx.lineWidth = 4 * k;
    ctx.beginPath();
    const ex = tr.x + (lean > 0 ? 1 : -1) * tr.w * 0.46;
    ctx.moveTo(ex + lean * 8 * k, 0);
    ctx.quadraticCurveTo(ex - lean * 4 * k, floorY, ex + lean * 16 * k, v.h);
    ctx.stroke();
  }

  function bush(ctx: Ctx, b: { x: number; w: number; top: number }, color: string): void {
    ctx.fillStyle = color;
    const h = floorY + 40 * k - b.top;
    for (let i = 0; i < 7; i++) {
      const f = i / 6;
      const r = b.w * (0.2 + 0.08 * Math.sin(i * 2.1));
      ctx.beginPath();
      ctx.arc(b.x - b.w / 2 + f * b.w, b.top + r * 0.9 + (Math.abs(f - 0.5) * h) / 2.2, r, 0, TAU);
      ctx.fill();
    }
    ctx.fillRect(b.x - b.w / 2, b.top + h * 0.5, b.w, h * 0.7);
  }

  return {
    view: v,
    floorY,
    spots,
    toys: [
      { id: 'owl', x: owl.x, y: owl.y, r: 52 * k },
      ...shrooms.map((s, i) => ({ id: `shroom${i}`, x: s.x, y: s.y - 20 * s.s, r: 46 * k })),
    ],
    paintStatic(ctx) {
      ctx.fillStyle = vgrad(ctx, 0, floorY, ['#070b22', '#12224a', '#2a4c66', '#4a6f78']);
      ctx.fillRect(0, 0, v.w, floorY + 10);
      for (const [sx, sy, sr] of stars) disc(ctx, sx, sy, (0.8 + sr * 1.6) * k, `rgba(255,255,255,${0.35 + sr * 0.5})`);
      moon(ctx, moonPos.x, moonPos.y, moonPos.r);
      pines(ctx, floorY - 26 * k, 150 * k, 70 * k, '#1c3a4e', 3);
      pines(ctx, floorY, 230 * k, 110 * k, '#102536', 8);
      // Ground and a path.
      ctx.fillStyle = vgrad(ctx, floorY, v.h, ['#0e1e20', '#16302a']);
      ctx.fillRect(0, floorY, v.w, v.h - floorY);
      ctx.fillStyle = 'rgba(110,120,100,0.22)';
      ctx.beginPath();
      ctx.moveTo(v.w * 0.42, floorY);
      ctx.quadraticCurveTo(v.w * 0.62, floorY + (v.h - floorY) * 0.4, v.w * 0.3, v.h);
      ctx.lineTo(v.w * 0.74, v.h);
      ctx.quadraticCurveTo(v.w * 0.78, floorY + (v.h - floorY) * 0.4, v.w * 0.5, floorY);
      ctx.closePath();
      ctx.fill();
      // Grass.
      ctx.strokeStyle = '#27503c';
      ctx.lineWidth = 3 * k;
      ctx.lineCap = 'round';
      for (let i = 0; i < 90; i++) {
        const gx = rng() * v.w;
        const gy = floorY + rng() * (v.h - floorY);
        const gh = (10 + rng() * 16) * k * (0.6 + (gy - floorY) / (v.h - floorY));
        ctx.beginPath();
        ctx.moveTo(gx, gy);
        ctx.quadraticCurveTo(gx + (rng() - 0.5) * 12 * k, gy - gh * 0.6, gx + (rng() - 0.5) * 18 * k, gy - gh);
        ctx.stroke();
      }
      // A thinner tree in the middle distance.
      const mx = v.w * 0.33;
      poly(ctx, [mx - 26 * k, floorY + 20 * k, mx - 14 * k, 0, mx + 16 * k, 0, mx + 30 * k, floorY + 20 * k], '#0d1a24');
      ctx.strokeStyle = '#0d1a24';
      ctx.lineWidth = 12 * k;
      ctx.beginPath();
      ctx.moveTo(mx + 8 * k, floorY * 0.42);
      ctx.quadraticCurveTo(mx + 90 * k, floorY * 0.34, mx + 150 * k, floorY * 0.2);
      ctx.moveTo(mx - 6 * k, floorY * 0.58);
      ctx.quadraticCurveTo(mx - 70 * k, floorY * 0.5, mx - 110 * k, floorY * 0.36);
      ctx.stroke();

      bush(ctx, bushL, '#11261f');
      bush(ctx, bushR, '#0f221c');
      // Fallen log.
      rr(ctx, log.x - log.w / 2, log.top, log.w, 66 * k, 30 * k, '#2c2019');
      oval(ctx, log.x + log.w / 2 - 16 * k, log.top + 33 * k, 18 * k, 33 * k, '#4a382b');
      oval(ctx, log.x + log.w / 2 - 16 * k, log.top + 33 * k, 10 * k, 20 * k, '#35271e');
      box(ctx, log.x - log.w * 0.3, log.top + 6 * k, log.w * 0.5, 4 * k, 'rgba(150,185,215,0.22)');
      oval(ctx, log.x - log.w * 0.2, log.top + 4 * k, 34 * k, 9 * k, '#1f4a36');

      // Big trunks at the sides, with branches over the top.
      trunk(ctx, trunkL, 1);
      trunk(ctx, trunkR, -1);
      ctx.strokeStyle = '#0d1116';
      ctx.lineCap = 'round';
      const limb = (x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, w: number): void => {
        ctx.lineWidth = w;
        ctx.beginPath();
        ctx.moveTo(x0, y0);
        ctx.quadraticCurveTo(x1, y1, x2, y2);
        ctx.stroke();
      };
      limb(trunkL.x, v.h * 0.16, v.w * 0.22, v.h * 0.02, v.w * 0.46, v.h * 0.07, 34 * k);
      limb(v.w * 0.3, v.h * 0.05, v.w * 0.36, v.h * 0.13, v.w * 0.42, v.h * 0.16, 12 * k);
      limb(trunkR.x, v.h * 0.12, v.w * 0.8, v.h * 0.01, v.w * 0.56, v.h * 0.05, 30 * k);
      limb(v.w * 0.74, v.h * 0.03, v.w * 0.7, v.h * 0.1, v.w * 0.63, v.h * 0.12, 10 * k);
      limb(trunkL.x, v.h * 0.34, v.w * 0.13, v.h * 0.26, v.w * 0.19, v.h * 0.27, 18 * k);
      // Leaves hanging from the canopy.
      ctx.fillStyle = '#0b1612';
      for (let i = 0; i < 26; i++) {
        const lx = rng() * v.w;
        const ly = rng() * v.h * 0.08;
        ctx.beginPath();
        ctx.ellipse(lx, ly, (40 + rng() * 50) * k, (18 + rng() * 22) * k, rng() * 0.6 - 0.3, 0, TAU);
        ctx.fill();
      }
      // The owl's hollow.
      oval(ctx, owl.x, owl.y, 36 * k, 46 * k, '#05070a');
      // Mushroom stems; the caps glow live.
      for (const s of shrooms) rr(ctx, s.x - 7 * s.s, s.y - 26 * s.s, 14 * s.s, 30 * s.s, 5, '#d9d2c0');
      cobweb(ctx, trunkL.x + trunkL.w * 0.5, v.h * 0.16, 150 * k, -0.2, 1.3, 0.3);
      vignette(ctx, v, 0.5);
    },
    paintLive(ctx, t) {
      // Twinkle.
      for (let i = 0; i < stars.length; i += 6) {
        const [sx, sy] = stars[i];
        const a = 0.5 + 0.5 * Math.sin(t * 2.2 + i);
        disc(ctx, sx, sy, 2.6 * k * a, `rgba(255,255,255,${0.7 * a})`);
      }
      // Clouds crossing the moon.
      for (let i = 0; i < 2; i++) {
        const cx = ((t * (9 + i * 5) + i * v.w * 0.6) % (v.w + 500 * k)) - 250 * k;
        const cy = moonPos.y + (i ? 46 : -20) * k;
        ctx.fillStyle = 'rgba(16,24,48,0.6)';
        for (const [ox, oy, r] of [[-70, 6, 40], [-20, -10, 54], [40, 0, 46], [90, 10, 32]]) {
          ctx.beginPath();
          ctx.arc(cx + ox * k, cy + oy * k, r * k, 0, TAU);
          ctx.fill();
        }
      }
      // Lightning.
      if (flash > 0.5 && bolt.length) {
        ctx.strokeStyle = 'rgba(235,240,255,0.95)';
        ctx.lineWidth = 4 * k;
        ctx.lineJoin = 'miter';
        ctx.beginPath();
        ctx.moveTo(bolt[0], bolt[1]);
        for (let i = 2; i < bolt.length; i += 2) ctx.lineTo(bolt[i], bolt[i + 1]);
        ctx.stroke();
      }
      // A bat across the sky.
      if (bat >= 0) {
        const bx = v.w * 1.1 - bat * v.w * 1.2;
        const by = v.h * 0.18 + Math.sin(bat * 9) * 40 * k;
        const flap = Math.sin(t * 22) * 0.6;
        ctx.fillStyle = '#05070c';
        for (const side of [-1, 1]) {
          ctx.beginPath();
          ctx.moveTo(bx, by);
          ctx.quadraticCurveTo(bx + side * 16 * k, by - 18 * k * (0.4 + flap), bx + side * 34 * k, by - 6 * k * flap * 2);
          ctx.quadraticCurveTo(bx + side * 20 * k, by + 4 * k, bx, by + 6 * k);
          ctx.fill();
        }
        disc(ctx, bx, by, 6 * k, '#05070c');
      }
      // The owl in its hollow.
      const hoot = Math.max(0, 1 - (t - hootT) / 0.9);
      const oy = owl.y + 8 * k - hoot * 6 * k;
      oval(ctx, owl.x, oy, 24 * k, 30 * k, '#4a3b2c');
      oval(ctx, owl.x, oy + 8 * k, 15 * k, 18 * k, '#75603f');
      const closed = (t * 0.21) % 1 > 0.93 && hoot === 0;
      for (const side of [-1, 1]) {
        disc(ctx, owl.x + side * 10 * k, oy - 9 * k, 9.5 * k, closed ? '#4a3b2c' : '#ffd84a');
        if (!closed) disc(ctx, owl.x + side * 10 * k + Math.sin(t * 0.7) * 2 * k, oy - 9 * k, (4 + hoot * 2) * k, '#0a0a0a');
        poly(ctx, [owl.x + side * 6 * k, oy - 28 * k, owl.x + side * 20 * k, oy - 36 * k, owl.x + side * 18 * k, oy - 22 * k], '#4a3b2c');
      }
      poly(ctx, [owl.x - 4 * k, oy - 2 * k, owl.x + 4 * k, oy - 2 * k, owl.x, oy + 6 * k], '#e8a03a');
      // Mushroom caps.
      const pulse = Math.max(0, 1 - (t - glowT) / 1.4);
      for (const s of shrooms) {
        const glowA = 0.25 + 0.12 * Math.sin(t * 1.6 + s.x) + pulse * 0.5;
        const g = ctx.createRadialGradient(s.x, s.y - 30 * s.s, 2, s.x, s.y - 30 * s.s, 70 * s.s);
        g.addColorStop(0, `rgba(120,255,210,${glowA})`);
        g.addColorStop(1, 'rgba(120,255,210,0)');
        ctx.fillStyle = g;
        ctx.fillRect(s.x - 70 * s.s, s.y - 100 * s.s, 140 * s.s, 140 * s.s);
        ctx.fillStyle = '#d6402f';
        ctx.beginPath();
        ctx.ellipse(s.x, s.y - 26 * s.s, 26 * s.s, 20 * s.s, 0, Math.PI, 0);
        ctx.fill();
        disc(ctx, s.x - 10 * s.s, s.y - 36 * s.s, 4 * s.s, '#fff3e0');
        disc(ctx, s.x + 8 * s.s, s.y - 32 * s.s, 3.4 * s.s, '#fff3e0');
        disc(ctx, s.x + 1 * s.s, s.y - 42 * s.s, 3 * s.s, '#fff3e0');
      }
    },
    paintFront(ctx, t) {
      // Fog along the ground.
      for (let i = 0; i < 3; i++) {
        const fy = floorY + (v.h - floorY) * (0.1 + i * 0.3);
        const drift = ((t * (14 + i * 7)) % (v.w * 1.5)) - v.w * 0.25;
        const g = ctx.createLinearGradient(0, fy - 50 * k, 0, fy + 50 * k);
        g.addColorStop(0, 'rgba(170,200,215,0)');
        g.addColorStop(0.5, `rgba(170,200,215,${0.1 + i * 0.02})`);
        g.addColorStop(1, 'rgba(170,200,215,0)');
        ctx.fillStyle = g;
        ctx.beginPath();
        ctx.ellipse(drift, fy, v.w * 0.6, 50 * k, 0, 0, TAU);
        ctx.ellipse(drift + v.w * 0.9, fy + 12 * k, v.w * 0.5, 44 * k, 0, 0, TAU);
        ctx.fill();
      }
      // Fireflies.
      for (const f of flies) {
        const fx = f.x + Math.sin(t * 0.5 + f.p) * 60 * k;
        const fy = f.y + Math.cos(t * 0.37 + f.p * 2) * 40 * k;
        const a = 0.5 + 0.5 * Math.sin(t * 2.4 + f.p * 5);
        if (a < 0.2) continue;
        const g = ctx.createRadialGradient(fx, fy, 0, fx, fy, 16 * k);
        g.addColorStop(0, `rgba(230,255,140,${a})`);
        g.addColorStop(1, 'rgba(230,255,140,0)');
        ctx.fillStyle = g;
        ctx.fillRect(fx - 16 * k, fy - 16 * k, 32 * k, 32 * k);
      }
      if (flash > 0) {
        ctx.fillStyle = `rgba(215,228,255,${flash * 0.22})`;
        ctx.fillRect(0, 0, v.w, v.h);
      }
    },
    step(dt, _t, emit) {
      nextFlash -= dt;
      if (nextFlash <= 0) {
        nextFlash = 13 + Math.random() * 12;
        flash = 1;
        thunderIn = 0.35 + Math.random() * 0.5;
        bolt = [];
        let bx = v.w * (0.2 + Math.random() * 0.6);
        let by = 0;
        while (by < floorY * 0.8) {
          bolt.push(bx, by);
          bx += (Math.random() - 0.5) * 90 * k;
          by += (30 + Math.random() * 50) * k;
        }
      }
      // Two quick flickers, then dark again.
      flash = Math.max(0, flash - dt * 2.4);
      if (thunderIn >= 0) {
        thunderIn -= dt;
        if (thunderIn < 0) emit('thunder', v.w / 2);
      }
      nextBat -= dt;
      if (bat < 0 && nextBat <= 0) {
        bat = 0;
        emit('bat', v.w * 0.8);
      }
      if (bat >= 0) {
        bat += dt / 4.5;
        if (bat > 1) {
          bat = -1;
          nextBat = 9 + Math.random() * 10;
        }
      }
      nextCricket -= dt;
      if (nextCricket <= 0) {
        nextCricket = 2.5 + Math.random() * 4;
        emit('cricket', Math.random() * v.w);
      }
    },
    darkness() {
      const f = flash > 0.5 ? 1 : flash * 1.4;
      return {
        level: 0.42 * (1 - Math.min(1, f)),
        lights: [
          { x: moonPos.x, y: moonPos.y, r: 300 * k, power: 0.7 },
          { x: v.w * 0.5, y: floorY + 60 * k, r: 520 * k, power: 0.5 },
        ],
      };
    },
    poke(id, t) {
      if (id === 'owl') {
        hootT = t;
        return 'hoot';
      }
      glowT = t;
      return 'chime';
    },
  };
}

const BUILDERS = [colegio, cocina, dormitorio, bodega, bosque];

export function buildScene(level: number, view: View): Scene {
  const build = BUILDERS[level % BUILDERS.length];
  // A tall frame looks at the middle of a wider room, so the furniture keeps
  // a size that suits the spiders.
  const wide = Math.max(view.w, view.h * 0.8);
  if (wide < view.w + 2) return build(view);
  const room = build({ w: wide, h: view.h });
  const dx = (wide - view.w) / 2;
  const inside = (x: number, margin: number): boolean => x > margin && x < view.w - margin;
  const spots: Spot[] = [];
  for (const sp of room.spots) {
    if (sp.mode === 'peek' && sp.dir !== 'up' && sp.edge !== undefined) {
      const edge = sp.edge - dx;
      if (inside(edge + (sp.dir === 'left' ? -70 : 70), 60)) spots.push({ ...sp, edge });
    } else if (sp.mode === 'crawl') {
      spots.push({ ...sp, x: Math.max(190, Math.min(view.w - 190, sp.x - dx)) });
    } else if (inside(sp.x - dx, 110)) {
      spots.push({ ...sp, x: sp.x - dx });
    }
  }
  // The wall is high here: room for two more threads.
  spots.push({ mode: 'drop', x: view.w * 0.3, y: view.h * 0.5 }, { mode: 'drop', x: view.w * 0.72, y: view.h * 0.45 });
  const shifted = (paint: (ctx: Ctx, t: number) => void) => (ctx: Ctx, t: number): void => {
    ctx.save();
    ctx.translate(-dx, 0);
    paint(ctx, t);
    ctx.restore();
  };
  return {
    view,
    floorY: room.floorY,
    spots,
    toys: room.toys.map((t) => ({ ...t, x: t.x - dx })).filter((t) => inside(t.x, 10)),
    paintStatic: (ctx) => shifted((c) => room.paintStatic(c))(ctx, 0),
    paintLive: shifted((c, t) => room.paintLive(c, t)),
    paintFront: shifted((c, t) => room.paintFront(c, t)),
    step: (dt, t, emit) => room.step(dt, t, (snd, x) => emit(snd, x - dx)),
    darkness: (t) => {
      const d = room.darkness(t);
      return { level: d.level, lights: d.lights.map((l) => ({ ...l, x: l.x - dx })) };
    },
    poke: (id, t) => room.poke(id, t),
  };
}