- 服务端 60Hz 固定步长模拟:重力/摩擦/轴分离碰撞/上台阶,客户端预测回滚 - 命中裁决:射线打墙打人(身体 + 头部盒),射速/弹匣/换弹/伤害/比分全在服务端 - 回合制:3 秒准备、先到 8 分、100 秒平局;观战自由视角 - 机器人陪练:视线检测 + 反应延迟 + 点射偏差,能单人开练 - 前端零依赖:手写 WebGL(程序化贴图、地图合批、粒子、曳光弹、假投影、黄昏天空) - 2D Canvas 枪模 + WebAudio 合成音效,无任何图片/音频/CDN 资源 - 地图 level.json 前后端同源,tools/gen_level.py 四分镜像生成,绝对对称 - 测试:14 组规则单测 + 6 组联机 e2e
197 lines
6.0 KiB
JavaScript
197 lines
6.0 KiB
JavaScript
// HUD 全部是 DOM,更新只碰必要节点;准星张开度用 CSS 变量。
|
|
|
|
const $ = (id) => document.getElementById(id);
|
|
|
|
export class Hud {
|
|
constructor() {
|
|
this.root = $("hud");
|
|
this.el = {
|
|
hud: this.root,
|
|
scoreMe: $("scoreMe"),
|
|
scoreFoe: $("scoreFoe"),
|
|
foeName: $("foeName"),
|
|
roundNo: $("roundNo"),
|
|
roundTimer: $("roundTimer"),
|
|
fps: $("fps"),
|
|
ping: $("ping"),
|
|
specBadge: $("specBadge"),
|
|
crosshair: $("crosshair"),
|
|
hitmarker: $("hitmarker"),
|
|
dmgFlash: $("dmgFlash"),
|
|
hp: $("hp"),
|
|
hpBar: $("hpBar"),
|
|
aliveText: $("aliveText"),
|
|
kda: $("kda"),
|
|
wname: $("wname"),
|
|
mag: $("mag"),
|
|
reserve: $("reserve"),
|
|
reloadTrack: $("reloadTrack"),
|
|
reloadFill: $("reloadFill"),
|
|
killfeed: $("killfeed"),
|
|
banner: $("banner"),
|
|
bannerKicker: $("bannerKicker"),
|
|
bannerTitle: $("bannerTitle"),
|
|
bannerSub: $("bannerSub"),
|
|
deathScreen: $("deathScreen"),
|
|
deathBy: $("deathBy"),
|
|
deathWeapon: $("deathWeapon"),
|
|
deathCount: $("deathCount"),
|
|
spectateBar: $("spectateBar"),
|
|
scoreboard: $("scoreboard"),
|
|
sbRows: $("sbRows"),
|
|
sbFoot: $("sbFoot"),
|
|
pauseScreen: $("pauseScreen"),
|
|
waiting: $("waiting"),
|
|
waitCode: $("waitCode"),
|
|
};
|
|
this._bannerTimer = 0;
|
|
this._lastHp = 100;
|
|
this._lastScore = [0, 0];
|
|
this._lastPhase = "";
|
|
}
|
|
|
|
show(on) {
|
|
this.root.hidden = !on;
|
|
}
|
|
|
|
setScore(me, foe, target, foeName) {
|
|
this.el.scoreMe.textContent = me;
|
|
this.el.scoreFoe.textContent = foe;
|
|
if (foeName) this.el.foeName.textContent = foeName;
|
|
this.el.sbFoot.textContent = `先到 ${target} 分获胜`;
|
|
}
|
|
|
|
setRound(no, leftMs) {
|
|
this.el.roundNo.textContent = `第 ${no} 回合`;
|
|
if (leftMs == null) {
|
|
this.el.roundTimer.textContent = "--:--";
|
|
return;
|
|
}
|
|
const s = Math.max(0, Math.ceil(leftMs / 1000));
|
|
const m = Math.floor(s / 60);
|
|
const ss = String(s % 60).padStart(2, "0");
|
|
this.el.roundTimer.textContent = `${m}:${ss}`;
|
|
}
|
|
|
|
setHp(hp, alive, kills, deaths) {
|
|
const v = Math.max(0, Math.round(hp));
|
|
this.el.hp.textContent = v;
|
|
this.el.hpBar.style.width = `${v}%`;
|
|
this.el.hpBar.className = v <= 30 ? "low" : v <= 60 ? "warn" : "";
|
|
this.el.aliveText.textContent = alive ? "存活" : "阵亡";
|
|
this.el.kda.textContent = `${kills} / ${deaths}`;
|
|
if (v < this._lastHp) this.damageFlash();
|
|
this._lastHp = v;
|
|
}
|
|
|
|
setAmmo(name, mag, reserve, reloading, progress, magSize) {
|
|
this.el.wname.textContent = name;
|
|
this.el.mag.textContent = mag;
|
|
this.el.reserve.textContent = reserve;
|
|
this.el.mag.classList.toggle("empty", mag === 0 && !reloading);
|
|
if (reloading) {
|
|
this.el.reloadTrack.hidden = false;
|
|
this.el.reloadFill.style.width = `${Math.min(100, Math.max(0, progress * 100))}%`;
|
|
} else {
|
|
this.el.reloadTrack.hidden = true;
|
|
}
|
|
if (magSize) this.el.mag.title = `${mag} / ${magSize}`;
|
|
}
|
|
|
|
setCrosshair(spread) {
|
|
const gap = 4 + spread * 26;
|
|
this.root.style.setProperty("--ch-gap", `${gap.toFixed(1)}px`);
|
|
}
|
|
|
|
hitmarker(kill) {
|
|
const hm = this.el.hitmarker;
|
|
hm.classList.remove("on", "kill");
|
|
void hm.offsetWidth;
|
|
hm.classList.add("on");
|
|
if (kill) hm.classList.add("kill");
|
|
clearTimeout(this._hmTimer);
|
|
this._hmTimer = setTimeout(() => hm.classList.remove("on", "kill"), 240);
|
|
}
|
|
|
|
damageFlash() {
|
|
const f = this.el.dmgFlash;
|
|
f.classList.add("on");
|
|
clearTimeout(this._dmgTimer);
|
|
this._dmgTimer = setTimeout(() => f.classList.remove("on"), 70);
|
|
}
|
|
|
|
killfeed(byName, victimName, weapon, head, mine) {
|
|
const d = document.createElement("div");
|
|
d.className = "kf" + (mine === "me" ? " me" : mine === "foe" ? " foe" : "");
|
|
d.innerHTML = `<span class="who ${mine === "me" ? "me" : "foe"}">${byName}</span>
|
|
<span class="gun">${weapon}</span>
|
|
<span class="hs">${head ? "爆头" : ""}</span>
|
|
<span class="who">${victimName}</span>`;
|
|
this.el.killfeed.prepend(d);
|
|
while (this.el.killfeed.children.length > 5) this.el.killfeed.lastChild.remove();
|
|
setTimeout(() => d.remove(), 6500);
|
|
}
|
|
|
|
banner(kicker, title, sub, ms = 1600) {
|
|
clearTimeout(this._bannerTimer);
|
|
this.el.bannerKicker.textContent = kicker || "";
|
|
this.el.bannerTitle.textContent = title || "";
|
|
this.el.bannerSub.textContent = sub || "";
|
|
this.el.banner.hidden = false;
|
|
this._bannerTimer = setTimeout(() => {
|
|
this.el.banner.hidden = true;
|
|
}, ms);
|
|
}
|
|
|
|
death(killer, weapon) {
|
|
this.el.deathBy.textContent = killer;
|
|
this.el.deathWeapon.textContent = weapon;
|
|
this.el.deathScreen.hidden = false;
|
|
}
|
|
|
|
hideDeath() {
|
|
this.el.deathScreen.hidden = true;
|
|
}
|
|
|
|
setDeathCountdown(ms) {
|
|
this.el.deathCount.textContent = (Math.max(0, ms) / 1000).toFixed(1);
|
|
}
|
|
|
|
scoreboard(players, selfSlot, score, target, spectating) {
|
|
const rows = [];
|
|
for (const p of players) {
|
|
const mine = p.slot === selfSlot;
|
|
rows.push(`<div class="sb-row">
|
|
<span class="pname"><i class="swatch ${mine ? "me" : "foe"}"></i>${p.name}${p.bot ? " · BOT" : ""}${mine ? " · 你" : ""}</span>
|
|
<span class="num">${p.kills}</span>
|
|
<span class="num">${p.deaths}</span>
|
|
<span class="st ${p.alive ? "" : "dead"}">${p.alive ? "存活" : "阵亡"}</span>
|
|
</div>`);
|
|
}
|
|
if (spectating) {
|
|
rows.push(`<div class="sb-row"><span class="pname" style="color:var(--dim)">观战中</span><span class="num">-</span><span class="num">-</span><span class="st">-</span></div>`);
|
|
}
|
|
this.el.sbRows.innerHTML = rows.join("");
|
|
this.el.sbFoot.textContent = `${score[0]} : ${score[1]} · 先到 ${target} 分获胜`;
|
|
}
|
|
|
|
setSpectate(on) {
|
|
this.el.specBadge.hidden = !on;
|
|
this.el.spectateBar.hidden = !on;
|
|
}
|
|
|
|
waiting(on, code) {
|
|
this.el.waiting.hidden = !on;
|
|
if (code) this.el.waitCode.textContent = code;
|
|
}
|
|
|
|
pause(on) {
|
|
this.el.pauseScreen.hidden = !on;
|
|
}
|
|
|
|
setNet(fps, ping) {
|
|
this.el.fps.textContent = fps;
|
|
this.el.ping.textContent = ping;
|
|
}
|
|
}
|