Advent of AI Slop Day 5: Pelican on a Bike
Endless winter-city ride: pelican on a bicycle or reindeer on a unicycle, procedural canvas skyline, garlands to duck, snowmen to hop, fish to hoard.
This commit is contained in:
commit
f2eec57b5e
16 files changed
+2267
No files matched your search
+584
@@ -0,0 +1,584 @@
|
||||
// All canvas drawing for Day 5: winter city, obstacles, pickups, and the two riders.
|
||||
// Everything is drawn with paths — no images, no fonts beyond numbers.
|
||||
|
||||
export const W = 1200;
|
||||
export const H = 600;
|
||||
export const GROUND = 508; // wheel-contact line
|
||||
|
||||
export interface Building {
|
||||
x: number; w: number; h: number;
|
||||
tone: number; // 0..1 darkness
|
||||
seed: number; // window layout seed
|
||||
spire: boolean;
|
||||
}
|
||||
|
||||
export interface Obstacle {
|
||||
kind: 'pothole' | 'snowman' | 'tree' | 'gift' | 'bin' | 'garland';
|
||||
x: number; w: number; h: number; // h = height above ground (garland: box top offset)
|
||||
seed: number;
|
||||
}
|
||||
|
||||
export interface Pickup { kind: 'fish' | 'carrot'; x: number; y: number; taken: boolean }
|
||||
|
||||
export interface Flake { x: number; y: number; r: number; vy: number; sway: number; phase: number }
|
||||
|
||||
// deterministic little PRNG so a building's windows never flicker between frames
|
||||
const rand = (n: number): number => {
|
||||
const x = Math.sin(n * 127.1 + 311.7) * 43758.5453;
|
||||
return x - Math.floor(x);
|
||||
};
|
||||
|
||||
export function makeBuilding(x: number, layer: number): Building {
|
||||
const s = Math.random() * 10000;
|
||||
const hRange = layer === 0 ? [140, 300] : layer === 1 ? [100, 220] : [70, 150];
|
||||
const h = hRange[0]! + Math.random() * (hRange[1]! - hRange[0]!);
|
||||
const w = layer === 0 ? 70 + Math.random() * 90 : layer === 1 ? 60 + Math.random() * 80 : 55 + Math.random() * 70;
|
||||
return { x, w, h, tone: Math.random(), seed: s, spire: Math.random() < 0.16 };
|
||||
}
|
||||
|
||||
export function drawSky(ctx: CRenderingContext, t: number): void {
|
||||
const g = ctx.createLinearGradient(0, 0, 0, GROUND);
|
||||
g.addColorStop(0, '#0c1024');
|
||||
g.addColorStop(0.55, '#1c2748');
|
||||
g.addColorStop(0.85, '#3a3f6b');
|
||||
g.addColorStop(1, '#6b4a63');
|
||||
ctx.fillStyle = g;
|
||||
ctx.fillRect(0, 0, W, GROUND);
|
||||
|
||||
// stars
|
||||
for (let i = 0; i < 70; i++) {
|
||||
const x = rand(i * 3.7) * W;
|
||||
const y = rand(i * 9.1) * GROUND * 0.55;
|
||||
const tw = 0.35 + 0.65 * Math.abs(Math.sin(t * (0.6 + rand(i) * 1.4) + i));
|
||||
ctx.fillStyle = `rgba(240,240,255,${(0.25 + 0.5 * tw) * (1 - y / (GROUND * 0.7))})`;
|
||||
ctx.fillRect(x, y, 2, 2);
|
||||
}
|
||||
|
||||
// moon with halo
|
||||
const mx = 1010, my = 92;
|
||||
const halo = ctx.createRadialGradient(mx, my, 8, mx, my, 60);
|
||||
halo.addColorStop(0, 'rgba(255,244,214,0.55)');
|
||||
halo.addColorStop(1, 'rgba(255,244,214,0)');
|
||||
ctx.fillStyle = halo;
|
||||
ctx.fillRect(mx - 60, my - 60, 120, 120);
|
||||
ctx.fillStyle = '#f8ecc8';
|
||||
ctx.beginPath(); ctx.arc(mx, my, 26, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.fillStyle = 'rgba(200,180,140,0.35)';
|
||||
ctx.beginPath(); ctx.arc(mx - 8, my - 6, 5, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.beginPath(); ctx.arc(mx + 7, my + 8, 7, 0, Math.PI * 2); ctx.fill();
|
||||
}
|
||||
|
||||
export function drawBuildings(ctx: CRenderingContext, list: Building[], layer: number, t: number): void {
|
||||
const baseY = GROUND - (layer === 2 ? 26 : layer === 1 ? 12 : 0);
|
||||
const dark = layer === 0 ? ['#141a33', '#182040'] : layer === 1 ? ['#1d2547', '#232c52'] : ['#2a3358', '#323d66'];
|
||||
for (const b of list) {
|
||||
if (b.x > W + 10 || b.x + b.w < -10) continue;
|
||||
const top = baseY - b.h;
|
||||
ctx.fillStyle = dark[b.tone > 0.5 ? 1 : 0]!;
|
||||
ctx.fillRect(b.x, top, b.w, b.h);
|
||||
if (b.spire) { // little church spire on some rooftops
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(b.x + b.w * 0.5 - 8, top);
|
||||
ctx.lineTo(b.x + b.w * 0.5, top - 34);
|
||||
ctx.lineTo(b.x + b.w * 0.5 + 8, top);
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
}
|
||||
// roof snow
|
||||
ctx.fillStyle = 'rgba(226,234,250,0.85)';
|
||||
ctx.fillRect(b.x - 2, top, b.w + 4, 5);
|
||||
// windows
|
||||
if (layer > 0) {
|
||||
const cols = Math.max(2, Math.floor(b.w / 22));
|
||||
const rows = Math.max(2, Math.floor(b.h / 26));
|
||||
for (let r = 0; r < rows; r++) {
|
||||
for (let c = 0; c < cols; c++) {
|
||||
const v = rand(b.seed + r * 31.3 + c * 7.7);
|
||||
if (v < 0.42) continue;
|
||||
const flick = v > 0.9 ? 0.6 + 0.4 * Math.sin(t * 3 + b.seed + r) : 1;
|
||||
const warm = v > 0.75;
|
||||
ctx.fillStyle = warm
|
||||
? `rgba(255,196,102,${0.75 * flick})`
|
||||
: `rgba(160,200,255,${0.4 * flick})`;
|
||||
ctx.fillRect(b.x + 8 + c * ((b.w - 16) / cols), top + 12 + r * ((b.h - 20) / rows), 8, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function drawStreet(ctx: CRenderingContext, scroll: number): void {
|
||||
// sidewalk
|
||||
ctx.fillStyle = '#cfd8ea';
|
||||
ctx.fillRect(0, GROUND - 26, W, 26);
|
||||
ctx.fillStyle = '#eef3fb';
|
||||
ctx.fillRect(0, GROUND - 26, W, 6);
|
||||
// curb shadow
|
||||
ctx.fillStyle = '#9aa6c0';
|
||||
ctx.fillRect(0, GROUND - 4, W, 4);
|
||||
// road
|
||||
const road = ctx.createLinearGradient(0, GROUND, 0, H);
|
||||
road.addColorStop(0, '#3a4056');
|
||||
road.addColorStop(1, '#232838');
|
||||
ctx.fillStyle = road;
|
||||
ctx.fillRect(0, GROUND, W, H - GROUND);
|
||||
// slushy lane dashes
|
||||
ctx.fillStyle = 'rgba(226,234,250,0.5)';
|
||||
const dash = 46, gap = 58;
|
||||
const off = scroll % (dash + gap);
|
||||
for (let x = -off; x < W; x += dash + gap) {
|
||||
ctx.fillRect(x, GROUND + 44, dash, 6);
|
||||
}
|
||||
// snowbanks on the sidewalk, scrolling with the near layer
|
||||
ctx.fillStyle = '#e8eefb';
|
||||
const bank = 210;
|
||||
const boff = scroll % bank;
|
||||
for (let x = -boff; x < W + bank; x += bank) {
|
||||
const s = rand(Math.floor((scroll + x) / bank) * 13.7);
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(x, GROUND - 6, 40 + s * 30, 9 + s * 5, 0, Math.PI, 0);
|
||||
ctx.fill();
|
||||
}
|
||||
}
|
||||
|
||||
export function drawLamp(ctx: CRenderingContext, x: number, t: number): void {
|
||||
ctx.strokeStyle = '#4a5474';
|
||||
ctx.lineWidth = 5;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, GROUND - 6);
|
||||
ctx.lineTo(x, GROUND - 150);
|
||||
ctx.quadraticCurveTo(x, GROUND - 168, x + 22, GROUND - 168);
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
// lamp head + glow
|
||||
const gx = x + 26, gy = GROUND - 164;
|
||||
const glow = ctx.createRadialGradient(gx, gy, 2, gx, gy, 46);
|
||||
glow.addColorStop(0, `rgba(255,214,140,${0.5 + 0.06 * Math.sin(t * 7 + x)})`);
|
||||
glow.addColorStop(1, 'rgba(255,214,140,0)');
|
||||
ctx.fillStyle = glow;
|
||||
ctx.fillRect(gx - 46, gy - 46, 92, 92);
|
||||
ctx.fillStyle = '#ffdf9e';
|
||||
ctx.beginPath(); ctx.ellipse(gx, gy, 8, 6, 0, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.fillStyle = '#38405c';
|
||||
ctx.fillRect(gx - 10, gy - 12, 20, 6);
|
||||
}
|
||||
|
||||
export function drawGarland(ctx: CRenderingContext, o: Obstacle, t: number): void {
|
||||
const topY = GROUND - 150;
|
||||
const sag = GROUND - 66;
|
||||
const x1 = o.x, x2 = o.x + o.w;
|
||||
// poles
|
||||
ctx.strokeStyle = '#4a5474';
|
||||
ctx.lineWidth = 5;
|
||||
ctx.beginPath(); ctx.moveTo(x1, GROUND - 6); ctx.lineTo(x1, topY); ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(x2, GROUND - 6); ctx.lineTo(x2, topY); ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
// string
|
||||
ctx.strokeStyle = '#69719a';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x1, topY);
|
||||
ctx.quadraticCurveTo((x1 + x2) / 2, sag + 26, x2, topY);
|
||||
ctx.stroke();
|
||||
// bulbs along the quadratic
|
||||
const colors = ['#ff5d5d', '#ffd166', '#5dd06a', '#5da9ff'];
|
||||
const n = 11;
|
||||
for (let i = 1; i < n; i++) {
|
||||
const u = i / n;
|
||||
const bx = (1 - u) * (1 - u) * x1 + 2 * (1 - u) * u * ((x1 + x2) / 2) + u * u * x2;
|
||||
const by = (1 - u) * (1 - u) * topY + 2 * (1 - u) * u * (sag + 26) + u * u * topY;
|
||||
const c = colors[i % colors.length]!;
|
||||
const pulse = 0.65 + 0.35 * Math.sin(t * 4 + i * 1.3);
|
||||
const g = ctx.createRadialGradient(bx, by + 8, 1, bx, by + 8, 14);
|
||||
g.addColorStop(0, c + 'aa');
|
||||
g.addColorStop(1, c + '00');
|
||||
ctx.fillStyle = g;
|
||||
ctx.fillRect(bx - 14, by - 6, 28, 28);
|
||||
ctx.globalAlpha = 0.55 + 0.45 * pulse;
|
||||
ctx.fillStyle = c;
|
||||
ctx.beginPath(); ctx.ellipse(bx, by + 8, 4.5, 6.5, 0, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
}
|
||||
|
||||
export function drawObstacle(ctx: CRenderingContext, o: Obstacle, t: number): void {
|
||||
const g = GROUND;
|
||||
switch (o.kind) {
|
||||
case 'pothole': {
|
||||
ctx.fillStyle = '#161a26';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(o.x + o.w / 2, g + 16, o.w / 2, 10, 0, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.strokeStyle = 'rgba(226,234,250,0.5)';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(o.x + o.w / 2, g + 14, o.w / 2 + 4, 12, 0, Math.PI * 1.05, Math.PI * 1.95);
|
||||
ctx.stroke();
|
||||
break;
|
||||
}
|
||||
case 'snowman': {
|
||||
const cx = o.x + o.w / 2;
|
||||
ctx.fillStyle = '#f2f6ff';
|
||||
ctx.beginPath(); ctx.arc(cx, g - 20, 20, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.beginPath(); ctx.arc(cx, g - 50, 14, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.beginPath(); ctx.arc(cx, g - 72, 10, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.fillStyle = '#20242f';
|
||||
ctx.beginPath(); ctx.arc(cx - 4, g - 74, 1.8, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.beginPath(); ctx.arc(cx + 3, g - 74, 1.8, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.fillStyle = '#ff7f3f'; // carrot nose
|
||||
ctx.beginPath(); ctx.moveTo(cx + 8, g - 72); ctx.lineTo(cx + 20, g - 69); ctx.lineTo(cx + 8, g - 67); ctx.fill();
|
||||
ctx.fillStyle = '#b8232f'; // hat
|
||||
ctx.fillRect(cx - 12, g - 84, 24, 4);
|
||||
ctx.fillRect(cx - 8, g - 96, 16, 13);
|
||||
ctx.strokeStyle = '#7a5a3a'; // twig arms
|
||||
ctx.lineWidth = 2;
|
||||
ctx.beginPath(); ctx.moveTo(cx - 12, g - 52); ctx.lineTo(cx - 26, g - 62); ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(cx + 12, g - 52); ctx.lineTo(cx + 26, g - 60); ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
break;
|
||||
}
|
||||
case 'tree': {
|
||||
const cx = o.x + o.w / 2;
|
||||
ctx.fillStyle = '#5a3a26';
|
||||
ctx.fillRect(cx - 5, g - 18, 10, 18);
|
||||
const green = ['#1e5c3a', '#236b44', '#2a7a4e'];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const y = g - 18 - i * 24;
|
||||
const wHalf = 30 - i * 7;
|
||||
ctx.fillStyle = green[i]!;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(cx - wHalf, y);
|
||||
ctx.lineTo(cx, y - 34);
|
||||
ctx.lineTo(cx + wHalf, y);
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
ctx.fillStyle = 'rgba(232,238,251,0.8)';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(cx - wHalf, y);
|
||||
ctx.lineTo(cx, y - 34);
|
||||
ctx.lineTo(cx + wHalf, y);
|
||||
ctx.lineTo(cx + wHalf - 8, y);
|
||||
ctx.lineTo(cx, y - 26);
|
||||
ctx.lineTo(cx - wHalf + 8, y);
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
}
|
||||
// star
|
||||
ctx.fillStyle = '#ffd166';
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, g - 92, 4, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
break;
|
||||
}
|
||||
case 'gift': {
|
||||
const boxes = [
|
||||
{ dx: 0, w: 46, h: 30, c: '#b8232f', r: '#ffd166' },
|
||||
{ dx: 8, w: 34, h: 24, c: '#2f7d5c', r: '#f2ecdd' },
|
||||
];
|
||||
let y = g;
|
||||
for (const bx of boxes) {
|
||||
y -= bx.h;
|
||||
ctx.fillStyle = bx.c;
|
||||
ctx.fillRect(o.x + bx.dx, y, bx.w, bx.h);
|
||||
ctx.fillStyle = bx.r;
|
||||
ctx.fillRect(o.x + bx.dx + bx.w / 2 - 3, y, 6, bx.h);
|
||||
ctx.fillRect(o.x + bx.dx, y + bx.h / 2 - 2, bx.w, 4);
|
||||
}
|
||||
ctx.strokeStyle = 'rgba(232,238,251,0.7)';
|
||||
ctx.strokeRect(o.x - 1, g - 55, 48, 1);
|
||||
break;
|
||||
}
|
||||
case 'bin': {
|
||||
ctx.fillStyle = '#2f5d4a';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(o.x + 4, g);
|
||||
ctx.lineTo(o.x, g - 52);
|
||||
ctx.lineTo(o.x + 40, g - 52);
|
||||
ctx.lineTo(o.x + 36, g);
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
ctx.fillStyle = '#3d7460';
|
||||
ctx.fillRect(o.x - 3, g - 60, 46, 9);
|
||||
ctx.fillStyle = '#20453a';
|
||||
ctx.fillRect(o.x + 14, g - 66, 12, 6);
|
||||
// overflowing snow bag
|
||||
ctx.fillStyle = '#dfe6f4';
|
||||
ctx.beginPath(); ctx.arc(o.x + 20, g - 66, 10, Math.PI, 0); ctx.fill();
|
||||
break;
|
||||
}
|
||||
case 'garland':
|
||||
drawGarland(ctx, o, t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export function drawPickup(ctx: CRenderingContext, p: Pickup, t: number): void {
|
||||
if (p.taken) return;
|
||||
const bob = Math.sin(t * 3 + p.x * 0.05) * 5;
|
||||
const y = p.y + bob;
|
||||
ctx.save();
|
||||
ctx.translate(p.x, y);
|
||||
const glow = ctx.createRadialGradient(0, 0, 2, 0, 0, 26);
|
||||
glow.addColorStop(0, 'rgba(255,220,150,0.35)');
|
||||
glow.addColorStop(1, 'rgba(255,220,150,0)');
|
||||
ctx.fillStyle = glow;
|
||||
ctx.fillRect(-26, -26, 52, 52);
|
||||
if (p.kind === 'fish') {
|
||||
ctx.rotate(Math.sin(t * 2 + p.x) * 0.12);
|
||||
ctx.fillStyle = '#b8c8dd';
|
||||
ctx.beginPath(); ctx.ellipse(0, 0, 15, 8, 0, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.fillStyle = '#93a7c2';
|
||||
ctx.beginPath(); ctx.moveTo(-13, 0); ctx.lineTo(-22, -7); ctx.lineTo(-22, 7); ctx.closePath(); ctx.fill();
|
||||
ctx.fillStyle = '#20242f';
|
||||
ctx.beginPath(); ctx.arc(7, -2, 1.8, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
|
||||
ctx.beginPath(); ctx.arc(2, 0, 8, -0.6, 0.9); ctx.stroke();
|
||||
} else {
|
||||
ctx.fillStyle = '#ff8c42';
|
||||
ctx.beginPath(); ctx.moveTo(12, -4); ctx.lineTo(-10, 6); ctx.lineTo(-8, 12); ctx.lineTo(12, 2); ctx.closePath(); ctx.fill();
|
||||
ctx.fillStyle = '#3f9d5a';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(11, -3); ctx.quadraticCurveTo(18, -14, 22, -10);
|
||||
ctx.quadraticCurveTo(17, -6, 13, 0); ctx.closePath(); ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(12, 0); ctx.quadraticCurveTo(23, -6, 25, -1);
|
||||
ctx.quadraticCurveTo(18, 1, 13, 3); ctx.closePath(); ctx.fill();
|
||||
}
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
// ---------- riders ----------
|
||||
|
||||
function wheel(ctx: CRenderingContext, cx: number, cy: number, r: number, spin: number): void {
|
||||
ctx.strokeStyle = '#181c28';
|
||||
ctx.lineWidth = 5;
|
||||
ctx.beginPath(); ctx.arc(cx, cy, r, 0, Math.PI * 2); ctx.stroke();
|
||||
ctx.strokeStyle = '#8f9ab8';
|
||||
ctx.lineWidth = 1.6;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const a = spin + (i * Math.PI) / 3;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(cx - Math.cos(a) * r, cy - Math.sin(a) * r);
|
||||
ctx.lineTo(cx + Math.cos(a) * r, cy + Math.sin(a) * r);
|
||||
ctx.stroke();
|
||||
}
|
||||
ctx.fillStyle = '#c9d2e6';
|
||||
ctx.beginPath(); ctx.arc(cx, cy, 3, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.lineWidth = 1;
|
||||
}
|
||||
|
||||
function leg(ctx: CRenderingContext, hipX: number, hipY: number, footX: number, footY: number): void {
|
||||
const kneeX = (hipX + footX) / 2 + 12;
|
||||
const kneeY = (hipY + footY) / 2 + 6;
|
||||
ctx.strokeStyle = '#d9a24a';
|
||||
ctx.lineWidth = 5;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(hipX, hipY);
|
||||
ctx.quadraticCurveTo(kneeX, kneeY, footX, footY);
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
}
|
||||
|
||||
export function drawPelican(ctx: CRenderingContext, x: number, y: number, crank: number, duck: number, t: number): void {
|
||||
// bicycle
|
||||
const wr = 24;
|
||||
const rear = { x: x - 36, y: y - wr };
|
||||
const front = { x: x + 36, y: y - wr };
|
||||
wheel(ctx, rear.x, rear.y, wr, crank);
|
||||
wheel(ctx, front.x, front.y, wr, crank);
|
||||
ctx.strokeStyle = '#c23a3a';
|
||||
ctx.lineWidth = 4;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(rear.x, rear.y); ctx.lineTo(x - 4, y - 52); ctx.lineTo(front.x, front.y); // rear triangle to seat tube
|
||||
ctx.moveTo(x - 4, y - 52); ctx.lineTo(x + 2, y - 26); ctx.lineTo(rear.x, rear.y); // seat stays
|
||||
ctx.moveTo(x + 2, y - 26); ctx.lineTo(front.x - 6, y - 56); // down tube
|
||||
ctx.moveTo(front.x - 6, y - 56); ctx.lineTo(front.x, front.y); // fork
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
// handlebar
|
||||
ctx.strokeStyle = '#39415f';
|
||||
ctx.lineWidth = 3.5;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(front.x - 6, y - 56); ctx.lineTo(front.x + 2, y - 66); ctx.lineTo(front.x + 12, y - 62);
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
// pedals
|
||||
const px = x + 2 + Math.cos(crank * 2) * 13;
|
||||
const py = y - 26 + Math.sin(crank * 2) * 13;
|
||||
const px2 = x + 2 - Math.cos(crank * 2) * 13;
|
||||
const py2 = y - 26 - Math.sin(crank * 2) * 13;
|
||||
ctx.strokeStyle = '#20242f';
|
||||
ctx.lineWidth = 3;
|
||||
ctx.beginPath(); ctx.moveTo(px - 6, py); ctx.lineTo(px + 6, py); ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(px2 - 6, py2); ctx.lineTo(px2 + 6, py2); ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
|
||||
const dip = duck * 20; // 0..1 duck amount
|
||||
const bodyY = y - 78 + dip;
|
||||
const bob = Math.sin(t * 11) * 1.4;
|
||||
|
||||
// legs pedaling (behind body)
|
||||
leg(ctx, x - 6, bodyY + 16, px, py);
|
||||
leg(ctx, x - 8, bodyY + 16, px2, py2);
|
||||
|
||||
// tail
|
||||
ctx.fillStyle = '#e4ddca';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x - 26, bodyY - 4 + bob);
|
||||
ctx.lineTo(x - 44, bodyY - 12 + bob);
|
||||
ctx.lineTo(x - 28, bodyY + 8 + bob);
|
||||
ctx.closePath(); ctx.fill();
|
||||
|
||||
// body
|
||||
ctx.fillStyle = '#f4f1e8';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(x - 4, bodyY + bob, 27, 20, -0.08, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.fillStyle = 'rgba(180,175,155,0.35)';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(x - 6, bodyY + 8 + bob, 22, 11, -0.08, 0, Math.PI);
|
||||
ctx.fill();
|
||||
|
||||
// neck + head (tucked when ducking)
|
||||
const headX = x + 26 - duck * 10;
|
||||
const headY = bodyY - 30 + dip * 1.4 + bob;
|
||||
ctx.strokeStyle = '#f4f1e8';
|
||||
ctx.lineWidth = 12;
|
||||
ctx.lineCap = 'round';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x + 8, bodyY - 6 + bob);
|
||||
ctx.quadraticCurveTo(x + 22 - duck * 14, bodyY - 22 + dip + bob, headX, headY);
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
ctx.lineCap = 'butt';
|
||||
ctx.fillStyle = '#f4f1e8';
|
||||
ctx.beginPath(); ctx.arc(headX, headY, 11, 0, Math.PI * 2); ctx.fill();
|
||||
// crest tuft
|
||||
ctx.strokeStyle = '#e4ddca';
|
||||
ctx.lineWidth = 2.5;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(headX - 4, headY - 9);
|
||||
ctx.quadraticCurveTo(headX - 10, headY - 18, headX - 4, headY - 20);
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
// beak with pouch
|
||||
ctx.fillStyle = '#ff9d3c';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(headX + 6, headY - 5);
|
||||
ctx.lineTo(headX + 46, headY + 1);
|
||||
ctx.lineTo(headX + 8, headY + 6);
|
||||
ctx.closePath(); ctx.fill();
|
||||
ctx.fillStyle = '#f2852a';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(headX + 8, headY + 4);
|
||||
ctx.quadraticCurveTo(headX + 28, headY + 18, headX + 44, headY + 2);
|
||||
ctx.lineTo(headX + 8, headY + 7);
|
||||
ctx.closePath(); ctx.fill();
|
||||
// eye
|
||||
ctx.fillStyle = '#20242f';
|
||||
ctx.beginPath(); ctx.arc(headX + 4, headY - 3, 2.2, 0, Math.PI * 2); ctx.fill();
|
||||
// wing reaching to the bar
|
||||
ctx.fillStyle = '#e8e2d0';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x + 2, bodyY - 6 + bob);
|
||||
ctx.quadraticCurveTo(x + 24, bodyY - 2 + bob, front.x + 8, y - 64);
|
||||
ctx.quadraticCurveTo(x + 20, bodyY + 6 + bob, x + 4, bodyY + 2 + bob);
|
||||
ctx.closePath(); ctx.fill();
|
||||
}
|
||||
|
||||
export function drawReindeer(ctx: CRenderingContext, x: number, y: number, crank: number, duck: number, t: number): void {
|
||||
// unicycle
|
||||
const wr = 27;
|
||||
const hub = { x, y: y - wr };
|
||||
wheel(ctx, hub.x, hub.y, wr, crank);
|
||||
ctx.strokeStyle = '#39415f';
|
||||
ctx.lineWidth = 4;
|
||||
ctx.beginPath(); ctx.moveTo(hub.x, hub.y); ctx.lineTo(x - 2, y - 74); ctx.stroke(); // seat post
|
||||
ctx.beginPath(); ctx.moveTo(hub.x, hub.y); ctx.lineTo(hub.x + 16, hub.y - 6); ctx.stroke(); // fork
|
||||
ctx.lineWidth = 1;
|
||||
// saddle
|
||||
ctx.fillStyle = '#6b3a2a';
|
||||
ctx.beginPath(); ctx.ellipse(x - 4, y - 78, 16, 5, -0.06, 0, Math.PI * 2); ctx.fill();
|
||||
// pedals on the hub
|
||||
const px = hub.x + Math.cos(crank * 2) * 15;
|
||||
const py = hub.y + Math.sin(crank * 2) * 15;
|
||||
const px2 = hub.x - Math.cos(crank * 2) * 15;
|
||||
const py2 = hub.y - Math.sin(crank * 2) * 15;
|
||||
ctx.strokeStyle = '#20242f';
|
||||
ctx.lineWidth = 3;
|
||||
ctx.beginPath(); ctx.moveTo(px - 6, py); ctx.lineTo(px + 6, py); ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(px2 - 6, py2); ctx.lineTo(px2 + 6, py2); ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
|
||||
const dip = duck * 18;
|
||||
const bodyY = y - 100 + dip;
|
||||
const bob = Math.sin(t * 10 + 1) * 1.4;
|
||||
|
||||
// legs
|
||||
ctx.strokeStyle = '#7a4e33';
|
||||
ctx.lineWidth = 6;
|
||||
ctx.lineCap = 'round';
|
||||
ctx.beginPath(); ctx.moveTo(x - 6, bodyY + 14); ctx.quadraticCurveTo(x + 12, bodyY + 40, px, py); ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(x - 10, bodyY + 14); ctx.quadraticCurveTo(x + 6, bodyY + 44, px2, py2); ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
ctx.lineCap = 'butt';
|
||||
|
||||
// tail
|
||||
ctx.fillStyle = '#f2ecdd';
|
||||
ctx.beginPath(); ctx.arc(x - 32, bodyY - 2 + bob, 6, 0, Math.PI * 2); ctx.fill();
|
||||
|
||||
// body
|
||||
ctx.fillStyle = '#8a5a3c';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(x - 8, bodyY + bob, 28, 17, -0.1, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.fillStyle = 'rgba(240,230,210,0.25)';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(x - 10, bodyY + 7 + bob, 22, 8, -0.1, 0, Math.PI);
|
||||
ctx.fill();
|
||||
|
||||
// neck + head
|
||||
const headX = x + 24 - duck * 12;
|
||||
const headY = bodyY - 26 + dip * 1.6 + bob;
|
||||
ctx.strokeStyle = '#8a5a3c';
|
||||
ctx.lineWidth = 11;
|
||||
ctx.lineCap = 'round';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x + 8, bodyY - 4 + bob);
|
||||
ctx.quadraticCurveTo(x + 20 - duck * 12, bodyY - 16 + dip + bob, headX, headY);
|
||||
ctx.stroke();
|
||||
ctx.lineWidth = 1;
|
||||
ctx.lineCap = 'butt';
|
||||
ctx.fillStyle = '#8a5a3c';
|
||||
ctx.beginPath(); ctx.ellipse(headX + 4, headY + 2, 13, 9, 0.15, 0, Math.PI * 2); ctx.fill();
|
||||
// snout
|
||||
ctx.fillStyle = '#6b4228';
|
||||
ctx.beginPath(); ctx.arc(headX + 14, headY + 4, 5, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.fillStyle = '#20242f';
|
||||
ctx.beginPath(); ctx.arc(headX + 17, headY + 3, 1.8, 0, Math.PI * 2); ctx.fill(); // nose
|
||||
ctx.beginPath(); ctx.arc(headX + 6, headY - 2, 2, 0, Math.PI * 2); ctx.fill(); // eye
|
||||
// ear
|
||||
ctx.fillStyle = '#7a4e33';
|
||||
ctx.beginPath(); ctx.ellipse(headX - 6, headY - 4, 6, 3, -0.6, 0, Math.PI * 2); ctx.fill();
|
||||
// antlers
|
||||
ctx.strokeStyle = '#d9c9a3';
|
||||
ctx.lineWidth = 3;
|
||||
ctx.lineCap = 'round';
|
||||
for (const s of [-1, 1]) {
|
||||
const ax = headX - 2 + s * 4;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(ax, headY - 7);
|
||||
ctx.quadraticCurveTo(ax + s * 6, headY - 24, ax + s * 2, headY - 32);
|
||||
ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(ax + s * 4, headY - 18); ctx.lineTo(ax + s * 13, headY - 24); ctx.stroke();
|
||||
ctx.beginPath(); ctx.moveTo(ax + s * 3, headY - 26); ctx.lineTo(ax + s * 11, headY - 33); ctx.stroke();
|
||||
}
|
||||
// red nose glow (Rudolph tax)
|
||||
const rg = ctx.createRadialGradient(headX + 17, headY + 3, 1, headX + 17, headY + 3, 12);
|
||||
rg.addColorStop(0, 'rgba(255,60,60,0.55)');
|
||||
rg.addColorStop(1, 'rgba(255,60,60,0)');
|
||||
ctx.fillStyle = rg;
|
||||
ctx.fillRect(headX + 5, headY - 9, 24, 24);
|
||||
ctx.lineWidth = 1;
|
||||
ctx.lineCap = 'butt';
|
||||
}
|
||||
|
||||
export type CRenderingContext = CanvasRenderingContext2D;
|
||||
@@ -0,0 +1,56 @@
|
||||
// <advent-badge day="1" name="Living fishtank">: the label in the top right corner, "Advent of AI Slop [Day 1]
|
||||
// Living fishtank". Its first part links to the calendar, the rest to the door of the day this was made for.
|
||||
// It draws itself in a shadow root, so its styles and the page's never touch.
|
||||
const site = 'https://advent-of-slop.quesma.com';
|
||||
|
||||
const css = `
|
||||
nav {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
z-index: 2147483647;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0 0.35em;
|
||||
max-width: 100vw;
|
||||
box-sizing: border-box;
|
||||
padding: 0.35rem 0.7rem;
|
||||
border-bottom-left-radius: 0.5rem;
|
||||
background: #b8232f;
|
||||
font: 700 0.8125rem/1.2 system-ui, sans-serif;
|
||||
}
|
||||
a {
|
||||
color: #f6ecd6;
|
||||
text-decoration: none;
|
||||
}
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
`;
|
||||
|
||||
const link = (href: string, text: string): HTMLAnchorElement => {
|
||||
const anchor = document.createElement('a');
|
||||
anchor.href = href;
|
||||
anchor.textContent = text;
|
||||
return anchor;
|
||||
};
|
||||
|
||||
class AdventBadge extends HTMLElement {
|
||||
connectedCallback(): void {
|
||||
const day = Number(this.getAttribute('day'));
|
||||
if (!Number.isInteger(day) || day < 1 || day > 24) {
|
||||
throw new Error(`<advent-badge> needs day="1" to day="24", got ${this.getAttribute('day')}`);
|
||||
}
|
||||
const name = this.getAttribute('name');
|
||||
if (name === null || name.trim() === '') {
|
||||
throw new Error('<advent-badge> needs name="...", the title of its day, such as name="Living fishtank"');
|
||||
}
|
||||
const style = document.createElement('style');
|
||||
style.textContent = css;
|
||||
const label = document.createElement('nav');
|
||||
label.append(link(`${site}/`, 'Advent of AI Slop'), link(`${site}/day/${day}/`, `[Day ${day}] ${name}`));
|
||||
this.attachShadow({ mode: 'open' }).append(style, label);
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define('advent-badge', AdventBadge);
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
// Core game: world state, physics, spawning, collisions, rendering order.
|
||||
import {
|
||||
W, H, GROUND,
|
||||
type Building, type Obstacle, type Pickup, type Flake,
|
||||
makeBuilding, drawSky, drawBuildings, drawStreet, drawLamp,
|
||||
drawObstacle, drawPickup, drawPelican, drawReindeer,
|
||||
} from './art';
|
||||
|
||||
export type CharId = 'pelican' | 'reindeer';
|
||||
export type Phase = 'menu' | 'playing' | 'crashed' | 'paused';
|
||||
|
||||
interface CharSpec {
|
||||
v0: number; accel: number; vmax: number;
|
||||
jumpV: number; jumps: number;
|
||||
standH: number; duckH: number;
|
||||
}
|
||||
|
||||
const SPECS: Record<CharId, CharSpec> = {
|
||||
pelican: { v0: 330, accel: 9, vmax: 700, jumpV: 980, jumps: 1, standH: 118, duckH: 66 },
|
||||
reindeer: { v0: 300, accel: 7, vmax: 640, jumpV: 850, jumps: 2, standH: 132, duckH: 62 },
|
||||
};
|
||||
|
||||
const GRAVITY = 2600;
|
||||
const RIDER_X = 190;
|
||||
const RIDER_W = 84;
|
||||
|
||||
interface Rider {
|
||||
y: number; // wheel-contact line
|
||||
vy: number;
|
||||
onGround: boolean;
|
||||
jumpsLeft: number;
|
||||
duck: number; // eased 0..1
|
||||
crank: number; // pedal angle
|
||||
crashT: number;
|
||||
}
|
||||
|
||||
export interface HudState {
|
||||
score: number; meters: number; kmh: number; fish: number; best: number;
|
||||
}
|
||||
|
||||
const rand = (a: number, b: number): number => a + Math.random() * (b - a);
|
||||
|
||||
export class Game {
|
||||
phase: Phase = 'menu';
|
||||
char: CharId = 'pelican';
|
||||
time = 0;
|
||||
scroll = 0;
|
||||
speed = 0;
|
||||
pickupsGot = 0;
|
||||
best = 0;
|
||||
|
||||
private spec: CharSpec = SPECS.pelican;
|
||||
private rider: Rider = this.freshRider();
|
||||
private far: Building[] = [];
|
||||
private mid: Building[] = [];
|
||||
private near: Building[] = [];
|
||||
private lamps: number[] = [];
|
||||
private obstacles: Obstacle[] = [];
|
||||
private pickups: Pickup[] = [];
|
||||
private flakes: Flake[] = [];
|
||||
private distSinceSpawn = 1e9;
|
||||
private distSincePickup = 0;
|
||||
private garlandCooldown = 0;
|
||||
private score = 0;
|
||||
private onHud: (h: HudState) => void;
|
||||
private onCrash: (score: number, meters: number, fish: number, best: number, isNewBest: boolean) => void;
|
||||
|
||||
constructor(hud: (h: HudState) => void, crash: (s: number, m: number, f: number, b: number, n: boolean) => void) {
|
||||
this.onHud = hud;
|
||||
this.onCrash = crash;
|
||||
this.buildLayer();
|
||||
let lx = 60;
|
||||
while (lx < W + 300) { this.lamps.push(lx); lx += 300; }
|
||||
for (let i = 0; i < 130; i++) {
|
||||
this.flakes.push({
|
||||
x: Math.random() * W, y: Math.random() * H,
|
||||
r: rand(1, 3), vy: rand(26, 70), sway: rand(10, 30), phase: Math.random() * Math.PI * 2,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private buildLayer(): void {
|
||||
for (const [layer, list] of [[0, this.far], [1, this.mid], [2, this.near]] as const) {
|
||||
let x = -40;
|
||||
while (x < W + 200) {
|
||||
const b = makeBuilding(x, layer);
|
||||
list.push(b);
|
||||
x += b.w + rand(6, 40);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private freshRider(): Rider {
|
||||
return { y: GROUND, vy: 0, onGround: true, jumpsLeft: 1, duck: 0, crank: 0, crashT: 0 };
|
||||
}
|
||||
|
||||
setChar(c: CharId, best: number): void {
|
||||
this.char = c;
|
||||
this.spec = SPECS[c];
|
||||
this.best = best;
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.phase = 'playing';
|
||||
this.time = 0;
|
||||
this.scroll = 0;
|
||||
this.speed = this.spec.v0;
|
||||
this.rider = this.freshRider();
|
||||
this.rider.jumpsLeft = this.spec.jumps;
|
||||
this.obstacles = [];
|
||||
this.pickups = [];
|
||||
this.pickupsGot = 0;
|
||||
this.score = 0;
|
||||
this.distSinceSpawn = 600; // first obstacle comes after a calm stretch
|
||||
this.distSincePickup = 0;
|
||||
this.garlandCooldown = 3;
|
||||
}
|
||||
|
||||
jump(): void {
|
||||
if (this.phase !== 'playing') return;
|
||||
const r = this.rider;
|
||||
if (r.onGround) {
|
||||
r.vy = -this.spec.jumpV;
|
||||
r.onGround = false;
|
||||
r.jumpsLeft = this.spec.jumps - 1;
|
||||
} else if (r.jumpsLeft > 0) {
|
||||
r.vy = -this.spec.jumpV * 0.88;
|
||||
r.jumpsLeft -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
setDuck(on: boolean): void {
|
||||
this.duckOn = on;
|
||||
}
|
||||
private duckOn = false;
|
||||
|
||||
get crashDone(): boolean { return this.rider.crashT > 1.15; }
|
||||
|
||||
pause(on: boolean): void {
|
||||
if (on && this.phase === 'playing') this.phase = 'paused';
|
||||
else if (!on && this.phase === 'paused') this.phase = 'playing';
|
||||
}
|
||||
|
||||
get meters(): number { return Math.floor(this.scroll / 40); }
|
||||
get kmh(): number { return Math.round(this.speed * 0.065); }
|
||||
get scoreNow(): number { return this.score; }
|
||||
|
||||
update(dt: number): void {
|
||||
this.time += dt;
|
||||
// snow always falls, even in the menu
|
||||
for (const f of this.flakes) {
|
||||
f.y += f.vy * dt;
|
||||
f.x -= (this.phase === 'playing' ? this.speed * 0.12 : 18) * dt + Math.sin(this.time * 2 + f.phase) * f.sway * dt;
|
||||
if (f.y > H) { f.y = -4; f.x = Math.random() * W; }
|
||||
if (f.x < -4) f.x = W + 4;
|
||||
}
|
||||
if (this.phase !== 'playing' && this.phase !== 'crashed') { this.pushHud(); return; }
|
||||
|
||||
const r = this.rider;
|
||||
if (this.phase === 'crashed') {
|
||||
r.crashT += dt;
|
||||
r.vy += GRAVITY * dt;
|
||||
r.y = Math.min(GROUND, r.y + r.vy * dt);
|
||||
return;
|
||||
}
|
||||
|
||||
// world speed ramps up
|
||||
this.speed = Math.min(this.spec.vmax, this.spec.v0 + this.spec.accel * this.time);
|
||||
const dx = this.speed * dt;
|
||||
this.scroll += dx;
|
||||
r.crank += dx * 0.045;
|
||||
|
||||
// parallax layers recycle
|
||||
const par = [0.22, 0.45, 0.72];
|
||||
for (const [i, list] of [[0, this.far], [1, this.mid], [2, this.near]] as const) {
|
||||
for (const b of list) b.x -= dx * par[i]!;
|
||||
let right = 0;
|
||||
for (const b of list) right = Math.max(right, b.x + b.w);
|
||||
for (const b of list) {
|
||||
if (b.x + b.w < -60) {
|
||||
b.x = right + rand(6, 40);
|
||||
const nb = makeBuilding(b.x, i);
|
||||
b.w = nb.w; b.h = nb.h; b.tone = nb.tone; b.seed = nb.seed; b.spire = nb.spire;
|
||||
right = b.x + b.w;
|
||||
}
|
||||
}
|
||||
}
|
||||
// lamps at street speed
|
||||
for (let i = 0; i < this.lamps.length; i++) {
|
||||
this.lamps[i]! -= dx;
|
||||
if (this.lamps[i]! < -40) this.lamps[i]! = Math.max(...this.lamps) + 300;
|
||||
}
|
||||
|
||||
// rider physics
|
||||
const duckTarget = this.duckOn && r.onGround ? 1 : this.duckOn ? 0.5 : 0;
|
||||
r.duck += (duckTarget - r.duck) * Math.min(1, dt * 14);
|
||||
const g = this.duckOn && !r.onGround ? GRAVITY * 1.55 : GRAVITY;
|
||||
r.vy += g * dt;
|
||||
r.y += r.vy * dt;
|
||||
if (r.y >= GROUND) { r.y = GROUND; r.vy = 0; r.onGround = true; r.jumpsLeft = this.spec.jumps; }
|
||||
|
||||
// spawn obstacles by distance travelled
|
||||
this.distSinceSpawn += dx;
|
||||
const minGap = 240 + this.speed * 0.42;
|
||||
if (this.distSinceSpawn > minGap * rand(0.85, 1.6)) {
|
||||
this.distSinceSpawn = 0;
|
||||
this.garlandCooldown -= 1;
|
||||
const kinds: Obstacle['kind'][] = ['pothole', 'snowman', 'tree', 'gift', 'bin'];
|
||||
if (this.garlandCooldown <= 0 && this.time > 12) {
|
||||
this.garlandCooldown = 5;
|
||||
this.obstacles.push({ kind: 'garland', x: W + 60, w: 230, h: 0, seed: Math.random() });
|
||||
} else {
|
||||
const kind = kinds[Math.floor(Math.random() * kinds.length)]!;
|
||||
const dims: Record<string, [number, number]> = {
|
||||
pothole: [64, 16], snowman: [40, 96], tree: [60, 92], gift: [46, 54], bin: [40, 66],
|
||||
};
|
||||
const [w, h] = dims[kind]!;
|
||||
this.obstacles.push({ kind, x: W + 60, w, h, seed: Math.random() });
|
||||
}
|
||||
}
|
||||
|
||||
// pickups: alternate ground-level and jump-height arcs
|
||||
this.distSincePickup += dx;
|
||||
if (this.distSincePickup > 200) {
|
||||
this.distSincePickup = 0;
|
||||
const kind = this.char === 'pelican' ? 'fish' : 'carrot';
|
||||
const high = Math.random() < 0.45;
|
||||
const y = high ? GROUND - rand(120, 175) : GROUND - 30;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
this.pickups.push({
|
||||
kind, x: W + 80 + i * 42,
|
||||
y: high ? y - Math.sin((i / 2) * Math.PI) * 26 : y,
|
||||
taken: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// move + collide
|
||||
const riderTop = r.y - (this.spec.standH - (this.spec.standH - this.spec.duckH) * r.duck);
|
||||
const riderLeft = RIDER_X - RIDER_W / 2;
|
||||
const riderRight = RIDER_X + RIDER_W / 2 - 14;
|
||||
for (const o of this.obstacles) {
|
||||
o.x -= dx;
|
||||
if (o.kind === 'garland') {
|
||||
const top = GROUND - 150, bottom = GROUND - 70;
|
||||
if (riderRight > o.x + 14 && riderLeft < o.x + o.w - 14 && riderTop < bottom && r.y > top) this.crash();
|
||||
} else if (o.kind !== 'pothole') {
|
||||
const oTop = GROUND - o.h;
|
||||
if (riderRight > o.x + 6 && riderLeft < o.x + o.w - 6 && r.y > oTop + 8) this.crash();
|
||||
} else {
|
||||
// pothole: only punishes if the wheel is on the ground while over it
|
||||
if (riderRight > o.x + 10 && riderLeft + 20 < o.x + o.w - 10 && r.onGround) this.crash();
|
||||
}
|
||||
}
|
||||
this.obstacles = this.obstacles.filter((o) => o.x + o.w > -80);
|
||||
|
||||
for (const p of this.pickups) {
|
||||
p.x -= dx;
|
||||
if (!p.taken && Math.abs(p.x - RIDER_X) < 34 && Math.abs(p.y - (r.y - 70)) < 52) {
|
||||
p.taken = true;
|
||||
this.pickupsGot += 1;
|
||||
this.score += 50;
|
||||
}
|
||||
}
|
||||
this.pickups = this.pickups.filter((p) => p.x > -40);
|
||||
|
||||
this.score = Math.max(this.score, 0);
|
||||
this.score += dx * 0.1; // distance points trickle in
|
||||
this.pushHud();
|
||||
}
|
||||
|
||||
private crash(): void {
|
||||
if (this.phase !== 'playing') return;
|
||||
this.phase = 'crashed';
|
||||
this.rider.crashT = 0;
|
||||
this.rider.vy = -420;
|
||||
const finalScore = Math.floor(this.score);
|
||||
const isNew = finalScore > this.best;
|
||||
this.onCrash(finalScore, this.meters, this.pickupsGot, Math.max(this.best, finalScore), isNew);
|
||||
}
|
||||
|
||||
private pushHud(): void {
|
||||
this.onHud({
|
||||
score: Math.floor(this.score),
|
||||
meters: this.meters,
|
||||
kmh: this.kmh,
|
||||
fish: this.pickupsGot,
|
||||
best: this.best,
|
||||
});
|
||||
}
|
||||
|
||||
render(ctx: CanvasRenderingContext2D): void {
|
||||
drawSky(ctx, this.time);
|
||||
drawBuildings(ctx, this.far, 0, this.time);
|
||||
drawBuildings(ctx, this.mid, 1, this.time);
|
||||
drawBuildings(ctx, this.near, 2, this.time);
|
||||
drawStreet(ctx, this.scroll);
|
||||
for (const lx of this.lamps) if (lx > -60 && lx < W + 60) drawLamp(ctx, lx, this.time);
|
||||
for (const o of this.obstacles) if (o.kind === 'garland') drawObstacle(ctx, o, this.time);
|
||||
for (const o of this.obstacles) if (o.kind !== 'garland') drawObstacle(ctx, o, this.time);
|
||||
for (const p of this.pickups) drawPickup(ctx, p, this.time);
|
||||
|
||||
// rider (with a crash tumble)
|
||||
const r = this.rider;
|
||||
ctx.save();
|
||||
if (this.phase === 'crashed') {
|
||||
ctx.translate(RIDER_X, r.y - 60);
|
||||
ctx.rotate(r.crashT * 5.2);
|
||||
ctx.translate(-RIDER_X, -(r.y - 60));
|
||||
// impact stars
|
||||
ctx.fillStyle = '#ffd166';
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const a = (i / 6) * Math.PI * 2 + r.crashT * 3;
|
||||
const d = 40 + r.crashT * 60;
|
||||
ctx.beginPath();
|
||||
ctx.arc(RIDER_X + Math.cos(a) * d, r.y - 70 + Math.sin(a) * d * 0.6, 4 - r.crashT * 2, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}
|
||||
}
|
||||
if (this.char === 'pelican') drawPelican(ctx, RIDER_X, r.y, r.crank, r.duck, this.time);
|
||||
else drawReindeer(ctx, RIDER_X, r.y, r.crank, r.duck, this.time);
|
||||
ctx.restore();
|
||||
|
||||
// snow on top of everything
|
||||
ctx.fillStyle = 'rgba(240,245,255,0.9)';
|
||||
for (const f of this.flakes) {
|
||||
ctx.globalAlpha = 0.35 + f.r / 4;
|
||||
ctx.beginPath(); ctx.arc(f.x, f.y, f.r, 0, Math.PI * 2); ctx.fill();
|
||||
}
|
||||
ctx.globalAlpha = 1;
|
||||
|
||||
// soft vignette
|
||||
const v = ctx.createRadialGradient(W / 2, H / 2, H * 0.55, W / 2, H / 2, H * 1.05);
|
||||
v.addColorStop(0, 'rgba(0,0,0,0)');
|
||||
v.addColorStop(1, 'rgba(5,8,18,0.5)');
|
||||
ctx.fillStyle = v;
|
||||
ctx.fillRect(0, 0, W, H);
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
import './style.css';
|
||||
import { Game, type CharId, type HudState } from './game';
|
||||
|
||||
const canvas = document.getElementById('game') as HTMLCanvasElement | null;
|
||||
const maybeCtx = canvas?.getContext('2d');
|
||||
if (!canvas || !maybeCtx) throw new Error('canvas #game missing');
|
||||
const ctx: CanvasRenderingContext2D = maybeCtx;
|
||||
|
||||
const el = (id: string): HTMLElement => {
|
||||
const e = document.getElementById(id);
|
||||
if (!e) throw new Error(`missing #${id}`);
|
||||
return e;
|
||||
};
|
||||
|
||||
const hudScore = el('hud-score'), hudDist = el('hud-dist'), hudSpeed = el('hud-speed'),
|
||||
hudFish = el('hud-fish'), hudBest = el('hud-best');
|
||||
const menu = el('menu'), pauseOv = el('pause'), gameover = el('gameover');
|
||||
const finalScore = el('final-score'), finalDist = el('final-dist'), finalFish = el('final-fish'),
|
||||
bestLine = el('best-line'), crashLine = el('crash-line');
|
||||
|
||||
const bestKey = (c: CharId): string => `aos5-best-${c}`;
|
||||
const loadBest = (c: CharId): number => Number(localStorage.getItem(bestKey(c)) ?? '0') || 0;
|
||||
|
||||
const game = new Game(
|
||||
(h: HudState) => {
|
||||
hudScore.textContent = String(h.score);
|
||||
hudDist.textContent = String(h.meters);
|
||||
hudSpeed.textContent = String(h.kmh);
|
||||
hudFish.textContent = String(h.fish);
|
||||
hudBest.textContent = String(h.best);
|
||||
},
|
||||
(score, meters, fish, best, isNew) => {
|
||||
finalScore.textContent = String(score);
|
||||
finalDist.textContent = String(meters);
|
||||
finalFish.textContent = String(fish);
|
||||
if (isNew) {
|
||||
localStorage.setItem(bestKey(game.char), String(best));
|
||||
bestLine.textContent = '★ new personal best!';
|
||||
bestLine.classList.add('new');
|
||||
} else {
|
||||
bestLine.textContent = `best: ${best}`;
|
||||
bestLine.classList.remove('new');
|
||||
}
|
||||
const lines = game.char === 'pelican'
|
||||
? ['Squawk.', 'Honk. That was not a ramp.', 'The fish saw everything.']
|
||||
: ['Clatter.', 'The unicycle disagrees.', 'Rudolph needs a moment.'];
|
||||
crashLine.textContent = lines[Math.floor(Math.random() * lines.length)]!;
|
||||
gameover.classList.remove('hidden');
|
||||
hudBest.textContent = String(best);
|
||||
},
|
||||
);
|
||||
|
||||
function selectChar(c: CharId): void {
|
||||
game.setChar(c, loadBest(c));
|
||||
hudBest.textContent = String(loadBest(c));
|
||||
game.start();
|
||||
menu.classList.add('hidden');
|
||||
gameover.classList.add('hidden');
|
||||
}
|
||||
|
||||
for (const card of document.querySelectorAll<HTMLButtonElement>('#cards .card')) {
|
||||
card.addEventListener('click', () => {
|
||||
const c = card.dataset['char'];
|
||||
if (c === 'pelican' || c === 'reindeer') selectChar(c);
|
||||
});
|
||||
}
|
||||
|
||||
el('restart-btn').addEventListener('click', () => selectChar(game.char));
|
||||
el('menu-btn').addEventListener('click', () => {
|
||||
gameover.classList.add('hidden');
|
||||
menu.classList.remove('hidden');
|
||||
game.phase = 'menu';
|
||||
});
|
||||
|
||||
const JUMP_KEYS = new Set(['Space', 'ArrowUp', 'KeyW']);
|
||||
const DUCK_KEYS = new Set(['ArrowDown', 'KeyS']);
|
||||
|
||||
window.addEventListener('keydown', (e) => {
|
||||
if (e.repeat) return;
|
||||
if (JUMP_KEYS.has(e.code)) {
|
||||
e.preventDefault();
|
||||
if (game.phase === 'menu' && !menu.classList.contains('hidden')) return; // menu needs a pick
|
||||
if (game.phase === 'crashed' && game.crashDone) selectChar(game.char);
|
||||
game.jump();
|
||||
} else if (DUCK_KEYS.has(e.code)) {
|
||||
e.preventDefault();
|
||||
game.setDuck(true);
|
||||
} else if (e.code === 'KeyR' && (game.phase === 'crashed' || game.phase === 'playing')) {
|
||||
selectChar(game.char);
|
||||
} else if (e.code === 'KeyP' || e.code === 'Escape') {
|
||||
game.pause(game.phase === 'playing');
|
||||
pauseOv.classList.toggle('hidden', game.phase !== 'paused');
|
||||
} else if (game.phase === 'menu' && !menu.classList.contains('hidden')) {
|
||||
if (e.code === 'Digit1') selectChar('pelican');
|
||||
if (e.code === 'Digit2') selectChar('reindeer');
|
||||
}
|
||||
});
|
||||
window.addEventListener('keyup', (e) => {
|
||||
if (DUCK_KEYS.has(e.code)) game.setDuck(false);
|
||||
});
|
||||
|
||||
canvas.addEventListener('pointerdown', (e) => {
|
||||
e.preventDefault();
|
||||
if (game.phase === 'playing') game.jump();
|
||||
});
|
||||
|
||||
// main loop
|
||||
let last = performance.now();
|
||||
function frame(now: number): void {
|
||||
const dt = Math.min(0.033, (now - last) / 1000);
|
||||
last = now;
|
||||
game.update(dt);
|
||||
game.render(ctx);
|
||||
requestAnimationFrame(frame);
|
||||
}
|
||||
requestAnimationFrame(frame);
|
||||
|
||||
// expose for the smoke test
|
||||
(window as unknown as Record<string, unknown>)['__aos5'] = game;
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
:root {
|
||||
--bg: #10131f;
|
||||
--panel: #1b2032;
|
||||
--ink: #f2ecdd;
|
||||
--dim: #9aa3bd;
|
||||
--accent: #ffb648;
|
||||
--red: #b8232f;
|
||||
--green: #2f7d5c;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
background:
|
||||
radial-gradient(1200px 500px at 50% -10%, #232c4a 0%, transparent 60%),
|
||||
var(--bg);
|
||||
color: var(--ink);
|
||||
font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
|
||||
}
|
||||
|
||||
#app {
|
||||
max-width: 1240px;
|
||||
margin: 0 auto;
|
||||
padding: 1.2rem 1rem 2.5rem;
|
||||
}
|
||||
|
||||
header { text-align: center; margin-bottom: 0.6rem; }
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: clamp(1.6rem, 4vw, 2.4rem);
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
h1::after { content: " 🚲"; }
|
||||
|
||||
.tagline { margin: 0.15rem 0 0; color: var(--dim); font-style: italic; }
|
||||
|
||||
#hud {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
gap: 0.45rem;
|
||||
margin: 0.7rem 0 0.8rem;
|
||||
}
|
||||
|
||||
.chip {
|
||||
background: var(--panel);
|
||||
border: 1px solid #2c3450;
|
||||
border-radius: 999px;
|
||||
padding: 0.25rem 0.8rem;
|
||||
font-size: 0.85rem;
|
||||
color: var(--dim);
|
||||
}
|
||||
.chip b { color: var(--ink); font-variant-numeric: tabular-nums; }
|
||||
.chip.best b { color: var(--accent); }
|
||||
|
||||
#stage {
|
||||
position: relative;
|
||||
border-radius: 14px;
|
||||
overflow: hidden;
|
||||
border: 1px solid #2c3450;
|
||||
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
|
||||
background: #0b0e18;
|
||||
}
|
||||
|
||||
canvas {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.overlay {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: rgba(10, 13, 24, 0.62);
|
||||
backdrop-filter: blur(3px);
|
||||
}
|
||||
.overlay.hidden { display: none; }
|
||||
|
||||
.panel {
|
||||
background: var(--panel);
|
||||
border: 1px solid #39415f;
|
||||
border-radius: 14px;
|
||||
padding: 1.4rem 1.8rem;
|
||||
text-align: center;
|
||||
max-width: 640px;
|
||||
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.panel h2 { margin: 0 0 0.9rem; font-size: 1.5rem; }
|
||||
|
||||
#cards { display: flex; gap: 0.8rem; justify-content: center; flex-wrap: wrap; }
|
||||
|
||||
.card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.3rem;
|
||||
width: 250px;
|
||||
padding: 0.9rem 1rem;
|
||||
background: #232a42;
|
||||
color: var(--ink);
|
||||
border: 1px solid #39415f;
|
||||
border-radius: 12px;
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
text-align: center;
|
||||
transition: transform 0.12s, border-color 0.12s;
|
||||
}
|
||||
.card:hover { transform: translateY(-3px); border-color: var(--accent); }
|
||||
.card .glyph { font-size: 1.9rem; }
|
||||
.card b { font-size: 1.02rem; }
|
||||
.card small { color: var(--dim); }
|
||||
|
||||
.controls { color: var(--dim); font-size: 0.88rem; margin: 1rem 0 0; }
|
||||
|
||||
kbd {
|
||||
background: #2c3450;
|
||||
border: 1px solid #465074;
|
||||
border-bottom-width: 2px;
|
||||
border-radius: 5px;
|
||||
padding: 0.05em 0.45em;
|
||||
font: 0.8em ui-monospace, monospace;
|
||||
}
|
||||
|
||||
.score-line { margin: 0.2rem 0; font-size: 1.05rem; }
|
||||
.score-line b { color: var(--accent); font-size: 1.3rem; }
|
||||
.best-line { color: var(--dim); margin: 0.2rem 0 1rem; min-height: 1.2em; }
|
||||
.best-line.new { color: var(--accent); font-weight: 700; }
|
||||
|
||||
#restart-btn, #menu-btn {
|
||||
font: inherit;
|
||||
border-radius: 10px;
|
||||
padding: 0.5rem 1.1rem;
|
||||
cursor: pointer;
|
||||
margin: 0 0.3rem;
|
||||
}
|
||||
#restart-btn {
|
||||
background: var(--red);
|
||||
border: 1px solid #d4374a;
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
}
|
||||
#restart-btn:hover { filter: brightness(1.15); }
|
||||
#menu-btn { background: transparent; border: 1px solid #465074; color: var(--dim); }
|
||||
#menu-btn:hover { color: var(--ink); }
|
||||
|
||||
.hint {
|
||||
text-align: center;
|
||||
color: var(--dim);
|
||||
font-size: 0.85rem;
|
||||
margin: 0.8rem 0 0;
|
||||
}
|
||||
Reference in new issue
Block a user