Files
rong 9a6441bcf9 feat: 蹲下 + 放大镜(开镜),移动降速
- 蹲下:C/Ctrl 按住(触屏「蹲下」按钮),碰撞体 1.75→1.05,
  命中盒跟着压矮;松键起身先查头顶空间,顶住站不起来;
  蹲走 1.5 m/s 且没脚步声;视点/枪模/远程玩家模型平滑过渡
- 放大镜:右键按住(触屏「开镜」按钮),FOV 78→30/42 平滑缩放,
  灵敏度随 FOV 联动;散布 ×0.35/×0.5、移速 ×0.55,服务端裁决;
  镜环 + 镜内准星替换普通准星
- 降速:跑 5.6→4.6、静步 2.7→2.0(反馈太快),观战飞行同步降
- 修复:reconcile 回滚时从快照恢复 crouch,避免低帧率/丢包时
  历史被 Ack 吃空后蹲下状态被 makeBody 抹掉
- 新增 3 组单测:蹲下变矮且命中盒跟着变、蹲/开镜减速、矮洞站不起来
2026-10-25 12:08:45 +08:00

210 lines
6.4 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"),
scope: $("scope"),
stance: $("stanceTag"),
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`);
}
/** 放大镜:开镜时普通准星隐身,镜环 + 镜内准星上岗 */
setZoom(on) {
this.el.crosshair.style.opacity = on ? "0" : "";
this.el.scope.classList.toggle("on", on);
}
/** 蹲下标签 */
setStance(crouch) {
this.el.stance.hidden = !crouch;
}
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;
}
}