// The page: the view, the HUD, input and the loop. With ?shot= it draws one deterministic frame // for the review pack's screenshots instead (?shot=title|play|over&seed=7&t=20), then sets // window.atelierReady. With ?auto=1 the autopilot rides. import { autopilot } from "./bot.js"; import { DT, createGame, start, step } from "./game.js"; import { createHud } from "./hud.js"; import { createInput } from "./input.js"; import { createView } from "./render.js"; const params = new URLSearchParams(location.search); const shot = params.get("shot"); const auto = params.has("auto"); const seed = Number(params.get("seed")) || (shot ? 1 : Math.floor(Math.random() * 1e9)); const canvas = document.getElementById("game"); const aspect = () => window.innerWidth / Math.max(1, window.innerHeight); const touch = matchMedia("(pointer: coarse)").matches; /** Is WebGL drawn in software (no GPU: SwiftShader, llvmpipe)? Then the full pipeline runs at a * frame every second or two and its first frame takes many seconds to compile, so such * machines get the light pipeline that phones get. */ function softwareGL() { try { const gl = document.createElement("canvas").getContext("webgl2"); if (!gl) return false; const info = gl.getExtension("WEBGL_debug_renderer_info"); const name = String(gl.getParameter(info ? info.UNMASKED_RENDERER_WEBGL : gl.RENDERER)); gl.getExtension("WEBGL_lose_context")?.loseContext(); return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(name); } catch { return false; } } const hud = createHud(document.getElementById("hud"), { touch }); let view; try { // high quality on laptops, a lighter pipeline on phones and on machines with no GPU; // ?quality=low|high overrides view = createView(canvas, { quality: params.get("quality") ?? (touch || softwareGL() ? "low" : "high") }); view.resize(); } catch (err) { document.getElementById("hud").innerHTML = `

Claudia

Claudia needs WebGL to ride. Try another browser or turn on hardware acceleration.

`; window.atelierReady = true; throw err; } let game = createGame(seed, { aspect: aspect() }); // A handle for tests and agents: step and draw frames by hand (a hidden page runs no rAF). window.claudia = { get game() { return game; }, view, step: (n = 1, auto = true) => { for (let i = 0; i < n; i++) step(game, auto ? autopilot(game) : {}); hud.update(game); view.render(game); return game; }, }; if (shot) { const t = Number(params.get("t") ?? (shot === "title" ? 0 : 20)); if (shot !== "title") start(game); for (let i = 0; i < Math.round(t / DT); i++) step(game, shot === "title" ? {} : autopilot(game)); if (shot === "over") game.state = "over"; hud.update(game); view.render(game); // One frame is enough: the drawing buffer is kept for the screenshot. (A second would only // compile more shaders: the first shadow pass runs before the lights are counted.) Say // ready once it has reached the screen; never wait on rAF alone, since a hidden page may // not run it. let done = false; const finish = () => { if (done) return; done = true; window.atelierReady = true; }; requestAnimationFrame(finish); setTimeout(finish, 120); } else { const begin = () => { if (game.state === "title") start(game); }; const input = createInput(canvas, { onAny: () => { if (game.state === "title") begin(); else if (game.state === "over" && touch) game.state = "play"; }, onPause: () => { if (game.state === "play") game.state = "over"; else if (game.state === "over") game.state = "play"; }, onRestart: () => { game = createGame(Math.floor(Math.random() * 1e9), { aspect: aspect() }); start(game); }, }); if (auto) begin(); addEventListener("resize", () => { view.resize(); game.view.aspect = aspect(); }); let last = performance.now(); let carry = 0; const frame = (now) => { carry = Math.min(0.25, carry + (now - last) / 1000); last = now; while (carry >= DT) { step(game, auto ? autopilot(game) : input.read()); carry -= DT; } hud.update(game); view.render(game); requestAnimationFrame(frame); }; hud.update(game); view.render(game); window.atelierReady = true; requestAnimationFrame(frame); }