| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
"use strict"; |
| 13 |
|
| 14 |
import { makeBeeper } from "./beep.js"; |
| 15 |
import { drawGun, gunSnapshot, gunTick, makeGun } from "./gun.js"; |
| 16 |
import { choosePrompt, REACH } from "./prompt.js"; |
| 17 |
import { drawScene, makeScene } from "./scene.js"; |
| 18 |
import { moveVector } from "./steer.js"; |
| 19 |
import { drawTopdown } from "./topdown.js"; |
| 20 |
|
| 21 |
const $ = (id) => document.getElementById(id); |
| 22 |
const view = $("view"); |
| 23 |
const ctx = view.getContext("2d"); |
| 24 |
|
| 25 |
const KEY_SLOT = { KeyC: "c", KeyQ: "q", KeyE: "e", KeyX: "x" }; |
| 26 |
const COARSE = matchMedia("(pointer: coarse)").matches; |
| 27 |
const LOOK = 0.0022; |
| 28 |
const HELP_SEEN = "val.help.seen"; |
| 29 |
|
| 30 |
let map = null; |
| 31 |
let solid = () => true; |
| 32 |
let snap = null; |
| 33 |
let socket = null; |
| 34 |
let agents = null; |
| 35 |
const shown = new Map(); |
| 36 |
const keys = new Set(); |
| 37 |
let yaw = 0; |
| 38 |
let locked = false; |
| 39 |
let firing = false; |
| 40 |
let mode = COARSE ? "top" : "fp"; |
| 41 |
let mapOpen = false; |
| 42 |
let pointer = null; |
| 43 |
let stick = null; |
| 44 |
let aimTouch = null; |
| 45 |
let helpShown = false; |
| 46 |
let last = performance.now(); |
| 47 |
|
| 48 |
const walk = { x: 0, y: 0 }; |
| 49 |
const scene = makeScene(); |
| 50 |
const gun = makeGun(); |
| 51 |
const beeper = makeBeeper(); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
function connect() { |
| 56 |
const url = (location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/ws"; |
| 57 |
socket = new WebSocket(url); |
| 58 |
socket.onopen = () => ($("conn").textContent = "live"); |
| 59 |
socket.onclose = () => { |
| 60 |
$("conn").textContent = "reconnecting…"; |
| 61 |
setTimeout(connect, 1500); |
| 62 |
}; |
| 63 |
socket.onmessage = (event) => { |
| 64 |
const data = JSON.parse(event.data); |
| 65 |
if (data.t !== "s") return; |
| 66 |
const first = snap === null; |
| 67 |
snap = data; |
| 68 |
if (first && mode === "fp") yaw = data.me.angle; |
| 69 |
gunSnapshot(gun, data); |
| 70 |
paintHud(data); |
| 71 |
maybeHelp(data); |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
const send = (message) => { |
| 76 |
if (socket && socket.readyState === 1) socket.send(JSON.stringify(message)); |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
function setMode(next) { |
| 82 |
mode = next; |
| 83 |
$("stage").dataset.mode = next; |
| 84 |
if (next !== "fp" && document.pointerLockElement === view) document.exitPointerLock(); |
| 85 |
if (snap) yaw = snap.me.angle; |
| 86 |
} |
| 87 |
|
| 88 |
addEventListener("keydown", (event) => { |
| 89 |
if (event.target instanceof HTMLInputElement) return; |
| 90 |
if (event.code === "Tab") { |
| 91 |
mapOpen = true; |
| 92 |
event.preventDefault(); |
| 93 |
return; |
| 94 |
} |
| 95 |
if (event.code === "KeyH" || event.key === "?") { |
| 96 |
toggleHelp(); |
| 97 |
event.preventDefault(); |
| 98 |
return; |
| 99 |
} |
| 100 |
if (event.code === "KeyV") { |
| 101 |
setMode(mode === "fp" ? "top" : "fp"); |
| 102 |
event.preventDefault(); |
| 103 |
return; |
| 104 |
} |
| 105 |
if (event.code === "KeyB") { |
| 106 |
toggleBuy(); |
| 107 |
event.preventDefault(); |
| 108 |
return; |
| 109 |
} |
| 110 |
if (KEY_SLOT[event.code]) { |
| 111 |
send({ t: "i", c: KEY_SLOT[event.code] }); |
| 112 |
event.preventDefault(); |
| 113 |
return; |
| 114 |
} |
| 115 |
keys.add(event.code); |
| 116 |
if (["KeyW", "KeyA", "KeyS", "KeyD", "Space", "KeyF", "KeyR", "ArrowLeft", "ArrowRight"].includes(event.code)) |
| 117 |
event.preventDefault(); |
| 118 |
}); |
| 119 |
addEventListener("keyup", (event) => { |
| 120 |
if (event.code === "Tab") mapOpen = false; |
| 121 |
keys.delete(event.code); |
| 122 |
}); |
| 123 |
addEventListener("blur", () => { |
| 124 |
keys.clear(); |
| 125 |
mapOpen = false; |
| 126 |
firing = false; |
| 127 |
}); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
view.addEventListener("click", () => { |
| 132 |
beeper.wake(); |
| 133 |
if (mode === "fp" && !locked && view.requestPointerLock) view.requestPointerLock(); |
| 134 |
}); |
| 135 |
document.addEventListener("pointerlockchange", () => { |
| 136 |
locked = document.pointerLockElement === view; |
| 137 |
if (!locked) firing = false; |
| 138 |
$("stage").dataset.locked = locked ? "1" : ""; |
| 139 |
}); |
| 140 |
addEventListener("mousemove", (event) => { |
| 141 |
if (locked) yaw += event.movementX * LOOK; |
| 142 |
}); |
| 143 |
addEventListener("mousedown", (event) => { |
| 144 |
if (locked && event.button === 0) firing = true; |
| 145 |
}); |
| 146 |
addEventListener("mouseup", (event) => { |
| 147 |
if (locked && event.button === 0) firing = false; |
| 148 |
}); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
const canvasPoint = (event) => { |
| 153 |
const box = view.getBoundingClientRect(); |
| 154 |
return { |
| 155 |
x: ((event.clientX - box.left) / box.width) * view.width, |
| 156 |
y: ((event.clientY - box.top) / box.height) * view.height, |
| 157 |
}; |
| 158 |
}; |
| 159 |
view.addEventListener("pointermove", (event) => { |
| 160 |
if (mode !== "top" || event.pointerType === "touch") return; |
| 161 |
pointer = canvasPoint(event); |
| 162 |
}); |
| 163 |
view.addEventListener("pointerdown", (event) => { |
| 164 |
beeper.wake(); |
| 165 |
if (mode !== "top") return; |
| 166 |
view.setPointerCapture(event.pointerId); |
| 167 |
const box = view.getBoundingClientRect(); |
| 168 |
if (event.pointerType === "touch") { |
| 169 |
if (event.clientX - box.left < box.width / 2) |
| 170 |
stick = { id: event.pointerId, ox: event.clientX, oy: event.clientY, mx: 0, my: 0 }; |
| 171 |
else { |
| 172 |
aimTouch = { id: event.pointerId, ...canvasPoint(event) }; |
| 173 |
firing = true; |
| 174 |
} |
| 175 |
event.preventDefault(); |
| 176 |
return; |
| 177 |
} |
| 178 |
pointer = canvasPoint(event); |
| 179 |
firing = true; |
| 180 |
}); |
| 181 |
view.addEventListener("pointermove", (event) => { |
| 182 |
if (stick && event.pointerId === stick.id) { |
| 183 |
const dx = event.clientX - stick.ox; |
| 184 |
const dy = event.clientY - stick.oy; |
| 185 |
const len = Math.hypot(dx, dy) || 1; |
| 186 |
const reach = Math.min(len, 48) / 48; |
| 187 |
stick.mx = (dx / len) * reach; |
| 188 |
stick.my = (dy / len) * reach; |
| 189 |
} |
| 190 |
if (aimTouch && event.pointerId === aimTouch.id) Object.assign(aimTouch, canvasPoint(event)); |
| 191 |
}); |
| 192 |
const endPointer = (event) => { |
| 193 |
if (stick && event.pointerId === stick.id) stick = null; |
| 194 |
else if (aimTouch && event.pointerId === aimTouch.id) { |
| 195 |
aimTouch = null; |
| 196 |
firing = false; |
| 197 |
} else firing = false; |
| 198 |
}; |
| 199 |
view.addEventListener("pointerup", endPointer); |
| 200 |
view.addEventListener("pointercancel", endPointer); |
| 201 |
view.addEventListener("contextmenu", (event) => event.preventDefault()); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
setInterval(() => { |
| 207 |
if (!snap || !map) return; |
| 208 |
let ax = 0; |
| 209 |
let ay = 0; |
| 210 |
if (keys.has("KeyW")) ay -= 1; |
| 211 |
if (keys.has("KeyS")) ay += 1; |
| 212 |
if (keys.has("KeyA")) ax -= 1; |
| 213 |
if (keys.has("KeyD")) ax += 1; |
| 214 |
if (stick) { |
| 215 |
ax = stick.mx; |
| 216 |
ay = stick.my; |
| 217 |
} |
| 218 |
let mx = ax; |
| 219 |
let my = ay; |
| 220 |
let angle; |
| 221 |
if (mode === "fp") { |
| 222 |
if (keys.has("ArrowLeft")) yaw -= 0.045; |
| 223 |
if (keys.has("ArrowRight")) yaw += 0.045; |
| 224 |
angle = yaw; |
| 225 |
moveVector(ax, ay, yaw, walk); |
| 226 |
mx = walk.x; |
| 227 |
my = walk.y; |
| 228 |
} else { |
| 229 |
const aim = aimTouch || pointer; |
| 230 |
const me = worldToScreen(snap.me); |
| 231 |
angle = aim ? Math.atan2(aim.y - me.y, aim.x - me.x) : snap.me.angle; |
| 232 |
yaw = angle; |
| 233 |
} |
| 234 |
send({ |
| 235 |
t: "i", |
| 236 |
mx, |
| 237 |
my, |
| 238 |
a: angle, |
| 239 |
f: firing || keys.has("Space"), |
| 240 |
u: keys.has("KeyF"), |
| 241 |
r: keys.has("KeyR"), |
| 242 |
}); |
| 243 |
}, 50); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
function resize() { |
| 248 |
const box = view.getBoundingClientRect(); |
| 249 |
const dpr = Math.min(devicePixelRatio || 1, 2); |
| 250 |
view.width = Math.round(box.width * dpr); |
| 251 |
view.height = Math.round(box.height * dpr); |
| 252 |
} |
| 253 |
addEventListener("resize", resize); |
| 254 |
|
| 255 |
const scale = () => (map ? view.width / (map.rows[0].length * map.tile) : 1); |
| 256 |
const worldToScreen = (p) => ({ x: p.x * scale(), y: p.y * scale() }); |
| 257 |
|
| 258 |
function smooth(id, x, y, angle) { |
| 259 |
const now = shown.get(id); |
| 260 |
if (!now) { |
| 261 |
const fresh = { x, y, angle }; |
| 262 |
shown.set(id, fresh); |
| 263 |
return fresh; |
| 264 |
} |
| 265 |
now.x += (x - now.x) * 0.35; |
| 266 |
now.y += (y - now.y) * 0.35; |
| 267 |
const turn = ((angle - now.angle + Math.PI * 3) % (Math.PI * 2)) - Math.PI; |
| 268 |
now.angle += turn * 0.4; |
| 269 |
return now; |
| 270 |
} |
| 271 |
|
| 272 |
const look = (p) => smooth(p.id, p.x, p.y, p.angle); |
| 273 |
|
| 274 |
function frame(now) { |
| 275 |
requestAnimationFrame(frame); |
| 276 |
const dt = Math.min(0.1, (now - last) / 1000); |
| 277 |
last = now; |
| 278 |
if (!map) return; |
| 279 |
gunTick(gun, dt); |
| 280 |
ctx.clearRect(0, 0, view.width, view.height); |
| 281 |
|
| 282 |
if (!snap) { |
| 283 |
drawTopdown(ctx, map, null, look, { x: 0, y: 0, w: view.width, h: view.height }); |
| 284 |
return; |
| 285 |
} |
| 286 |
beeper.update(snap.spike.planted, snap.spike.fuse, now / 1000); |
| 287 |
const eye = look(snap.me); |
| 288 |
const facing = mode === "fp" ? yaw : eye.angle; |
| 289 |
|
| 290 |
if (mode === "fp" && !mapOpen) { |
| 291 |
drawScene(ctx, scene, { solid, tile: map.tile }, { x: eye.x, y: eye.y, angle: facing }, snap, { |
| 292 |
width: view.width, |
| 293 |
height: view.height, |
| 294 |
look, |
| 295 |
}); |
| 296 |
drawGun(ctx, gun, view.width, view.height, snap); |
| 297 |
drawMinimap(); |
| 298 |
} else { |
| 299 |
drawTopdown(ctx, map, snap, look, { x: 0, y: 0, w: view.width, h: view.height }); |
| 300 |
} |
| 301 |
|
| 302 |
if (snap.me.planting > 0) bar(snap.me.planting / 4, "#e0a040", "planting"); |
| 303 |
else if (snap.spike.planted && snap.spike.defuse > 0 && snap.me.side === "def") |
| 304 |
bar(snap.spike.defuse / 7, "#4fd6a0", "defusing"); |
| 305 |
$("flash").style.opacity = Math.min(0.95, snap.me.blind * 0.8); |
| 306 |
$("dead").style.display = snap.me.alive ? "none" : "grid"; |
| 307 |
$("aimhint").style.display = mode === "fp" && !locked && !COARSE ? "grid" : "none"; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
function drawMinimap() { |
| 312 |
const w = Math.min(view.width * 0.3, 260 * (view.width / 900)); |
| 313 |
const h = (w * map.rows.length) / map.rows[0].length; |
| 314 |
const pad = 8; |
| 315 |
ctx.save(); |
| 316 |
ctx.globalAlpha = 0.85; |
| 317 |
ctx.fillStyle = "#0b0d11"; |
| 318 |
ctx.fillRect(pad - 2, pad - 2, w + 4, h + 4); |
| 319 |
drawTopdown(ctx, map, snap, look, { x: pad, y: pad, w, h }, { labels: false }); |
| 320 |
ctx.restore(); |
| 321 |
ctx.strokeStyle = "rgba(255,255,255,0.18)"; |
| 322 |
ctx.lineWidth = 1; |
| 323 |
ctx.strokeRect(pad - 2, pad - 2, w + 4, h + 4); |
| 324 |
} |
| 325 |
|
| 326 |
function bar(fraction, colour, label) { |
| 327 |
const w = view.width * 0.4; |
| 328 |
const x = view.width / 2 - w / 2; |
| 329 |
const y = view.height * 0.86; |
| 330 |
ctx.fillStyle = "#0009"; |
| 331 |
ctx.fillRect(x, y, w, 16); |
| 332 |
ctx.fillStyle = colour; |
| 333 |
ctx.fillRect(x, y, w * Math.min(1, fraction), 16); |
| 334 |
ctx.fillStyle = "#fff"; |
| 335 |
ctx.font = "12px system-ui, sans-serif"; |
| 336 |
ctx.textAlign = "center"; |
| 337 |
ctx.fillText(label, view.width / 2, y + 12); |
| 338 |
ctx.textAlign = "left"; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
function toggleBuy() { |
| 346 |
const open = $("buy").classList.toggle("on"); |
| 347 |
$("buy").dataset.closed = open ? "" : "1"; |
| 348 |
} |
| 349 |
$("buytoggle").addEventListener("click", toggleBuy); |
| 350 |
$("viewtoggle").addEventListener("click", () => setMode(mode === "fp" ? "top" : "fp")); |
| 351 |
$("helptoggle").addEventListener("click", () => toggleHelp()); |
| 352 |
$("helpclose").addEventListener("click", () => toggleHelp(false)); |
| 353 |
for (const button of document.querySelectorAll("[data-buy]")) |
| 354 |
button.addEventListener("click", () => send({ t: "buy", what: button.dataset.buy })); |
| 355 |
for (const button of document.querySelectorAll("[data-agent]")) |
| 356 |
button.addEventListener("click", () => send({ t: "agent", id: button.dataset.agent })); |
| 357 |
|
| 358 |
function toggleHelp(force) { |
| 359 |
const open = force === undefined ? $("help").style.display !== "grid" : force; |
| 360 |
$("help").style.display = open ? "grid" : "none"; |
| 361 |
if (!open) { |
| 362 |
helpShown = true; |
| 363 |
try { |
| 364 |
localStorage.setItem(HELP_SEEN, "1"); |
| 365 |
} catch { |
| 366 |
|
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
function maybeHelp(s) { |
| 373 |
if (helpShown) return; |
| 374 |
try { |
| 375 |
if (localStorage.getItem(HELP_SEEN) === "1") { |
| 376 |
helpShown = true; |
| 377 |
return; |
| 378 |
} |
| 379 |
} catch { |
| 380 |
|
| 381 |
} |
| 382 |
if (s.phase === "live" && s.me.alive) return; |
| 383 |
helpShown = true; |
| 384 |
$("help").style.display = "grid"; |
| 385 |
} |
| 386 |
|
| 387 |
const KITS = {}; |
| 388 |
|
| 389 |
function paintHud(s) { |
| 390 |
$("clock").textContent = Math.ceil(s.phase === "live" && s.spike.planted ? s.spike.fuse : s.clock); |
| 391 |
document.querySelector("#scoreline .red").textContent = "red " + s.score.red; |
| 392 |
document.querySelector("#scoreline .blue").textContent = s.score.blue + " blue"; |
| 393 |
$("phase").textContent = |
| 394 |
`round ${s.round} · ${s.phase === "buy" ? "buy phase" : s.phase === "live" ? (s.spike.planted ? "spike down" : "live") : s.phase === "end" ? "round over" : "match over"} · ${s.attackers} attacking · you are ${s.me.side === "atk" ? "attacking" : "defending"}${s.result ? " · " + s.result : ""}`; |
| 395 |
$("hp").textContent = `${s.me.hp} hp${s.me.armour ? " +" + s.me.armour : ""}`; |
| 396 |
$("gun").textContent = `${s.me.weapon}${s.me.reloading ? " reloading" : " " + s.me.ammo}`; |
| 397 |
$("creds").textContent = `${s.me.credits} cr`; |
| 398 |
$("ultmeter").textContent = `ult ${s.me.ult}/${s.me.ultNeeds}`; |
| 399 |
$("buy").classList.toggle("on", s.phase === "buy" && $("buy").dataset.closed !== "1"); |
| 400 |
if (s.phase !== "buy") $("buy").dataset.closed = ""; |
| 401 |
|
| 402 |
const text = choosePrompt(s, { |
| 403 |
onSite: onSite(s.me), |
| 404 |
nearSpike: s.spike.x !== null && Math.hypot(s.spike.x - s.me.x, s.spike.y - s.me.y) < REACH, |
| 405 |
}); |
| 406 |
if ($("prompt").textContent !== text) $("prompt").textContent = text; |
| 407 |
|
| 408 |
paintKit(s); |
| 409 |
paintBuy(s); |
| 410 |
|
| 411 |
const log = $("log"); |
| 412 |
const lines = s.events.slice(0, 3).join("\n"); |
| 413 |
if (log.dataset.text !== lines) { |
| 414 |
log.dataset.text = lines; |
| 415 |
log.replaceChildren(); |
| 416 |
for (const line of s.events.slice(0, 3)) { |
| 417 |
const div = document.createElement("div"); |
| 418 |
div.textContent = line; |
| 419 |
log.append(div); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
for (const side of ["atk", "def"]) { |
| 424 |
const body = $(side + "board"); |
| 425 |
const rows = s.board.filter((p) => p.side === side).sort((a, b) => b.kills - a.kills); |
| 426 |
const key = rows.map((p) => `${p.name}${p.kills}${p.deaths}${p.alive}${p.credits}`).join(); |
| 427 |
if (body.dataset.key === key) continue; |
| 428 |
body.dataset.key = key; |
| 429 |
body.replaceChildren(); |
| 430 |
for (const player of rows) { |
| 431 |
const tr = document.createElement("tr"); |
| 432 |
const cells = [ |
| 433 |
player.name + (player.bot ? " (bot)" : "") + (player.alive ? "" : " ✕"), |
| 434 |
player.agent + (player.credits !== null ? ` · ${player.credits}cr` : ""), |
| 435 |
String(player.kills), |
| 436 |
String(player.deaths), |
| 437 |
]; |
| 438 |
for (const value of cells) { |
| 439 |
const td = document.createElement("td"); |
| 440 |
td.textContent = value; |
| 441 |
tr.append(td); |
| 442 |
} |
| 443 |
if (player.id === s.me.id) tr.style.fontWeight = "700"; |
| 444 |
body.append(tr); |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
function onSite(me) { |
| 451 |
if (!map) return false; |
| 452 |
const row = map.rows[Math.floor(me.y / map.tile)]; |
| 453 |
const cell = row ? row[Math.floor(me.x / map.tile)] : ""; |
| 454 |
return cell === "A" || cell === "B"; |
| 455 |
} |
| 456 |
|
| 457 |
const kitOf = (id) => (agents ? agents.find((a) => a.id === id) : null); |
| 458 |
|
| 459 |
|
| 460 |
function paintKit(s) { |
| 461 |
const kit = $("kit"); |
| 462 |
const agent = kitOf(s.me.agent); |
| 463 |
const shape = |
| 464 |
["c", "q", "e", "x"].map((slot) => `${slot}${s.me.charges[slot]}${Math.ceil(s.me.cooldown[slot])}`).join("|") + |
| 465 |
s.me.agent + |
| 466 |
s.me.ult + |
| 467 |
(agent ? "k" : ""); |
| 468 |
if (KITS.shape === shape) return; |
| 469 |
KITS.shape = shape; |
| 470 |
kit.replaceChildren(); |
| 471 |
for (const slot of ["c", "q", "e", "x"]) { |
| 472 |
const ability = agent ? agent.abilities[slot] : null; |
| 473 |
const button = document.createElement("button"); |
| 474 |
const ready = slot === "x" ? s.me.ult >= s.me.ultNeeds : s.me.charges[slot] > 0 && s.me.cooldown[slot] <= 0; |
| 475 |
button.disabled = !ready; |
| 476 |
button.type = "button"; |
| 477 |
const count = |
| 478 |
slot === "x" |
| 479 |
? `${s.me.ult}/${s.me.ultNeeds}` |
| 480 |
: s.me.cooldown[slot] > 0 |
| 481 |
? `${Math.ceil(s.me.cooldown[slot])}s` |
| 482 |
: s.me.charges[slot]; |
| 483 |
const name = document.createElement("span"); |
| 484 |
name.textContent = `${slot.toUpperCase()}${ability ? " " + ability.name : ""}`; |
| 485 |
const tally = document.createElement("b"); |
| 486 |
tally.textContent = String(count); |
| 487 |
button.append(name, tally); |
| 488 |
if (ability) button.title = `${ability.name} — ${ability.text}`; |
| 489 |
button.addEventListener("click", () => send({ t: "i", c: slot })); |
| 490 |
kit.append(button); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
function paintBuy(s) { |
| 496 |
const agent = kitOf(s.me.agent); |
| 497 |
for (const button of document.querySelectorAll("[data-buy]")) { |
| 498 |
const what = button.dataset.buy; |
| 499 |
let cost = Number(button.dataset.cost ?? 0); |
| 500 |
if ((what === "c" || what === "q") && agent) { |
| 501 |
const ability = agent.abilities[what]; |
| 502 |
cost = ability.cost; |
| 503 |
button.replaceChildren(); |
| 504 |
const name = document.createElement("span"); |
| 505 |
name.textContent = `${what.toUpperCase()} ${ability.name}`; |
| 506 |
const price = document.createElement("b"); |
| 507 |
price.textContent = `${ability.cost} · ${s.me.charges[what]}/${ability.charges}`; |
| 508 |
button.append(name, price); |
| 509 |
button.title = `${ability.text} (${ability.charges} charge${ability.charges === 1 ? "" : "s"} a round)`; |
| 510 |
} |
| 511 |
button.disabled = cost > s.me.credits; |
| 512 |
} |
| 513 |
for (const button of document.querySelectorAll("[data-agent]")) { |
| 514 |
const picked = button.dataset.agent === s.me.agent; |
| 515 |
button.style.outline = picked ? "2px solid #6fcf9f" : ""; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
Promise.all([fetch("/map.json").then((r) => r.json()), fetch("/agents.json").then((r) => r.json())]) |
| 522 |
.then(([mapData, kit]) => { |
| 523 |
map = mapData; |
| 524 |
agents = kit.agents; |
| 525 |
const rows = map.rows; |
| 526 |
const cols = rows[0].length; |
| 527 |
solid = (col, row) => |
| 528 |
col < 0 || row < 0 || row >= rows.length || col >= cols || rows[row][col] === "#"; |
| 529 |
$("stage").dataset.mode = mode; |
| 530 |
fillHelp(); |
| 531 |
resize(); |
| 532 |
connect(); |
| 533 |
}) |
| 534 |
.catch(() => ($("conn").textContent = "offline")); |
| 535 |
|
| 536 |
|
| 537 |
function fillHelp() { |
| 538 |
const list = $("helpagents"); |
| 539 |
if (!list || !agents) return; |
| 540 |
list.replaceChildren(); |
| 541 |
for (const agent of agents) { |
| 542 |
const li = document.createElement("li"); |
| 543 |
li.textContent = `${agent.name} (${agent.role}) — ${agent.blurb}`; |
| 544 |
list.append(li); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
requestAnimationFrame(frame); |