// 触屏操作层:左摇杆移动、右半屏滑动瞄准、开火 / 跳 / 换弹 / 切枪 / 菜单。 // 只在粗指针(手机、平板)上启用,桌面仍然走鼠标锁定 + 键盘。 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; } }