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

339 lines
9.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 触屏操作层:左摇杆移动、右半屏滑动瞄准、开火 / 跳 / 换弹 / 切枪 / 菜单。
// 只在粗指针(手机、平板)上启用,桌面仍然走鼠标锁定 + 键盘。
export class Touch {
constructor() {
// 只在真正的手机 / 平板上启用:主指针是粗的、没有 hover,
// 或者 UA 明确是移动端(iPadOS 会伪装成 Mac,但带 Mobile 字样)。
const coarse = !!(window.matchMedia && window.matchMedia("(pointer: coarse)").matches);
const noHover = !!(window.matchMedia && window.matchMedia("(hover: none)").matches);
const mobileUA = /Mobile|Android|iPad|Tablet|iPod|iPhone/i.test(navigator.userAgent);
this.enabled = (coarse && noHover) || (coarse && mobileUA && (navigator.maxTouchPoints || 0) > 0);
this.move = { x: 0, y: 0 };
this.fireHeld = false;
this.zoomHeld = false;
this.crouchHeld = false;
this.jumpQueued = false;
this.reloadQueued = false;
this.switchQueued = false;
this.menuQueued = false;
this.look = { x: 0, y: 0 };
this.stickId = null;
this.lookId = null;
this.lx = 0;
this.ly = 0;
if (!this.enabled) return;
this.mount();
}
mount() {
this.root = document.getElementById("touch");
this.stick = document.getElementById("tStick");
this.knob = document.getElementById("tKnob");
if (!this.root || !this.stick || !this.knob) return;
document.body.classList.add("touch");
this.root.hidden = false;
this.root.setAttribute("aria-hidden", "false");
this.bindStick();
// 开火:按住连发,松手停(轻点也保证至少打出一发)
this.bindFire(document.getElementById("tFire"));
// 单次触发按键
this.bindTap(document.getElementById("tJump"), () => {
this.jumpQueued = true;
});
this.bindTap(document.getElementById("tReload"), () => {
this.reloadQueued = true;
});
this.bindTap(document.getElementById("tSwap"), () => {
this.switchQueued = true;
});
this.bindTap(document.getElementById("tMenu"), () => {
this.menuQueued = true;
});
// 按住类:开镜(放大镜)/ 蹲下
this.bindHold(
document.getElementById("tZoom"),
() => {
this.zoomHeld = true;
},
() => {
this.zoomHeld = false;
}
);
this.bindHold(
document.getElementById("tCrouch"),
() => {
this.crouchHeld = true;
},
() => {
this.crouchHeld = false;
}
);
this.bindLook();
this.watchBlur();
}
/* ------------------------------- 左摇杆 ------------------------------- */
bindStick() {
const stick = this.stick;
const knob = this.knob;
let cx = 0;
let cy = 0;
let radius = 1;
const place = (x, y) => {
let dx = x - cx;
let dy = y - cy;
const len = Math.hypot(dx, dy);
if (len > radius) {
dx = (dx / len) * radius;
dy = (dy / len) * radius;
}
knob.style.transform = `translate(calc(-50% + ${dx}px), calc(-50% + ${dy}px))`;
// 归一化:上推是前进(负数 y)
this.move.x = dx / radius;
this.move.y = dy / radius;
// 死区,避免手指微抖就走
if (Math.hypot(this.move.x, this.move.y) < 0.14) {
this.move.x = 0;
this.move.y = 0;
}
};
const reset = () => {
this.stickId = null;
this.move.x = 0;
this.move.y = 0;
knob.style.transform = "translate(-50%, -50%)";
stick.classList.remove("on");
};
stick.addEventListener(
"pointerdown",
(e) => {
if (this.stickId !== null) return;
e.preventDefault();
e.stopPropagation();
this.stickId = e.pointerId;
stick.setPointerCapture(e.pointerId);
stick.classList.add("on");
const r = stick.getBoundingClientRect();
cx = r.left + r.width / 2;
cy = r.top + r.height / 2;
radius = r.width * 0.42;
place(e.clientX, e.clientY);
},
{ passive: false }
);
stick.addEventListener(
"pointermove",
(e) => {
if (e.pointerId !== this.stickId) return;
e.preventDefault();
place(e.clientX, e.clientY);
},
{ passive: false }
);
for (const type of ["pointerup", "pointercancel", "pointerleave"]) {
stick.addEventListener(type, (e) => {
if (e.pointerId !== this.stickId) return;
reset();
});
}
}
/* ------------------------------- 右侧按键 ------------------------------- */
/** 开火键:点击至少保持 90ms,避免手快点一下就丢帧没打出子弹 */
bindFire(el) {
if (!el) return;
this.fireStart = 0;
el.addEventListener(
"pointerdown",
(e) => {
e.preventDefault();
e.stopPropagation();
el.classList.add("on");
try {
el.setPointerCapture(e.pointerId);
} catch {
/* ignore */
}
this.fireStart = performance.now();
this.fireHeld = true;
},
{ passive: false }
);
const release = () => {
el.classList.remove("on");
const start = this.fireStart;
const wait = Math.max(0, 90 - (performance.now() - start));
const done = () => {
if (this.fireStart === start) this.fireHeld = false;
};
if (wait > 0) setTimeout(done, wait);
else done();
};
el.addEventListener("pointerup", release);
el.addEventListener("pointercancel", release);
el.addEventListener("lostpointercapture", release);
el.addEventListener("contextmenu", (e) => e.preventDefault());
}
bindHold(el, down, up) {
if (!el) return;
const release = () => {
el.classList.remove("on");
up && up();
};
el.addEventListener(
"pointerdown",
(e) => {
e.preventDefault();
e.stopPropagation();
el.classList.add("on");
try {
el.setPointerCapture(e.pointerId);
} catch {
/* 老浏览器忽略 */
}
down && down();
},
{ passive: false }
);
el.addEventListener("pointerup", release);
el.addEventListener("pointercancel", release);
el.addEventListener("lostpointercapture", release);
el.addEventListener("contextmenu", (e) => e.preventDefault());
}
bindTap(el, fn) {
if (!el) return;
el.addEventListener(
"pointerdown",
(e) => {
e.preventDefault();
e.stopPropagation();
el.classList.add("on");
setTimeout(() => el.classList.remove("on"), 120);
fn();
},
{ passive: false }
);
el.addEventListener("contextmenu", (e) => e.preventDefault());
}
/* ------------------------------- 滑动瞄准 ------------------------------- */
bindLook() {
const surface = document.getElementById("gl");
if (!surface) return;
surface.addEventListener(
"pointerdown",
(e) => {
if (e.pointerType === "mouse") return;
if (this.lookId !== null) return;
e.preventDefault();
this.lookId = e.pointerId;
this.lx = e.clientX;
this.ly = e.clientY;
try {
surface.setPointerCapture(e.pointerId);
} catch {
/* ignore */
}
},
{ passive: false }
);
surface.addEventListener(
"pointermove",
(e) => {
if (e.pointerId !== this.lookId) return;
e.preventDefault();
this.look.x += e.clientX - this.lx;
this.look.y += e.clientY - this.ly;
this.lx = e.clientX;
this.ly = e.clientY;
},
{ passive: false }
);
const stop = (e) => {
if (e.pointerId !== this.lookId) return;
this.lookId = null;
};
surface.addEventListener("pointerup", stop);
surface.addEventListener("pointercancel", stop);
surface.addEventListener("contextmenu", (e) => e.preventDefault());
}
/** 切后台 / 失焦时松开所有输入,避免"卡住一直往前跑" */
watchBlur() {
const release = () => {
this.fireHeld = false;
this.zoomHeld = false;
this.crouchHeld = false;
this.move.x = 0;
this.move.y = 0;
this.stickId = null;
this.lookId = null;
if (this.knob) this.knob.style.transform = "translate(-50%, -50%)";
for (const el of document.querySelectorAll(".tbtn.on")) el.classList.remove("on");
};
window.addEventListener("blur", release);
document.addEventListener("visibilitychange", () => {
if (document.hidden) release();
});
}
/* -------------------------------- 读取端 -------------------------------- */
/** 取走这一帧的滑动量(像素) */
consumeLook() {
const d = { x: this.look.x, y: this.look.y };
this.look.x = 0;
this.look.y = 0;
return d;
}
takeJump() {
const v = this.jumpQueued;
this.jumpQueued = false;
return v;
}
takeReload() {
const v = this.reloadQueued;
this.reloadQueued = false;
return v;
}
takeSwitch() {
const v = this.switchQueued;
this.switchQueued = false;
return v;
}
takeMenu() {
const v = this.menuQueued;
this.menuQueued = false;
return v;
}
}