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