Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion carnival/booths/jedi4ever/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ above it.
and the dashboard stay bright on top.
- **The snowman**: now and then a small snowman in a red scarf stands on the
snowbank and waves a twig arm as the van comes by (`snowman.ts`).
- **The robin**: every nine seconds a small robin hops along the dashboard's
rim at the bottom of the scene, one way and then back the other, flicking its
tail with each hop. Like the van it keeps its red breast in the noir street.
When the pointer comes near it turns its head to look at it, cocked up
towards it (`robin.ts`). It never touches the run, so the game plays the same.
- **Snow**: it falls and streaks past the camera faster as you speed up.
- **Driving**: steer with the pointer and the van follows it across the road. On
touch, a tap on the left or right half steers that way. Speed builds slowly
Expand All @@ -42,4 +47,5 @@ the run in progress, who is steering and the best score, for one visit.
`autopilot.ts` the `autopilotTarget`, tested in `game.test.ts`.
- `view.ts` is the chase camera (`project`, `bend`); `sky.ts`, `street.ts`,
`snow.ts` and `vehicles.ts` draw the scene, `lighting.ts` its light and shadow;
`dashboard.ts` the dials.
`dashboard.ts` the dials, and `robin.ts` the robin (`robinAt`,
`robinLook`, `drawRobin`), tested in `robin.test.ts`.
6 changes: 5 additions & 1 deletion carnival/booths/jedi4ever/booth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* North Pole Night Run: puts the street, the traffic and the van together
* back to front, steers the van from the pointer (or the autopilot when nobody
* is), and keeps the run and the best of this visit as the booth's one memory.
* is), lets the robin hop along the bottom, and keeps the run and the best of this visit as the booth's one memory.
*/
import { drawLighting, drawNoir } from './lighting';
import type { NorthPoleSquare, SquarePointer } from '../../../src/northpole/square';
import { drawDashboard } from './dashboard';
import { autopilotTarget } from './autopilot';
import { advance, newRun, restart, type Run } from './game';
import { drawSky, drawSkyline } from './sky';
import { drawRobin, robinAt, robinLook } from './robin';
import { snowmen } from './snowman';
import { drawPuff, drawSnowfall, drawSparkles } from './snow';
import { drawRoad, drawTrails, roadside, type Roadside } from './street';
Expand Down Expand Up @@ -149,6 +150,9 @@ const booth: NorthPoleSquare = {
{ speed: run.van.speed, score: run.score, best: run.best },
run.crashed && run.crashTime > 0.6,
);
// The robin hops along the dashboard's rim on its own clock, apart from the run.
const robin = robinAt(size, time / 1000);
if (robin) drawRobin(ctx, size, robin, robinLook(robin, size, pointer));
},
};

Expand Down
125 changes: 125 additions & 0 deletions carnival/booths/jedi4ever/robin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { describe, expect, it } from 'vitest';
import type { Ctx } from './brushes';
import { drawRobin, LOOK_REACH, ROBIN_CYCLE, robinAt, robinLook } from './robin';

describe('robinAt', () => {
it('is out of sight for part of every cycle, so it comes by every few seconds', () => {
const seen = Array.from({ length: 90 }, (_, i) => robinAt(100, (i / 90) * ROBIN_CYCLE));
expect(seen.some((robin) => robin === undefined)).toBe(true);
expect(seen.some((robin) => robin !== undefined)).toBe(true);
expect(ROBIN_CYCLE).toBeGreaterThanOrEqual(5);
expect(ROBIN_CYCLE).toBeLessThanOrEqual(15);
});

it('comes back the same way each cycle, from the time alone', () => {
expect(robinAt(100, 1.3)).toEqual(robinAt(100, 1.3 + ROBIN_CYCLE * 2));
});

it('crosses the whole bottom edge, from one side to the other', () => {
const xs = Array.from({ length: 200 }, (_, i) => robinAt(100, (i / 200) * ROBIN_CYCLE)?.x);
const placed = xs.filter((x): x is number => x !== undefined);
expect(Math.min(...placed)).toBeLessThan(5);
expect(Math.max(...placed)).toBeGreaterThan(95);
});

it('walks one way on one pass and back the other on the next', () => {
const first = robinAt(100, 1);
const second = robinAt(100, 1 + ROBIN_CYCLE);
expect(first?.heading).toBe(1);
expect(second?.heading).toBe(-1);
expect(second?.x).toBeCloseTo(100 - (first?.x ?? 0));
});

it('hops: off the ground mid-hop and back down between hops', () => {
const lifts = Array.from({ length: 60 }, (_, i) => robinAt(100, 1 + i * 0.02)?.lift ?? 0);
expect(Math.max(...lifts)).toBeGreaterThan(1);
expect(Math.min(...lifts)).toBe(0);
});

it('stays still on the ground between hops and only moves while in the air', () => {
const grounded = Array.from({ length: 60 }, (_, i) => robinAt(100, 1 + i * 0.02)).filter(
(robin) => robin?.lift === 0,
);
expect(grounded.length).toBeGreaterThan(1);
});

it('stands on the bottom edge of the scene, scaled with the square', () => {
const small = robinAt(100, 1);
const large = robinAt(400, 1);
expect(small?.y).toBeGreaterThan(80);
expect(large?.y).toBeCloseTo((small?.y ?? 0) * 4);
expect(large?.x).toBeCloseTo((small?.x ?? 0) * 4);
});
});

describe('robinLook', () => {
const robin = { x: 50, y: 88, lift: 0, heading: 1 as const };

it('looks where it is going while nobody is near', () => {
expect(robinLook(robin, 100)).toEqual({ facing: 1, tilt: 0 });
expect(robinLook(robin, 100, { x: 50, y: 10, down: false, presses: 0 })).toEqual({
facing: 1,
tilt: 0,
});
});

it('turns to face a pointer that comes near behind it', () => {
const look = robinLook(robin, 100, { x: 40, y: 80, down: false, presses: 0 });
expect(look.facing).toBe(-1);
expect(look.tilt).toBeGreaterThan(0);
});

it('keeps facing ahead when the pointer is near in front, and cocks its head up at it', () => {
const look = robinLook(robin, 100, { x: 60, y: 70, down: false, presses: 0 });
expect(look.facing).toBe(1);
expect(look.tilt).toBeGreaterThan(0);
expect(look.tilt).toBeLessThanOrEqual(0.8);
});

it('notices the pointer only within its reach, scaled with the square', () => {
const edge = LOOK_REACH * 100;
const near = robinLook(robin, 100, { x: 50 - edge + 1, y: 88, down: false, presses: 0 });
const far = robinLook(robin, 100, { x: 50 - edge - 1, y: 88, down: false, presses: 0 });
expect(near.facing).toBe(-1);
expect(far.facing).toBe(1);
});
});

/** The arcs a robin paints, on a canvas that keeps only those. */
function arcs(size: number, look: { facing: 1 | -1; tilt: number }): number[][] {
const kept: number[][] = [];
const ignore = () => undefined;
const canvas = {
fillStyle: '',
globalAlpha: 1,
save: ignore,
restore: ignore,
beginPath: ignore,
closePath: ignore,
moveTo: ignore,
lineTo: ignore,
fill: ignore,
ellipse: (x: number, y: number) => kept.push([x, y]),
arc: (x: number, y: number, r: number) => kept.push([x, y, r]),
};
const robin = robinAt(size, 1);
if (robin) drawRobin(canvas as unknown as Ctx, size, robin, look);
return kept;
}

describe('drawRobin', () => {
it('scales the whole bird with the square', () => {
const small = arcs(100, { facing: 1, tilt: 0 });
const large = arcs(400, { facing: 1, tilt: 0 });
expect(small.length).toBeGreaterThan(2);
small.forEach((arc, i) => {
arc.forEach((value, j) => {
expect(large[i]?.[j]).toBeCloseTo(value * 4);
});
});
});

it('turns its head when it looks the other way', () => {
expect(arcs(100, { facing: -1, tilt: 0.4 })).not.toEqual(arcs(100, { facing: 1, tilt: 0 }));
});
});
144 changes: 144 additions & 0 deletions carnival/booths/jedi4ever/robin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* The robin: a small red-breasted robin that hops along the dashboard's rim,
* the bottom edge of the scene, every few seconds, and turns its head to look
* at the pointer when it comes near. It is scenery only: it never touches the
* run, so the game plays exactly as before. Drawn after the noir wash, it
* keeps its red breast like the van.
*/
import type { SquarePointer } from '../../../src/northpole/square';
import { circle, colors, polygon, type Ctx } from './brushes';

/** Seconds between one robin's visit and the next. */
export const ROBIN_CYCLE = 9;
/** Seconds of each cycle the robin spends crossing; the rest it is out of sight. */
const CROSSING = 6;
/** Seconds each hop takes, in the air and then a moment on the ground. */
const HOP = 0.5;
/** The share of each hop spent in the air. */
const AIRBORNE = 0.6;
/** Where the robin stands: the dashboard's top edge, as a share of the square. */
const RIM = 0.885;
/** How far beyond each side the robin starts and ends, so it hops in and out of frame. */
const MARGIN = 0.06;
/** How near, as a share of the square, the pointer comes before the robin looks at it. */
export const LOOK_REACH = 0.25;

/** The robin on the rim: where its feet are, how high it hops and which way it is going. */
export interface Robin {
x: number;
y: number;
lift: number;
heading: 1 | -1;
}

/** Which way the robin's head points: its side, and how far it is cocked up, in radians. */
export interface Look {
facing: 1 | -1;
tilt: number;
}

/**
* The robin `seconds` into the visit, or nothing while it is out of sight.
* Each pass crosses the other way, and it only moves while in the air.
*/
export function robinAt(size: number, seconds: number): Robin | undefined {
const pass = Math.floor(seconds / ROBIN_CYCLE);
const into = seconds - pass * ROBIN_CYCLE;
if (into >= CROSSING) return undefined;
const hop = Math.floor(into / HOP);
const air = Math.min(1, (into - hop * HOP) / (HOP * AIRBORNE));
const along = (hop + air) / (CROSSING / HOP);
const heading = pass % 2 === 0 ? 1 : -1;
const across = -MARGIN + (1 + 2 * MARGIN) * along;
return {
x: (heading === 1 ? across : 1 - across) * size,
y: RIM * size,
lift: air < 1 ? Math.sin(Math.PI * air) * 0.035 * size : 0,
heading,
};
}

/** Where the robin looks: at a pointer within its reach, otherwise the way it is hopping. */
export function robinLook(robin: Robin, size: number, pointer?: SquarePointer): Look {
if (!pointer) return { facing: robin.heading, tilt: 0 };
const dx = pointer.x - robin.x;
const up = robin.y - robin.lift - pointer.y;
if (Math.hypot(dx, up) > LOOK_REACH * size) return { facing: robin.heading, tilt: 0 };
const facing = dx === 0 ? robin.heading : dx > 0 ? 1 : -1;
return { facing, tilt: Math.max(-0.3, Math.min(0.8, Math.atan2(up, Math.abs(dx)))) };
}

/** One robin: a soft shadow, twig legs, a brown back, a red breast, a cocked head and a beak. */
export function drawRobin(ctx: Ctx, size: number, robin: Robin, look: Look) {
const u = size * 0.012;
const { x, y, heading } = robin;
const body = y - robin.lift - u * 1.7;
ctx.globalAlpha = 0.35;
ctx.fillStyle = colors.shadow;
ctx.beginPath();
ctx.ellipse(x + u * 0.3, y, u * 1.3, u * 0.35, 0, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
for (const leg of [-0.35, 0.35]) {
polygon(
ctx,
[
[x + leg * u - u * 0.08, body + u * 0.6],
[x + leg * u + u * 0.08, body + u * 0.6],
[x + leg * u + u * 0.08, y - robin.lift],
[x + leg * u - u * 0.08, y - robin.lift],
],
colors.cocoa,
);
}
// The tail sticks out behind, and flicks up while the robin is in the air.
const flick = robin.lift / (0.035 * size);
polygon(
ctx,
[
[x - heading * u * 0.8, body - u * 0.2],
[x - heading * u * 2.1, body - u * (0.7 + flick * 0.5)],
[x - heading * u * 2.2, body - u * (0.2 + flick * 0.5)],
[x - heading * u * 0.8, body + u * 0.4],
],
colors.cocoa,
);
ctx.fillStyle = colors.wood;
ctx.beginPath();
ctx.ellipse(x, body, u * 1.25, u * 0.95, 0, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = colors.cream;
ctx.beginPath();
ctx.ellipse(x + heading * u * 0.2, body + u * 0.45, u * 0.8, u * 0.45, 0, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = colors.berry;
circle(ctx, x + heading * u * 0.6, body - u * 0.1, u * 0.62);
// The wing, lit from the upper left, lifting with the hop.
polygon(
ctx,
[
[x - heading * u * 0.7, body - u * 0.3],
[x + heading * u * 0.3, body - u * (0.5 + flick * 0.4)],
[x + heading * u * 0.1, body + u * 0.3],
],
colors.cocoa,
);
const headX = x + heading * u * 0.85;
const headY = body - u * 1.05;
ctx.fillStyle = colors.wood;
circle(ctx, headX, headY, u * 0.6);
ctx.fillStyle = colors.berry;
circle(ctx, headX + look.facing * u * 0.25, headY + u * 0.25, u * 0.38);
const cos = Math.cos(look.tilt);
const sin = Math.sin(look.tilt);
const tip = (reach: number, side: number): [number, number] => [
headX + look.facing * (cos * reach + sin * side) * u,
headY + (cos * side - sin * reach) * u,
];
polygon(ctx, [tip(0.45, -0.15), tip(1.05, 0), tip(0.45, 0.15)], colors.gold);
ctx.fillStyle = colors.ink;
const [eyeX, eyeY] = tip(0.25, -0.2);
circle(ctx, eyeX, eyeY, u * 0.12);
ctx.fillStyle = colors.text;
circle(ctx, eyeX - u * 0.04, eyeY - u * 0.04, u * 0.04);
}