// Draws a game state. The same state always draws the same frame. import { H, LANTERN, W } from "./game.js"; function text(ctx, s, x, y, size, color = "rgba(255, 255, 255, 0.85)", align = "left", weight = 600) { ctx.font = `${weight} ${size}px Inter, system-ui, sans-serif`; ctx.textAlign = align; ctx.fillStyle = color; ctx.fillText(s, x, y); } export function draw(ctx, game) { const g = ctx.createLinearGradient(0, 0, 0, H); g.addColorStop(0, "#1e1630"); g.addColorStop(1, "#0d0a14"); ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); for (const s of game.sparks) { ctx.fillStyle = "rgba(255, 190, 90, 0.25)"; ctx.fillRect(s.x - 1.5, s.y - 22, 3, 22); ctx.fillStyle = "#ffd27a"; ctx.beginPath(); ctx.arc(s.x, s.y, 6, 0, Math.PI * 2); ctx.fill(); } ctx.fillStyle = "#f4e3c1"; ctx.beginPath(); ctx.roundRect(game.x - LANTERN.halfWidth, LANTERN.y, LANTERN.halfWidth * 2, 14, 7); ctx.fill(); if (game.state === "play") { text(ctx, `Caught ${game.caught}`, 22, 38, 22); text(ctx, "●".repeat(game.lives), W - 22, 38, 20, "#ffd27a", "right"); } else { ctx.fillStyle = "rgba(0, 0, 0, 0.45)"; ctx.fillRect(0, 0, W, H); const title = game.state === "title"; text(ctx, title ? "Doomr" : "Out of sparks", W / 2, 220, 60, "#ffd27a", "center", 800); text( ctx, title ? "Catch the sparks. ← → or point to move." : `${game.caught} caught`, W / 2, 268, 22, undefined, "center", 500, ); text( ctx, title ? "Space or tap to start" : "Space or tap to play again", W / 2, 310, 18, "rgba(255, 255, 255, 0.6)", "center", 500, ); } }