// Draws a game state. Nothing here changes the game: the same state always draws the same frame. import { BEAM, H, LAMP, W, inBeam } from "./game.js"; const SEA_TOP = "#0a1830"; const SEA_BOTTOM = "#04101e"; const LIGHT = "255, 214, 150"; const ROCK = "#1b2733"; const ROCK_LIT = "#4b6075"; const WRECK = "#e5484d"; function sea(ctx, game) { const g = ctx.createLinearGradient(0, 0, 0, H); g.addColorStop(0, SEA_TOP); g.addColorStop(1, SEA_BOTTOM); ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); // Faint swell: short strokes drifting east, placed by a fixed pattern so frames are repeatable. ctx.strokeStyle = "rgba(140, 180, 220, 0.07)"; ctx.lineWidth = 1.5; for (let i = 0; i < 70; i++) { const row = i % 14; const y = 24 + row * 36 + ((i * 7) % 11); const x = ((((i * 173) % W) + game.time * (8 + (i % 5) * 3)) % (W + 60)) - 30; ctx.beginPath(); ctx.moveTo(x, y); ctx.quadraticCurveTo(x + 9, y - 3, x + 18, y); ctx.stroke(); } } function beam(ctx, game) { const range = BEAM.range; ctx.save(); ctx.globalCompositeOperation = "lighter"; const g = ctx.createRadialGradient(LAMP.x, LAMP.y, 6, LAMP.x, LAMP.y, range); g.addColorStop(0, `rgba(${LIGHT}, 0.5)`); g.addColorStop(0.55, `rgba(${LIGHT}, 0.16)`); g.addColorStop(1, `rgba(${LIGHT}, 0)`); ctx.fillStyle = g; ctx.beginPath(); ctx.moveTo(LAMP.x, LAMP.y); ctx.arc(LAMP.x, LAMP.y, range, game.beam - BEAM.halfAngle, game.beam + BEAM.halfAngle); ctx.closePath(); ctx.fill(); ctx.restore(); } function rocks(ctx, game) { for (const r of game.rocks) { const lit = inBeam(game, r.x, r.y); ctx.fillStyle = lit ? ROCK_LIT : ROCK; ctx.beginPath(); // A lumpy outline, the same every frame. for (let i = 0; i <= 10; i++) { const a = (i / 10) * Math.PI * 2; const k = 1 + 0.12 * Math.sin(a * 3 + r.x * 0.1) + 0.06 * Math.cos(a * 5 + r.y * 0.1); const x = r.x + Math.cos(a) * r.r * k; const y = r.y + Math.sin(a) * r.r * k; if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.closePath(); ctx.fill(); ctx.strokeStyle = lit ? "rgba(255, 240, 210, 0.35)" : "rgba(120, 150, 180, 0.18)"; ctx.lineWidth = 1.5; ctx.stroke(); // Surf where the swell breaks on the rock. ctx.strokeStyle = `rgba(200, 225, 255, ${lit ? 0.3 : 0.1})`; ctx.beginPath(); ctx.arc(r.x, r.y, r.r + 5 + Math.sin(game.time * 2 + r.x) * 1.5, 0, Math.PI * 2); ctx.stroke(); } } function ships(ctx, game) { for (const s of game.ships) { ctx.save(); ctx.translate(s.x, s.y); ctx.rotate(s.heading); ctx.fillStyle = s.lit ? "#efe6d2" : "rgba(160, 175, 195, 0.35)"; ctx.beginPath(); ctx.moveTo(12, 0); ctx.quadraticCurveTo(4, -6, -10, -4.5); ctx.lineTo(-10, 4.5); ctx.quadraticCurveTo(4, 6, 12, 0); ctx.fill(); ctx.fillStyle = s.lit ? "#c9b48a" : "rgba(120, 130, 150, 0.35)"; ctx.fillRect(-3, -1.2, 9, 2.4); // The stern lantern: all a ship shows in the dark. ctx.fillStyle = s.lit ? "rgba(255, 210, 120, 0.9)" : "rgba(255, 200, 110, 0.75)"; ctx.beginPath(); ctx.arc(-9, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } } function effects(ctx, game) { for (const e of game.effects) { const t = e.age / e.life; if (e.kind === "wreck") { ctx.strokeStyle = `rgba(229, 72, 77, ${1 - t})`; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(e.x, e.y, 8 + t * 26, 0, Math.PI * 2); ctx.stroke(); ctx.fillStyle = WRECK; for (let i = 0; i < 6; i++) { const a = e.heading + (i / 6) * Math.PI * 2; ctx.globalAlpha = 1 - t; ctx.fillRect(e.x + Math.cos(a) * t * 18, e.y + Math.sin(a) * t * 18, 3, 3); } ctx.globalAlpha = 1; } else if (e.kind === "home") { ctx.fillStyle = `rgba(${LIGHT}, ${1 - t})`; ctx.beginPath(); ctx.arc(e.x, e.y, 3 + t * 10, 0, Math.PI * 2); ctx.fill(); } } } function lighthouse(ctx, game) { ctx.fillStyle = "#121c26"; ctx.beginPath(); ctx.ellipse(LAMP.x, LAMP.y + 14, 46, 22, 0, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "#e9e4da"; ctx.beginPath(); ctx.arc(LAMP.x, LAMP.y, 13, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = "#b23a3a"; ctx.lineWidth = 4; ctx.beginPath(); ctx.arc(LAMP.x, LAMP.y, 9, 0, Math.PI * 2); ctx.stroke(); const glow = ctx.createRadialGradient(LAMP.x, LAMP.y, 0, LAMP.x, LAMP.y, 26); glow.addColorStop(0, `rgba(${LIGHT}, 1)`); glow.addColorStop(1, `rgba(${LIGHT}, 0)`); ctx.fillStyle = glow; ctx.beginPath(); ctx.arc(LAMP.x, LAMP.y, 26, 0, Math.PI * 2); ctx.fill(); // Which way the lamp faces. ctx.fillStyle = `rgba(${LIGHT}, 1)`; ctx.beginPath(); ctx.arc(LAMP.x + Math.cos(game.beam) * 6, LAMP.y + Math.sin(game.beam) * 6, 3.5, 0, Math.PI * 2); ctx.fill(); } function text(ctx, s, x, y, size, opts = {}) { ctx.font = `${opts.weight ?? 600} ${size}px Inter, system-ui, -apple-system, "Segoe UI", sans-serif`; ctx.textAlign = opts.align ?? "left"; ctx.textBaseline = "alphabetic"; ctx.fillStyle = opts.color ?? "rgba(255, 255, 255, 0.85)"; ctx.fillText(s, x, y); } function hud(ctx, game) { text(ctx, `Saved ${game.saved}`, 22, 38, 22); for (let i = 0; i < 3; i++) { const x = W - 30 - i * 26; ctx.beginPath(); ctx.arc(x, 30, 8, 0, Math.PI * 2); if (i < game.lives) { ctx.fillStyle = `rgba(${LIGHT}, 0.95)`; ctx.fill(); } else { ctx.strokeStyle = "rgba(255, 255, 255, 0.35)"; ctx.lineWidth = 2; ctx.stroke(); } } if (game.lives > 3) text(ctx, `+${game.lives - 3}`, W - 104, 36, 16, { align: "right" }); } function veil(ctx, alpha) { ctx.fillStyle = `rgba(2, 8, 16, ${alpha})`; ctx.fillRect(0, 0, W, H); } function titleScreen(ctx) { veil(ctx, 0.45); text(ctx, "BEACON", W / 2, 205, 76, { align: "center", weight: 800, color: `rgba(${LIGHT}, 0.95)` }); text(ctx, "Keep the lamp turning. Ships in your light can see the rocks.", W / 2, 252, 22, { align: "center", weight: 500, }); text(ctx, "← → or point to turn the light · Space or tap to start", W / 2, 300, 18, { align: "center", weight: 500, color: "rgba(255, 255, 255, 0.6)", }); } function overScreen(ctx, game) { veil(ctx, 0.55); text(ctx, "The lamp went dark", W / 2, 215, 52, { align: "center", weight: 800, color: `rgba(${LIGHT}, 0.95)` }); text(ctx, `${game.saved} ship${game.saved === 1 ? "" : "s"} saved`, W / 2, 262, 26, { align: "center", weight: 600 }); text(ctx, "Space or tap to light it again", W / 2, 306, 18, { align: "center", weight: 500, color: "rgba(255, 255, 255, 0.6)", }); } export function draw(ctx, game) { sea(ctx, game); beam(ctx, game); rocks(ctx, game); ships(ctx, game); effects(ctx, game); lighthouse(ctx, game); if (game.state === "play") hud(ctx, game); if (game.state === "title") titleScreen(ctx); if (game.state === "over") overScreen(ctx, game); }