// The words on screen: the title card, where Claudia is, how fast she's going, the moments
// worth a word (air, tumbles) and the ride summary. Everything reads from the game state, so a
// screenshot at t seconds shows the same HUD.
import { summary } from "./game.js";
const html = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
export function createHud(root, { touch = false } = {}) {
root.innerHTML = `
Claudia
Can you stop AI and save the world in time?
${touch ? "Tap to ride" : "Press any key to ride"}
0mph2×
0GPUs
${touch ? "Left thumb: drag up to go, down to brake, sideways to steer · Boost and Hop on the right" : "W / ↑ go · S / ↓ brake · A D / ← → steer · Shift boost · Space hop · Esc pause"}
Your ride
${touch ? "Tap to keep riding" : "Esc to keep riding · R to start over"}
`;
const $ = (s) => root.querySelector(s);
const place = $(".place");
const speed = $(".speed b");
const gpus = $(".gpus b");
const banner = $(".banner");
const msg = $(".msg");
const help = $(".help");
const dl = $(".summary dl");
let last = "";
return {
update(game) {
root.dataset.state = game.state;
const p = game.p;
speed.textContent = Math.round(Math.abs(p.speed) * 2.23694);
speed.parentElement.classList.toggle("boosting", !!p.boost && !(p.burrito > 0));
speed.parentElement.classList.toggle("fed", p.burrito > 0);
gpus.textContent = game.stats.gpus ?? 0;
const key = `${game.place.district}|${game.place.street}`;
if (key !== last) {
last = key;
place.querySelector("b").textContent = game.place.district;
place.querySelector("span").textContent = game.place.street && game.place.street !== game.place.district ? game.place.street : "";
}
const since = game.time - game.started;
help.classList.toggle("on", game.state === "play" && since < 9);
const b = game.banner;
const bOn = b && game.time - b.at < 2.8 && since > 0.6;
banner.classList.toggle("on", !!bOn);
if (b) {
banner.querySelector("b").textContent = b.text;
banner.querySelector("span").textContent = b.sub ?? "";
}
const m = game.msg;
const mOn = m && game.time - m.at < 2.2;
msg.classList.toggle("on", !!mOn);
if (m) {
msg.textContent = m.text;
msg.dataset.kind = m.kind;
}
if (game.state === "over") {
const s = summary(game);
dl.innerHTML = [
["Distance", `${s.miles.toFixed(2)} mi`],
["Top speed", `${Math.round(s.topMph)} mph`],
["Longest air", `${s.longestAir.toFixed(1)} s`],
["Jumps", s.jumps],
["GPUs taken", s.gpus],
["Burritos", s.burritos],
["Tumbles", s.tumbles],
["Last seen", game.place.street && game.place.street !== game.place.district ? `${game.place.street}, ${game.place.district}` : game.place.district],
]
.map(([k, v]) => `${html(k)}${html(String(v))}`)
.join("");
}
},
};
}