// The page: canvas, input and the loop. With ?shot= in the URL it draws one deterministic frame // instead, for the review pack's screenshots: // ?shot=title&seed=7 the title screen, after a few seconds of the sea running // ?shot=play&seed=7&t=25 25 seconds into a game the autopilot plays // ?shot=over&seed=7&t=40 the game-over screen after 40 seconds // lives=N starts with N lives. window.atelierReady turns true once the frame is drawn. import { autopilot } from "./bot.js"; import { DT, H, LAMP, W, createGame, simulate, start, step } from "./game.js"; import { draw } from "./render.js"; const params = new URLSearchParams(location.search); const shot = params.get("shot"); const seed = Number(params.get("seed")) || (shot ? 1 : Math.floor(Math.random() * 1e9)); const lives = params.has("lives") ? Number(params.get("lives")) : undefined; const canvas = document.getElementById("sea"); const ctx = canvas.getContext("2d"); function fit() { const scale = window.devicePixelRatio || 1; canvas.width = W * scale; canvas.height = H * scale; ctx.setTransform(scale, 0, 0, scale, 0, 0); } fit(); let game = createGame(seed, { lives }); if (shot) { if (shot === "title") simulate(game, 4, autopilot); else { start(game); simulate(game, Number(params.get("t") ?? 20), (g) => (g.state === "play" ? autopilot(g) : {})); if (shot === "over") { game.lives = 0; game.state = "over"; } } draw(ctx, game); window.atelierReady = true; } else { const keys = new Set(); let pointer = null; addEventListener("keydown", (e) => { if (["ArrowLeft", "ArrowRight", "a", "d", " "].includes(e.key)) e.preventDefault(); keys.add(e.key); if (e.key === " " || e.key === "Enter") begin(); }); addEventListener("keyup", (e) => keys.delete(e.key)); const aimAt = (e) => { const rect = canvas.getBoundingClientRect(); const x = ((e.clientX - rect.left) / rect.width) * W; const y = ((e.clientY - rect.top) / rect.height) * H; pointer = Math.atan2(y - LAMP.y, x - LAMP.x); }; canvas.addEventListener("pointermove", aimAt); canvas.addEventListener("pointerdown", (e) => { aimAt(e); begin(); }); function begin() { if (game.state === "title") start(game); else if (game.state === "over") game = start(createGame(Math.floor(Math.random() * 1e9))); } function input() { if (game.state === "title") return autopilot(game); const turn = (keys.has("ArrowRight") || keys.has("d") ? 1 : 0) - (keys.has("ArrowLeft") || keys.has("a") ? 1 : 0); if (turn) { pointer = null; return { turn }; } return pointer === null ? {} : { aim: pointer }; } let last = performance.now(); let carry = 0; function frame(now) { carry = Math.min(0.25, carry + (now - last) / 1000); last = now; while (carry >= DT) { step(game, input()); carry -= DT; } draw(ctx, game); requestAnimationFrame(frame); } draw(ctx, game); window.atelierReady = true; requestAnimationFrame(frame); }