From 1931228c292a371ffd707904e1de259b354bcedb Mon Sep 17 00:00:00 2001 From: prem315 Date: Sun, 1 Nov 2026 20:01:16 +0530 Subject: [PATCH] feat(web): add playable Chrome dino arcade to new session page --- apps/web/src/landing/arcade.tsx | 566 ++++++++++++++++++ apps/web/src/landing/dino.ts | 955 +++++++++++++++++++++++++++++++ apps/web/src/landing/landing.tsx | 12 +- 3 files changed, 1530 insertions(+), 3 deletions(-) create mode 100644 apps/web/src/landing/arcade.tsx create mode 100644 apps/web/src/landing/dino.ts diff --git a/apps/web/src/landing/arcade.tsx b/apps/web/src/landing/arcade.tsx new file mode 100644 index 00000000..9232d88e --- /dev/null +++ b/apps/web/src/landing/arcade.tsx @@ -0,0 +1,566 @@ +import { useCallback, useEffect, useRef, useState } from "react"; + +import { + ARCADE_MODES, + type ArcadeMode, + createDinoArcade, + type DinoArcade, + type DinoSprite, + type DinoSpriteKind, + SPRITE_PATHS, + SPRITE_UNIT, +} from "./dino"; + +const CELL = 6; +const GAP = 1; +const PITCH = CELL + GAP; +const BORDER_OPACITY = 0.06; +const PEAK_OPACITY = 0.72; +const SPRITE_OPACITY = 0.92; +const BUBBLE_OPACITY = 0.9; +const IDLE_DIM = 0.55; +const BUBBLE_EASE = 0.22; +const FRAME_MS = 33; + +/** One "pixel" of the speech bubble art, in CSS px. */ +const UNIT = 2; +const STEP = UNIT; +const TAIL_STEPS = 3; +const TAIL_H = TAIL_STEPS * STEP; +const SHADOW_OFFSET = 2 * UNIT; +const FONT_PX = 10; +const PAD_X = 3 * UNIT; +const PAD_Y = 2 * UNIT; +const FONT = `600 ${FONT_PX}px ui-monospace, SFMono-Regular, Menlo, monospace`; + +const snap = (value: number) => Math.round(value / UNIT) * UNIT; +const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value)); + +type Point = readonly [number, number]; + +function outline(x: number, y: number, w: number, h: number, tailX: number, below: boolean): Point[] { + const points: Point[] = []; + const at = (px: number, py: number) => points.push([px, py]); + + at(x + STEP, y); + if (below) { + at(x + tailX, y); + at(x + tailX, y - TAIL_H); + for (let step = TAIL_STEPS; step > 0; step--) { + at(x + tailX + (TAIL_STEPS - step + 1) * STEP, y - step * STEP); + at(x + tailX + (TAIL_STEPS - step + 1) * STEP, y - (step - 1) * STEP); + } + } + at(x + w - STEP, y); + at(x + w - STEP, y + STEP); + at(x + w, y + STEP); + + at(x + w, y + h - STEP); + at(x + w - STEP, y + h - STEP); + at(x + w - STEP, y + h); + if (!below) { + for (let step = 1; step <= TAIL_STEPS; step++) { + at(x + tailX + (TAIL_STEPS - step + 1) * STEP, y + h + (step - 1) * STEP); + at(x + tailX + (TAIL_STEPS - step + 1) * STEP, y + h + step * STEP); + } + at(x + tailX, y + h + TAIL_H); + at(x + tailX, y + h); + } + at(x + STEP, y + h); + at(x + STEP, y + h - STEP); + at(x, y + h - STEP); + + at(x, y + STEP); + at(x + STEP, y + STEP); + + return points; +} + +function trace(ctx: CanvasRenderingContext2D, points: readonly Point[]) { + ctx.beginPath(); + points.forEach(([x, y], index) => { + if (index === 0) ctx.moveTo(x, y); + else ctx.lineTo(x, y); + }); + ctx.closePath(); +} + +function drawSpeechBubble( + ctx: CanvasRenderingContext2D, + text: string, + headX: number, + headY: number, + headSize: number, + bounds: { width: number; height: number }, + alpha: number, + theme: { fg: string; bg: string }, +) { + if (alpha <= 0.01) return; + + ctx.save(); + ctx.font = FONT; + if ("letterSpacing" in ctx) ctx.letterSpacing = "1px"; + + const w = snap(ctx.measureText(text).width + PAD_X * 2); + const h = snap(FONT_PX + PAD_Y * 2); + const margin = UNIT + SHADOW_OFFSET; + + const above = headY - UNIT - TAIL_H - h; + const below = above < margin; + const y = snap(below ? headY + headSize + UNIT + TAIL_H : above); + + const preferred = headX - 4 * STEP; + const x = snap(clamp(preferred, margin, Math.max(margin, bounds.width - w - margin))); + const tailX = clamp(snap(headX - x), 2 * STEP, Math.max(2 * STEP, w - (TAIL_STEPS + 2) * STEP)); + + ctx.globalAlpha = alpha; + ctx.lineJoin = "miter"; + ctx.lineWidth = UNIT; + + ctx.fillStyle = theme.fg; + ctx.globalAlpha = alpha * 0.2; + trace(ctx, outline(x + SHADOW_OFFSET, y + SHADOW_OFFSET, w, h, tailX, below)); + ctx.fill(); + + ctx.globalAlpha = alpha; + trace(ctx, outline(x, y, w, h, tailX, below)); + ctx.fillStyle = theme.bg; + ctx.fill(); + ctx.strokeStyle = theme.fg; + ctx.stroke(); + + ctx.fillStyle = theme.fg; + ctx.textBaseline = "middle"; + ctx.textAlign = "left"; + ctx.fillText(text, x + PAD_X, y + h / 2 + 1); + + ctx.restore(); +} + +const spritePaths = new Map(); + +function getSpritePath(kind: DinoSpriteKind): Path2D { + const cached = spritePaths.get(kind); + if (cached !== undefined) return cached; + const path = new Path2D(SPRITE_PATHS[kind]); + spritePaths.set(kind, path); + return path; +} + +function drawSprite(ctx: CanvasRenderingContext2D, sprite: DinoSprite, fill: string) { + const path = getSpritePath(sprite.kind); + const unit = SPRITE_UNIT * PITCH; + const left = sprite.x * PITCH; + const top = sprite.y * PITCH; + + ctx.save(); + ctx.fillStyle = fill; + ctx.translate(left, top); + ctx.scale(unit, unit); + ctx.fill(path); + ctx.restore(); +} + +function parseRgb(value: string, fallback: string) { + const match = value.match(/\d+/g); + if (!match || match.length < 3) return fallback; + return `${match[0]}, ${match[1]}, ${match[2]}`; +} + +function parseContentRgb() { + return parseRgb(getComputedStyle(document.body).color, "235, 235, 235"); +} + +function parseSurfaceRgb() { + return parseRgb(getComputedStyle(document.body).backgroundColor, "23, 23, 23"); +} + +const JUMP_KEYS = new Set([" ", "Spacebar", "ArrowUp", "w", "W"]); +const DUCK_KEYS = new Set(["ArrowDown", "s", "S"]); + +export function Arcade({ onPlayingChange }: { onPlayingChange: (playing: boolean) => void }) { + const rootRef = useRef(null); + const startRef = useRef(null); + const returnFocusRef = useRef(null); + const canvasRef = useRef(null); + const arcadeRef = useRef(null); + const playingRef = useRef(false); + const scoreRef = useRef(0); + const highScoreRef = useRef(0); + const gameOverRef = useRef(false); + + const [playing, setPlaying] = useState(false); + const [score, setScore] = useState(0); + const [highScore, setHighScore] = useState(0); + const [gameOver, setGameOver] = useState(false); + const [mode, setMode] = useState("mid"); + + playingRef.current = playing; + + useEffect(() => { + const root = rootRef.current; + const canvas = canvasRef.current; + const ctx = canvas?.getContext("2d"); + if (!root || !canvas || !ctx) return; + + const arcade = createDinoArcade(); + arcadeRef.current = arcade; + + const gridCanvas = document.createElement("canvas"); + const gridCtx = gridCanvas.getContext("2d"); + + let raf = 0; + let cols = 0; + let rows = 0; + let width = 0; + let height = 0; + let stamp = new Float32Array(0); + let rgb = parseContentRgb(); + let surfaceRgb = parseSurfaceRgb(); + let lastFrame = 0; + let bubbleAt: { x: number; y: number } | null = null; + + const drawGrid = () => { + if (!gridCtx || width <= 0 || height <= 0) return; + const dpr = window.devicePixelRatio || 1; + gridCanvas.width = Math.floor(width * dpr); + gridCanvas.height = Math.floor(height * dpr); + gridCtx.setTransform(dpr, 0, 0, dpr, 0, 0); + gridCtx.lineWidth = 1; + gridCtx.strokeStyle = `rgba(${rgb}, ${BORDER_OPACITY})`; + for (let y = 0; y < rows; y++) { + for (let x = 0; x < cols; x++) { + gridCtx.strokeRect(x * PITCH + 0.5, y * PITCH + 0.5, CELL - 1, CELL - 1); + } + } + }; + + const start = () => { + if (raf || document.hidden || width <= 0 || height <= 0) return; + lastFrame = 0; + raf = requestAnimationFrame(draw); + }; + + const layout = () => { + const rect = root.getBoundingClientRect(); + width = rect.width; + height = rect.height; + if (width <= 0 || height <= 0) return; + + const dpr = window.devicePixelRatio || 1; + const nextCols = Math.ceil(width / PITCH); + const nextRows = Math.ceil(height / PITCH); + + canvas.width = Math.floor(width * dpr); + canvas.height = Math.floor(height * dpr); + canvas.style.width = `${width}px`; + canvas.style.height = `${height}px`; + ctx.setTransform(dpr, 0, 0, dpr, 0, 0); + arcade.resize(nextCols, nextRows); + + cols = nextCols; + rows = nextRows; + drawGrid(); + start(); + }; + + const paint = () => { + const dim = arcade.controlled() ? 1 : IDLE_DIM; + if (stamp.length === cols * rows) stamp.fill(0); + else stamp = new Float32Array(cols * rows); + arcade.stamp(stamp, cols, rows); + + const fade = arcade.fade(); + ctx.clearRect(0, 0, width, height); + + const peak = PEAK_OPACITY * dim; + for (let y = 0; y < rows; y++) { + for (let x = 0; x < cols; x++) { + const fillOpacity = (stamp[y * cols + x] ?? 0) * peak; + if (fillOpacity > 0.02) { + ctx.fillStyle = `rgba(${rgb}, ${fillOpacity})`; + ctx.fillRect(x * PITCH, y * PITCH, CELL, CELL); + } + } + } + + if (fade > 0 && gridCanvas.width > 0) { + ctx.globalAlpha = fade; + ctx.drawImage(gridCanvas, 0, 0, width, height); + ctx.globalAlpha = 1; + } + + for (const sprite of arcade.sprites()) { + const opacity = sprite.alpha * dim * SPRITE_OPACITY; + if (opacity <= 0.02) continue; + drawSprite(ctx, sprite, `rgba(${rgb}, ${opacity})`); + } + + const bubble = arcade.speechBubble(); + if (!bubble) { + bubbleAt = null; + return; + } + + const target = { x: bubble.x * PITCH, y: bubble.y * PITCH }; + bubbleAt = !bubbleAt + ? target + : { + x: bubbleAt.x + (target.x - bubbleAt.x) * BUBBLE_EASE, + y: bubbleAt.y + (target.y - bubbleAt.y) * BUBBLE_EASE, + }; + + drawSpeechBubble( + ctx, + bubble.text, + bubbleAt.x, + bubbleAt.y, + CELL, + { width, height }, + bubble.alpha * BUBBLE_OPACITY * dim, + { fg: `rgb(${rgb})`, bg: `rgb(${surfaceRgb})` }, + ); + }; + + function draw(time: number) { + if (document.hidden || width <= 0 || height <= 0) { + raf = 0; + return; + } + raf = requestAnimationFrame(draw); + if (time - lastFrame < FRAME_MS) return; + const dt = lastFrame ? time - lastFrame : FRAME_MS; + lastFrame = time; + + arcade.step(dt); + paint(); + + if (!playingRef.current) return; + + const nextScore = arcade.score(); + if (nextScore !== scoreRef.current) { + scoreRef.current = nextScore; + setScore(nextScore); + } + const nextHigh = arcade.highScore(); + if (nextHigh !== highScoreRef.current) { + highScoreRef.current = nextHigh; + setHighScore(nextHigh); + } + const nextOver = arcade.gameOver(); + if (nextOver !== gameOverRef.current) { + gameOverRef.current = nextOver; + setGameOver(nextOver); + } + } + + layout(); + + document.addEventListener("visibilitychange", start); + + const resizeObserver = new ResizeObserver(layout); + resizeObserver.observe(root); + + const themeObserver = new MutationObserver(() => { + const next = parseContentRgb(); + surfaceRgb = parseSurfaceRgb(); + if (next === rgb) return; + rgb = next; + drawGrid(); + }); + themeObserver.observe(document.documentElement, { + attributes: true, + attributeFilter: ["style", "class", "data-theme"], + }); + + return () => { + cancelAnimationFrame(raf); + document.removeEventListener("visibilitychange", start); + resizeObserver.disconnect(); + themeObserver.disconnect(); + arcadeRef.current = null; + }; + }, []); + + const takeControl = useCallback(() => { + const arcade = arcadeRef.current; + arcade?.setMode(mode); + arcade?.takeControl(); + scoreRef.current = 0; + gameOverRef.current = false; + setScore(0); + setGameOver(false); + returnFocusRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null; + setPlaying(true); + onPlayingChange(true); + }, [mode, onPlayingChange]); + + const pickMode = useCallback((next: ArcadeMode) => { + arcadeRef.current?.setMode(next); + setMode(next); + }, []); + + const releaseControl = useCallback(() => { + arcadeRef.current?.releaseControl(); + scoreRef.current = 0; + gameOverRef.current = false; + setScore(0); + setGameOver(false); + setPlaying(false); + onPlayingChange(false); + }, [onPlayingChange]); + + useEffect(() => { + if (!playing) { + const previous = returnFocusRef.current; + if (previous) (previous.isConnected ? previous : startRef.current)?.focus(); + returnFocusRef.current = null; + return; + } + + const root = rootRef.current; + root?.focus(); + const onWheel = (event: WheelEvent) => event.preventDefault(); + root?.addEventListener("wheel", onWheel, { passive: false }); + + const onKeyDown = (event: KeyboardEvent) => { + if (event.metaKey || event.ctrlKey || event.altKey || event.isComposing) return; + const active = document.activeElement; + if (active !== root && !(active instanceof Node && root?.contains(active))) return; + + if (event.key === "Escape") { + event.preventDefault(); + event.stopPropagation(); + releaseControl(); + return; + } + if (active instanceof Element && active.closest("button")) return; + if (JUMP_KEYS.has(event.key)) { + event.preventDefault(); + event.stopPropagation(); + if (!event.repeat) arcadeRef.current?.jump(); + return; + } + if (DUCK_KEYS.has(event.key)) { + event.preventDefault(); + event.stopPropagation(); + arcadeRef.current?.setDuck(true); + } + }; + + const onKeyUp = (event: KeyboardEvent) => { + if (DUCK_KEYS.has(event.key)) { + arcadeRef.current?.setDuck(false); + } + }; + + const onBlur = () => { + arcadeRef.current?.setDuck(false); + }; + + window.addEventListener("keydown", onKeyDown, true); + window.addEventListener("keyup", onKeyUp, true); + window.addEventListener("blur", onBlur); + return () => { + window.removeEventListener("keydown", onKeyDown, true); + window.removeEventListener("keyup", onKeyUp, true); + window.removeEventListener("blur", onBlur); + root?.removeEventListener("wheel", onWheel); + }; + }, [playing, releaseControl]); + + return ( +
{ + if (!playing || event.button !== 0 || (event.target instanceof Element && event.target.closest("button"))) + return; + event.preventDefault(); + rootRef.current?.focus(); + arcadeRef.current?.jump(); + }} + aria-label={playing ? "Dino runner. Space or Up to jump, Down to duck, Escape to release." : undefined} + className={ + playing + ? "absolute inset-0 z-20 touch-none overflow-hidden bg-frame outline-none" + : "group pointer-events-auto absolute inset-x-0 top-0 z-0 h-48" + } + > +
+ +
+ + {playing ? ( +
+ + score {String(score).padStart(5, "0")} + {highScore > 0 ? ( + hi {String(highScore).padStart(5, "0")} + ) : null} + {gameOver ? game over : null} + +
+ {ARCADE_MODES.map((id) => { + const on = mode === id; + return ( + + ); + })} +
+
+ +
+
+ ) : ( +
+ +
+ )} + {playing ? ( +

+ {gameOver ? "Space or tap to restart" : "Space / ↑ / tap to jump · ↓ to duck"} · Esc to release +

+ ) : null} +
+ ); +} diff --git a/apps/web/src/landing/dino.ts b/apps/web/src/landing/dino.ts new file mode 100644 index 00000000..e66f797d --- /dev/null +++ b/apps/web/src/landing/dino.ts @@ -0,0 +1,955 @@ +/** + * Chrome offline T-Rex runner for the empty-session grid. + * + * When idle, the dino runs on autopilot along the top grid band — jumping over + * saguaro clusters and ducking under low-flying pterodactyls. Taking control + * hands jump (Space / Up / W) and duck (Down / S) over to the player. + */ + +export const ARCADE_MODES = ["low", "mid", "hard"] as const; +export type ArcadeMode = (typeof ARCADE_MODES)[number]; + +/** One pixel of a sprite in grid-cell units (`PITCH = 7px`, so `0.36 * 7 ≈ 2.5px`). */ +export const SPRITE_UNIT = 0.36; + +export type SpeechBubble = { + text: string; + /** Grid cell the tail points at. */ + x: number; + y: number; + alpha: number; +}; + +export type DinoSpriteKind = + | "dino-jump" + | "dino-run-a" + | "dino-run-b" + | "dino-duck-a" + | "dino-duck-b" + | "dino-crash" + | "cactus-small" + | "cactus-tall" + | "ptero-up" + | "ptero-down" + | "star-face" + | "star-edge"; + +export type DinoSprite = { + kind: DinoSpriteKind; + /** Left edge in grid cells. */ + x: number; + /** Top edge in grid cells. */ + y: number; + /** Width in sprite pixels. */ + width: number; + /** Height in sprite pixels. */ + height: number; + alpha: number; +}; + +const SPRITE_ROWS: Record = { + "dino-jump": [ + "........#######.", + ".......#########", + ".......##.######", + ".......#########", + ".......#####....", + ".......########.", + "#.....######....", + "##...########...", + "###.###########.", + "############..#.", + ".###########....", + "..#########.....", + "...#######......", + "....##..##......", + "....#....#......", + "....##...##.....", + ], + "dino-run-a": [ + "........#######.", + ".......#########", + ".......##.######", + ".......#########", + ".......#####....", + ".......########.", + "#.....######....", + "##...########...", + "###.###########.", + "############..#.", + ".###########....", + "..#########.....", + "...#######......", + "....##..###.....", + "....#...........", + "....##..........", + ], + "dino-run-b": [ + "........#######.", + ".......#########", + ".......##.######", + ".......#########", + ".......#####....", + ".......########.", + "#.....######....", + "##...########...", + "###.###########.", + "############..#.", + ".###########....", + "..#########.....", + "...#######......", + "....###..##.....", + ".........#......", + ".........##.....", + ], + "dino-duck-a": [ + "............#######.", + "#..........#########", + "##..#########.######", + "####################", + ".############.#####.", + "..##########..#####.", + "...########...#.....", + "....##..###.........", + "....#...............", + "....##..............", + ], + "dino-duck-b": [ + "............#######.", + "#..........#########", + "##..#########.######", + "####################", + ".############.#####.", + "..##########..#####.", + "...########...#.....", + "....###..##.........", + ".........#..........", + ".........##.........", + ], + "dino-crash": [ + "........#######.", + ".......#########", + ".......#...#####", + ".......#.#.#####", + ".......#...#....", + ".......########.", + "#.....######....", + "##...########...", + "###.###########.", + "############..#.", + ".###########....", + "..#########.....", + "...#######......", + "....##..##......", + "....#....#......", + "....##...##.....", + ], + "cactus-small": [ + "...##...", + "...##...", + "#..##...", + "#..##..#", + "#..##..#", + "#####..#", + ".######.", + "...##...", + "...##...", + "...##...", + "...##...", + "...##...", + ], + "cactus-tall": [ + "....##....", + "....##....", + "#...##....", + "##..##..#.", + "##..##..##", + "##..##..##", + "######..##", + ".#####..##", + "....######", + "....#####.", + "....##....", + "....##....", + "....##....", + "....##....", + "....##....", + "....##....", + ], + "ptero-up": [ + "......##........", + ".....###........", + "....####........", + "..###.#####.....", + ".##########.....", + "############....", + "...##########...", + ".....##########.", + "......####..##..", + "................", + "................", + "................", + ], + "ptero-down": [ + "................", + "................", + "................", + "..###...........", + ".#####..........", + "############....", + "...#############", + ".....#########..", + ".....####...##..", + ".....###........", + "......##........", + "......#.........", + ], + "star-face": ["...##...", "...##...", "..####..", "########", "########", "..####..", "...##...", "...##..."], + "star-edge": ["........", "...##...", "...##...", "..####..", "..####..", "...##...", "...##...", "........"], +}; + +/** Merges each row's filled runs into one SVG path so each sprite paints in a single fill. */ +function traceRows(rows: readonly string[]): string { + let path = ""; + rows.forEach((row, y) => { + let x = 0; + while (x < row.length) { + if (row[x] !== "#") { + x += 1; + continue; + } + let run = 1; + while (row[x + run] === "#") run += 1; + path += `M${x} ${y}h${run}v1h-${run}z`; + x += run; + } + }); + return path; +} + +export const SPRITE_PATHS: Record = { + "dino-jump": traceRows(SPRITE_ROWS["dino-jump"]), + "dino-run-a": traceRows(SPRITE_ROWS["dino-run-a"]), + "dino-run-b": traceRows(SPRITE_ROWS["dino-run-b"]), + "dino-duck-a": traceRows(SPRITE_ROWS["dino-duck-a"]), + "dino-duck-b": traceRows(SPRITE_ROWS["dino-duck-b"]), + "dino-crash": traceRows(SPRITE_ROWS["dino-crash"]), + "cactus-small": traceRows(SPRITE_ROWS["cactus-small"]), + "cactus-tall": traceRows(SPRITE_ROWS["cactus-tall"]), + "ptero-up": traceRows(SPRITE_ROWS["ptero-up"]), + "ptero-down": traceRows(SPRITE_ROWS["ptero-down"]), + "star-face": traceRows(SPRITE_ROWS["star-face"]), + "star-edge": traceRows(SPRITE_ROWS["star-edge"]), +}; + +const BOOT_FADE_MS = 420; +const LEG_STEP_MS = 95; +const WING_FLAP_MS = 160; +const STAR_SPIN_MS = 220; +const OVER_AUTO_RESET_MS = 2600; +const OVER_RESTART_GUARD_MS = 320; +const JUMP_BUFFER_MS = 130; + +const GRAVITY = 240; +const FAST_FALL_MULTIPLIER = 2.8; +const JUMP_VELOCITY = 59; + +const IDLE_SPEED = 34; +const SPEEDS: Record = { + low: { base: 28, max: 44, accel: 0.45 }, + mid: { base: 36, max: 58, accel: 0.7 }, + hard: { base: 46, max: 72, accel: 1.0 }, +}; + +const SPEECH_FADE_MS = 260; +const SPEECH_HOLD_MS = 2100; + +const RUN_CHATTER = [ + "NO INTERNET?", + "STILL RUNNING", + "200 OK", + "PING 0MS", + "SHIP IT", + "CACHE HIT", + "ZERO DOWNTIME", + "AIR TIME", + "CLEARED IT", + "HOLD MY COFFEE", + "CHROME WHO?", + "OFFLINE MODE", + "FAST TRACK", +]; + +const CRASH_CHATTER = ["ERR_DISCONNECTED", "GAME OVER", "404: JUMP NOT FOUND", "MERGE CONFLICT", "SKILL ISSUE", "OOF"]; + +type CactusPiece = { + kind: "cactus-small" | "cactus-tall"; + offsetX: number; + widthPx: number; + heightPx: number; +}; + +type Obstacle = + | { + type: "cactus"; + x: number; + width: number; + height: number; + pieces: readonly CactusPiece[]; + } + | { + type: "ptero"; + x: number; + width: number; + height: number; + /** Cells above the ground baseline. */ + elevation: number; + }; + +type Cloud = { + x: number; + /** Cells above the ground baseline. */ + elevation: number; + width: number; + speedFactor: number; +}; + +type Dust = { + x: number; + /** Cells above the ground baseline. */ + elevation: number; + age: number; + life: number; +}; + +type StarPickup = { + x: number; + elevation: number; + age: number; +}; + +type Speech = { + text: string; + age: number; +}; + +function clamp(value: number, min: number, max: number) { + return Math.max(min, Math.min(max, value)); +} + +function pick(items: readonly T[]): T { + return items[Math.floor(Math.random() * items.length)]!; +} + +/** Deterministic hash for scrolling ground pebbles so they stay fixed to world X. */ +function groundHash(worldCol: number): number { + const n = Math.imul(worldCol ^ 0x9e3779b9, 0x85ebca6b); + return ((n ^ (n >>> 13)) >>> 0) / 0xffffffff; +} + +export type DinoArcade = { + resize(nextCols: number, nextRows: number): void; + takeControl(): void; + releaseControl(): void; + jump(): void; + setDuck(active: boolean): void; + setMode(next: ArcadeMode): void; + mode(): ArcadeMode; + controlled(): boolean; + score(): number; + highScore(): number; + gameOver(): boolean; + step(dt: number): void; + speechBubble(): SpeechBubble | null; + sprites(): DinoSprite[]; + fade(): number; + stamp(out: Float32Array, stampCols: number, stampRows: number): void; +}; + +export function createDinoArcade(): DinoArcade { + let cols = 0; + let rows = 0; + let groundRow = 0; + + let booted = 0; + let player = false; + let mode: ArcadeMode = "mid"; + let status: "run" | "over" = "run"; + let overTimer = 0; + + let distance = 0; + let bonusScore = 0; + let score = 0; + let highScore = 0; + let nextMilestone = 100; + + let dinoY = 0; + let dinoVy = 0; + let ducking = false; + let jumpBuffer = 0; + let runClock = 0; + let dustTimer = 0; + + let obstacles: Obstacle[] = []; + let spawnGap = 28; + let clouds: Cloud[] = []; + let dust: Dust[] = []; + let star: StarPickup | null = null; + let starTimer = 4800; + + let speech: Speech | null = null; + let lastSay = ""; + + const computeGroundRow = () => + player + ? clamp(Math.floor(rows * 0.64), 14, Math.max(14, rows - 6)) + : clamp(Math.floor(rows * 0.68), 11, Math.max(11, rows - 3)); + + const dinoX = () => clamp(Math.floor(cols * 0.14), 6, 22); + + const currentSpeed = () => { + if (!player) return IDLE_SPEED; + const spec = SPEEDS[mode]; + return Math.min(spec.max, spec.base + (distance / 40) * spec.accel); + }; + + const bootAlpha = () => Math.min(1, booted / BOOT_FADE_MS); + + const say = (pool: readonly string[]) => { + let next = lastSay; + while (next === lastSay && pool.length > 1) next = pick(pool); + lastSay = next; + speech = { text: next, age: 0 }; + }; + + const spawnDust = (count: number) => { + const baseX = dinoX() + 1.2; + for (let i = 0; i < count; i++) { + dust.push({ + x: baseX - Math.random() * 1.6, + elevation: Math.random() * 1.2, + age: 0, + life: 180 + Math.random() * 120, + }); + } + }; + + const seedClouds = () => { + clouds = []; + if (cols <= 0) return; + const count = clamp(Math.floor(cols / 36), 2, 5); + const skySpan = Math.max(5, groundRow - 8); + for (let i = 0; i < count; i++) { + clouds.push({ + x: Math.floor(((i + 0.35) / count) * cols), + elevation: 6 + ((i * 5) % skySpan), + width: 4 + (i % 3), + speedFactor: 0.18 + (i % 3) * 0.05, + }); + } + }; + + const makeCactusObstacle = (atX: number): Obstacle => { + const roll = Math.random(); + const count = roll < 0.42 ? 1 : roll < 0.8 ? 2 : 3; + const pieces: CactusPiece[] = []; + let cursor = 0; + let maxHeightPx = 0; + + for (let i = 0; i < count; i++) { + const tall = Math.random() < 0.52; + const kind = tall ? "cactus-tall" : "cactus-small"; + const widthPx = tall ? 10 : 8; + const heightPx = tall ? 16 : 12; + pieces.push({ kind, offsetX: cursor, widthPx, heightPx }); + cursor += (widthPx - 1) * SPRITE_UNIT; + if (heightPx > maxHeightPx) maxHeightPx = heightPx; + } + + const totalWidth = cursor + SPRITE_UNIT; + return { + type: "cactus", + x: atX, + width: totalWidth, + height: maxHeightPx * SPRITE_UNIT, + pieces, + }; + }; + + const makePteroObstacle = (atX: number): Obstacle => { + // Low: jump over. Mid: duck under or jump over. High: duck under. + const elevations = [0.8, 3.9, 4.6] as const; + const elevation = pick(elevations); + return { + type: "ptero", + x: atX, + width: 16 * SPRITE_UNIT, + height: 10 * SPRITE_UNIT, + elevation, + }; + }; + + const spawnObstacle = () => { + const spawnX = cols + 4; + const canSpawnPtero = !player || score >= 90; + const obstacle = canSpawnPtero && Math.random() < 0.28 ? makePteroObstacle(spawnX) : makeCactusObstacle(spawnX); + obstacles.push(obstacle); + + const speed = currentSpeed(); + const minGap = speed * 0.68; + const maxGap = speed * 1.25; + spawnGap = minGap + Math.random() * (maxGap - minGap); + }; + + const maybeSpawnStar = () => { + if (star || cols < 28) return; + const spawnX = cols + 3; + // Keep the pickup away from obstacles so jumping for it isn't a trap. + const tooClose = obstacles.some((obs) => Math.abs(obs.x - spawnX) < 16); + if (tooClose) return; + star = { + x: spawnX, + elevation: 6.2 + Math.random() * 2.2, + age: 0, + }; + }; + + const startRun = () => { + status = "run"; + overTimer = 0; + distance = 0; + bonusScore = 0; + score = 0; + nextMilestone = 100; + dinoY = 0; + dinoVy = 0; + ducking = false; + jumpBuffer = 0; + dust = []; + star = null; + starTimer = 3500 + Math.random() * 3500; + speech = null; + obstacles = []; + if (cols > 0) { + obstacles.push(makeCactusObstacle(Math.max(dinoX() + 34, Math.floor(cols * 0.72)))); + } + spawnGap = 26 + Math.random() * 14; + }; + + const boot = () => { + booted = 0; + seedClouds(); + startRun(); + }; + + const triggerJump = () => { + if (dinoY <= 0.01) { + dinoY = 0; + dinoVy = JUMP_VELOCITY; + jumpBuffer = 0; + spawnDust(3); + return true; + } + return false; + }; + + /** Autopilot: jumps over cacti & low pterodactyls, ducks under mid/high pterodactyls, and leaps for safe stars. */ + const thinkDino = () => { + const x = dinoX(); + const standW = 16 * SPRITE_UNIT; + const speed = currentSpeed(); + const next = obstacles.find((obs) => obs.x + obs.width > x + 0.6); + + if (next) { + const dist = next.x - (x + standW); + if (next.type === "ptero" && next.elevation >= 3.5) { + ducking = dist < speed * 0.34 && next.x + next.width > x; + return; + } + + ducking = false; + const jumpLead = speed * 0.18 + next.width * 0.18; + if (dist <= jumpLead && dist > -1.5 && dinoY <= 0.01) { + triggerJump(); + return; + } + } else { + ducking = false; + } + + // Leap for a floating star when no obstacle is near the landing spot. + if (star && dinoY <= 0.01) { + const starDist = star.x - (x + standW * 0.6); + const safeLanding = !next || next.x - x > speed * 0.72; + if (safeLanding && starDist <= speed * 0.16 && starDist > -1) { + triggerJump(); + } + } + }; + + const checkCollisions = () => { + const x = dinoX(); + const isDucking = ducking && dinoY <= 0.05; + const dinoW = (isDucking ? 20 : 16) * SPRITE_UNIT; + const dinoH = (isDucking ? 10 : 16) * SPRITE_UNIT; + + const dLeft = x + 0.85; + const dRight = x + dinoW - 0.75; + const dBottom = dinoY + 0.25; + const dTop = dinoY + dinoH - 0.35; + + if (star) { + const sSize = 8 * SPRITE_UNIT; + const sLeft = star.x; + const sRight = star.x + sSize; + const sBottom = star.elevation; + const sTop = star.elevation + sSize; + if (dRight >= sLeft && dLeft <= sRight && dTop >= sBottom && dBottom <= sTop) { + star = null; + starTimer = 5000 + Math.random() * 5000; + bonusScore += 25; + say(RUN_CHATTER); + } + } + + for (const obs of obstacles) { + const oLeft = obs.x + 0.45; + const oRight = obs.x + obs.width - 0.45; + if (dRight < oLeft || dLeft > oRight) continue; + + const oBottom = obs.type === "ptero" ? obs.elevation + 0.35 : 0; + const oTop = (obs.type === "ptero" ? obs.elevation : 0) + obs.height - 0.35; + + if (dTop >= oBottom && dBottom <= oTop) { + if (player) { + status = "over"; + overTimer = OVER_AUTO_RESET_MS; + if (score > highScore) highScore = score; + say(CRASH_CHATTER); + } else { + // Autopilot safety net if a resize dropped an obstacle on the dino. + dinoVy = JUMP_VELOCITY; + } + return; + } + } + }; + + return { + resize(nextCols: number, nextRows: number) { + if (nextCols === cols && nextRows === rows) return; + const hadBoard = cols > 0 && rows > 0; + cols = nextCols; + rows = nextRows; + groundRow = computeGroundRow(); + if (!hadBoard) { + boot(); + return; + } + seedClouds(); + }, + + takeControl() { + if (player) return; + player = true; + groundRow = computeGroundRow(); + booted = BOOT_FADE_MS; + startRun(); + }, + + releaseControl() { + if (!player) return; + player = false; + groundRow = computeGroundRow(); + startRun(); + }, + + jump() { + if (!player) return; + if (status === "over") { + if (overTimer <= OVER_AUTO_RESET_MS - OVER_RESTART_GUARD_MS) { + startRun(); + } + return; + } + ducking = false; + if (!triggerJump()) { + jumpBuffer = JUMP_BUFFER_MS; + } + }, + + setDuck(active: boolean) { + if (!player) return; + ducking = active; + }, + + setMode(next: ArcadeMode) { + mode = next; + }, + + mode() { + return mode; + }, + + controlled() { + return player; + }, + + score() { + return score; + }, + + highScore() { + return highScore; + }, + + gameOver() { + return status === "over"; + }, + + step(dt: number) { + if (cols <= 0 || rows <= 0) return; + booted += dt; + runClock += dt; + + if (speech) { + speech.age += dt; + if (speech.age >= SPEECH_HOLD_MS + SPEECH_FADE_MS) speech = null; + } + + for (const puff of dust) puff.age += dt; + dust = dust.filter((puff) => puff.age < puff.life); + + if (status === "over") { + overTimer -= dt; + if (overTimer <= 0) startRun(); + return; + } + + if (!player) { + thinkDino(); + } else if (jumpBuffer > 0) { + jumpBuffer = Math.max(0, jumpBuffer - dt); + if (dinoY <= 0.01) triggerJump(); + } + + const dtSec = Math.min(0.05, dt / 1000); + const speed = currentSpeed(); + const dx = speed * dtSec; + distance += dx; + + score = Math.floor(distance * 0.35) + bonusScore; + if (score > highScore && player) highScore = score; + if (score >= nextMilestone) { + nextMilestone += 100; + if (!speech) say(RUN_CHATTER); + } + + // Vertical jump physics + fast-fall when holding duck in mid-air. + if (dinoY > 0 || dinoVy > 0) { + const gravity = ducking ? GRAVITY * FAST_FALL_MULTIPLIER : GRAVITY; + if (ducking && dinoVy > 0) dinoVy *= 0.6; + dinoY += dinoVy * dtSec; + dinoVy -= gravity * dtSec; + if (dinoY <= 0) { + dinoY = 0; + dinoVy = 0; + spawnDust(2); + if (jumpBuffer > 0) triggerJump(); + } + } else { + dustTimer += dt; + if (dustTimer >= 150) { + dustTimer = 0; + spawnDust(1); + } + } + + for (const puff of dust) { + puff.x -= dx * 0.55; + puff.elevation += dtSec * 1.4; + } + + const skySpan = Math.max(5, groundRow - 8); + for (const cloud of clouds) { + cloud.x -= dx * cloud.speedFactor; + if (cloud.x + cloud.width < -2) { + cloud.x = cols + 2 + Math.random() * 12; + cloud.elevation = 6 + Math.floor(Math.random() * skySpan); + } + } + + for (const obs of obstacles) { + obs.x -= dx; + } + obstacles = obstacles.filter((obs) => obs.x + obs.width > -4); + + const lastObs = obstacles[obstacles.length - 1]; + const tailDist = lastObs ? cols - (lastObs.x + lastObs.width) : Infinity; + if (tailDist >= spawnGap) { + spawnObstacle(); + } + + if (star) { + star.age += dt; + star.x -= dx; + if (star.x < -4) { + star = null; + starTimer = 4500 + Math.random() * 4500; + } + } else { + starTimer -= dt; + if (starTimer <= 0) { + maybeSpawnStar(); + if (!star) starTimer = 1800; + } + } + + checkCollisions(); + }, + + speechBubble(): SpeechBubble | null { + if (!speech) return null; + const fadeIn = Math.min(1, speech.age / SPEECH_FADE_MS); + const fadeOut = Math.max(0, 1 - (speech.age - SPEECH_HOLD_MS) / SPEECH_FADE_MS); + const alpha = Math.min(fadeIn, fadeOut) * bootAlpha(); + const isDucking = ducking && dinoY <= 0.05 && status === "run"; + const dinoH = (isDucking ? 10 : 16) * SPRITE_UNIT; + const headX = dinoX() + (isDucking ? 16 : 12) * SPRITE_UNIT; + const headY = groundRow - dinoY * 0.25 - dinoH; + return { + text: speech.text, + x: headX, + y: headY, + alpha, + }; + }, + + sprites(): DinoSprite[] { + if (cols <= 0 || rows <= 0) return []; + const alpha = bootAlpha(); + const dim = status === "over" ? 0.75 : 1; + const out: DinoSprite[] = []; + + if (star) { + const edge = Math.floor(star.age / STAR_SPIN_MS) % 2 === 1; + const bob = Math.sin(star.age / 180) * 0.35; + out.push({ + kind: edge ? "star-edge" : "star-face", + x: star.x, + y: groundRow - (star.elevation + bob) - 8 * SPRITE_UNIT, + width: 8, + height: 8, + alpha: alpha * 0.9 * dim, + }); + } + + const flap = Math.floor(runClock / WING_FLAP_MS) % 2 === 0; + for (const obs of obstacles) { + if (obs.type === "cactus") { + for (const piece of obs.pieces) { + out.push({ + kind: piece.kind, + x: obs.x + piece.offsetX, + y: groundRow - piece.heightPx * SPRITE_UNIT, + width: piece.widthPx, + height: piece.heightPx, + alpha: alpha * 0.85 * dim, + }); + } + } else { + out.push({ + kind: flap ? "ptero-up" : "ptero-down", + x: obs.x, + y: groundRow - obs.elevation - 12 * SPRITE_UNIT, + width: 16, + height: 12, + alpha: alpha * 0.88 * dim, + }); + } + } + + const stepA = Math.floor(runClock / LEG_STEP_MS) % 2 === 0; + const isDucking = ducking && dinoY <= 0.05 && status === "run"; + const dinoKind: DinoSpriteKind = + status === "over" + ? "dino-crash" + : dinoY > 0.05 + ? "dino-jump" + : isDucking + ? stepA + ? "dino-duck-a" + : "dino-duck-b" + : stepA + ? "dino-run-a" + : "dino-run-b"; + const dinoW = isDucking ? 20 : 16; + const dinoH = isDucking ? 10 : 16; + + out.push({ + kind: dinoKind, + x: dinoX(), + y: groundRow - dinoY - dinoH * SPRITE_UNIT, + width: dinoW, + height: dinoH, + alpha, + }); + + return out; + }, + + fade() { + return bootAlpha(); + }, + + stamp(out: Float32Array, stampCols: number, stampRows: number) { + if (cols <= 0 || rows <= 0) return; + const alpha = bootAlpha(); + const dim = status === "over" ? 0.5 : 1; + + const light = (x: number, y: number, value: number) => { + if (x < 0 || y < 0 || x >= stampCols || y >= stampRows) return; + const index = y * stampCols + x; + if ((out[index] ?? 0) < value) out[index] = value; + }; + + // Clouds drifting through the upper sky. + for (const cloud of clouds) { + const cx = Math.round(cloud.x); + const cy = groundRow - Math.round(cloud.elevation); + for (let dx = 0; dx < cloud.width; dx++) { + light(cx + dx, cy, 0.22 * dim); + if (dx > 0 && dx < cloud.width - 1) { + light(cx + dx, cy - 1, 0.16 * dim); + } + } + } + + // Scrolling horizon line and desert pebbles underneath. + const worldOffset = Math.floor(distance); + for (let x = 0; x < stampCols; x++) { + const wx = x + worldOffset; + const h = groundHash(wx); + // Occasional 1-cell desert ridge or dip along the horizon. + const ridge = h > 0.96 ? -1 : 0; + light(x, groundRow + ridge, 0.42 * dim); + + if (h < 0.22) { + light(x, groundRow + 1, 0.24 * dim); + } else if (h > 0.76 && h < 0.86) { + light(x, groundRow + 2, 0.15 * dim); + } + } + + // Footstep & landing dust puffs. + for (const puff of dust) { + const life = 1 - puff.age / puff.life; + if (life <= 0) continue; + light(Math.round(puff.x), groundRow - 1 - Math.round(puff.elevation), 0.55 * life * dim); + } + + if (alpha >= 0.999) return; + for (let i = 0; i < out.length; i++) out[i]! *= alpha; + }, + }; +} diff --git a/apps/web/src/landing/landing.tsx b/apps/web/src/landing/landing.tsx index 3563a4b6..925fd29d 100644 --- a/apps/web/src/landing/landing.tsx +++ b/apps/web/src/landing/landing.tsx @@ -17,6 +17,7 @@ import { sessionAddress } from "../widgets/session/address"; import { Json, type Place as Where, topics } from "../widgets/session/schema"; import { useOpenElsewhere } from "../wm/open"; import { ModelPicker, ThinkingPicker, useChoice, useModelsNotice } from "@/components/model"; +import { Arcade } from "./arcade"; import { placeAtom, placeLabel, usePickFolder } from "./place"; import { projectsAtom } from "../widgets/session/projects"; @@ -34,6 +35,7 @@ export function Landing() { const openElsewhere = useOpenElsewhere(); const refreshProjects = useAtomRefresh(projectsAtom); const [query, setQuery] = useState(""); + const [playing, setPlaying] = useState(false); const place = useAtomValue(placeAtom); const [pending, setPending] = useState(false); const [error, setError] = useState(); @@ -79,14 +81,18 @@ export function Landing() { return (
-
+ +
-
+

{title}

-
+
{notice !== undefined && }