diff --git a/README.md b/README.md index 3d6ab0c..8f9978c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ cargo run --release # 或 cargo run 走 debug,编译更快 | 右半屏滑动 | 转视角(拖动多少转多少,跟灵敏度挂钩) | | 右下「开火」 | 按住连发;轻点也保证打出一发 | | 「跳」 | 跳(能上 1 米矮台) | +| 「开镜」 | 按住放大(放大镜):走得慢、子弹更聚 | +| 「蹲下」 | 按住蹲:盒子变矮、没脚步声,松键才起身 | | 「换弹」 | 换弹 | | 「切枪」 | 步枪 / 手枪互换 | | 右上「≡」 | 打开菜单(继续 / 灵敏度 / 离开) | @@ -92,6 +94,8 @@ location / { | `Shift` | 静步(更慢、更稳、没脚步声) | | `空格` | 跳跃(能跳上 1 米矮台) | | 鼠标左键 | 开火(步枪按住连发,手枪单发) | +| 鼠标右键 | 开镜放大(放大镜,按住生效:移速减半、散布大减) | +| `C` / `Ctrl` | 蹲下(按住生效:碰撞体 1.75→1.05、没脚步声;头顶有东西站不起来) | | `R` | 换弹 | | `1` / `2` | 切步枪 / 手枪 | | `Tab` | 记分板 | @@ -105,6 +109,8 @@ location / { - 单回合 100 秒打不完算平局,双方都不加分。 - 步枪 30 发 / 备用 90,手枪 7 发 / 备用 35;换弹有 2 秒多空窗,别在对手冲脸时换。 - 移动、腾空会放大散布;后坐力会抬枪口,需要自己压枪。 +- **右键开镜(放大镜)**:FOV 78→30(步枪)/ 42(手枪),移速减半、散布乘以 0.35 / 0.5,灵敏度随 FOV 一起缩放,远点对枪不吃亏。 +- **C / Ctrl 蹲下**:碰撞体从 1.75 压到 1.05,命中盒跟着矮,蹲走只有 1.5 m/s 且没脚步声;头顶有东西时松键也站不起来。 - 爆头、击杀、被击杀都有独立音效和画面反馈(命中标记、受击红晕、击杀播报)。 ## 代码结构 @@ -200,7 +206,7 @@ cargo run # 起服务 cargo test ``` -- `game::tests` —— 14 组规则断言:出生点不卡墙、射线挡墙、冻结 3 秒、前进/摩擦、命中盒分头身、射速限制、击杀加分与回合重开、8 分结束、弹匣打空、换弹、机器人会动、观战占位、中离暂停、输入队列防加速。 +- `game::tests` —— 17 组规则断言:出生点不卡墙、射线挡墙、冻结 3 秒、前进/摩擦、命中盒分头身、射速限制、击杀加分与回合重开、8 分结束、弹匣打空、换弹、机器人会动、观战占位、中离暂停、输入队列防加速、蹲下变矮且命中盒跟着变、蹲下和开镜都会减速、矮洞底下站不起来。 - `tests/e2e.rs` —— 6 组联机断言:建房→加入→冻结→开打→移动生效、观战者看得到但动不了、机器人会出门、狂发 200 帧输入不会瞬移、未知房号报错、`/health` 与静态资源。 单跑某一组:`cargo test --test e2e` / `cargo test 击杀`。 diff --git a/public/index.html b/public/index.html index 20bca4a..110c14c 100644 --- a/public/index.html +++ b/public/index.html @@ -18,6 +18,13 @@ + +
+
+ +
+
+
@@ -71,7 +78,7 @@ / 90
-
1 步枪 · 2 手枪 · R 换弹
+
1 步枪 · 2 手枪 · R 换弹 · 右键开镜 · C 蹲下
@@ -142,6 +149,8 @@
跳
换弹
切枪
+
开镜
+
蹲下
≡
@@ -210,6 +219,7 @@ diff --git a/public/js/client/hud.js b/public/js/client/hud.js index 63e9ef9..0f59436 100644 --- a/public/js/client/hud.js +++ b/public/js/client/hud.js @@ -16,6 +16,8 @@ export class Hud { ping: $("ping"), specBadge: $("specBadge"), crosshair: $("crosshair"), + scope: $("scope"), + stance: $("stanceTag"), hitmarker: $("hitmarker"), dmgFlash: $("dmgFlash"), hp: $("hp"), @@ -103,6 +105,17 @@ export class Hud { 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"); diff --git a/public/js/client/main.js b/public/js/client/main.js index a25cd8f..119d5ef 100644 --- a/public/js/client/main.js +++ b/public/js/client/main.js @@ -75,6 +75,11 @@ const S = { drop: 0, walkPhase: 0, reloadPulse: 0, + // 放大镜(开镜):FOV 平滑收缩 + 姿势插值量 + fov: 78, + ads: 0, + // 蹲下:视点高度平滑过渡量(0 = 站立,1 = 全蹲) + crouchAmt: 0, // 观战 spec: { pos: [0, 13, 24], yaw: 0, pitch: -0.4 }, // 特效 @@ -251,6 +256,14 @@ async function enterGame(msg) { S.reserve = S.reserves[0]; S.reloadLeft = 0; S.nextFire = 0; + S.fov = 78; + S.ads = 0; + S.crouchAmt = 0; + mouse.right = false; + if (touch.enabled) { + touch.zoomHeld = false; + touch.crouchHeld = false; + } const yaw = S.yaw; S.spec.pos = [sp[0] - Math.sin(yaw) * 6, sp[1] + 6, sp[2] + Math.cos(yaw) * 6]; @@ -418,6 +431,9 @@ function reconcile(me) { const body = makeBody(me.pos[0], me.pos[1], me.pos[2]); body.vel = { x: me.vel[0], y: me.vel[1], z: me.vel[2] }; body.onGround = !!me.ground; + // 蹲伏状态以服务端为准:丢包 / 低帧率下历史可能被 Ack 吃空, + // 不恢复的话 body.crouch 会被 makeBody 抹掉,视点平滑就丢了 + body.crouch = !!me.crouch; if (S.phase === "live" && me.alive && S.level) { for (const h of S.history) { @@ -483,6 +499,9 @@ function onRound(r) { S.drop = 0; S.recoil = { pitch: 0, yaw: 0 }; S.reloadLeft = 0; + S.fov = 78; + S.ads = 0; + mouse.right = false; S.mag = WEAPONS[0].mag; S.reserve = WEAPONS[0].reserve; S.weapon = 0; @@ -516,15 +535,20 @@ function onMatch(m) { /* ----------------------------------- 输入 ----------------------------------- */ -const keys = { fwd: false, back: false, left: false, right: false, jump: false, walk: false }; -const mouse = { left: false }; +const keys = { fwd: false, back: false, left: false, right: false, jump: false, walk: false, crouch: false }; +const mouse = { left: false, right: false }; /** 鼠标左键或触屏「开火」都算开火 */ function isFiring() { return mouse.left || (touch.enabled && touch.fireHeld); } -/** 触屏摇杆 / 跳跃 → 键位掩码 */ +/** 放大镜:桌面按住右键,触屏按住「开镜」按钮 */ +function zoomHeld() { + return mouse.right || (touch.enabled && touch.zoomHeld); +} + +/** 触屏摇杆 / 跳跃 / 蹲下 / 开镜 → 键位掩码 */ function touchMask() { let k = 0; const m = touch.move; @@ -533,6 +557,8 @@ function touchMask() { if (m.x < -0.3) k |= K.LEFT; if (m.x > 0.3) k |= K.RIGHT; if (performance.now() < S.jumpUntil) k |= K.JUMP; + if (touch.crouchHeld) k |= K.CROUCH; + if (touch.zoomHeld) k |= K.ZOOM; return k; } @@ -544,6 +570,7 @@ function keyMask() { if (keys.right) k |= K.RIGHT; if (keys.jump) k |= K.JUMP; if (keys.walk) k |= K.WALK; + if (keys.crouch) k |= K.CROUCH; return k; } @@ -581,6 +608,12 @@ window.addEventListener("keydown", (e) => { case "ShiftRight": keys.walk = true; break; + case "ControlLeft": + case "ControlRight": + case "KeyC": + e.preventDefault(); + keys.crouch = true; + break; case "KeyR": startReload(); break; @@ -619,6 +652,11 @@ window.addEventListener("keyup", (e) => { case "ShiftRight": keys.walk = false; break; + case "ControlLeft": + case "ControlRight": + case "KeyC": + keys.crouch = false; + break; default: break; } @@ -631,23 +669,33 @@ canvas.addEventListener("mousedown", (e) => { canvas.requestPointerLock()?.catch?.(() => {}); return; } + // 右键:按住开镜(放大镜) + if (e.button === 2) { + mouse.right = true; + return; + } if (e.button === 0) { mouse.left = true; S.firedThisPress = false; } }); +// 右键别弹系统菜单,挡住开镜 +canvas.addEventListener("contextmenu", (e) => e.preventDefault()); + window.addEventListener("mouseup", (e) => { if (e.button === 0) { mouse.left = false; S.firedThisPress = false; } + if (e.button === 2) mouse.right = false; }); document.addEventListener("mousemove", (e) => { if (S.scene !== "game") return; if (!S.locked) return; - const k = 0.0021 * S.sens; + // 灵敏度跟着 FOV 缩放:开镜后转得慢,放大镜里才瞄得准 + const k = 0.0021 * S.sens * (S.fov / 78); if (S.spectating) { S.spec.yaw += e.movementX * k; S.spec.pitch = clamp(S.spec.pitch - e.movementY * k, -1.5, 1.5); @@ -819,11 +867,20 @@ function frame(now) { } function updateGame(dt) { + // 放大镜:FOV 平滑收缩,灵敏度按比例缩放;姿势插值量给枪模用 + const zooming = zoomHeld(); + const wantFov = zooming ? (S.spectating ? 40 : weaponOf(S.weapon).zoomFov) : 78; + S.fov += (wantFov - S.fov) * (1 - Math.exp(-16 * dt)); + S.ads += ((zooming ? 1 : 0) - S.ads) * (1 - Math.exp(-18 * dt)); + // 蹲下:视点高度平滑过渡,别瞬移 + const crouching = !!(S.body && S.body.crouch); + S.crouchAmt += ((crouching ? 1 : 0) - S.crouchAmt) * (1 - Math.exp(-14 * dt)); + // 触屏:滑动瞄准 + 跳跃 / 换弹 / 切枪 / 菜单 if (touch.enabled) { const d = touch.consumeLook(); if (d.x || d.y) { - const k = 0.0021 * S.sens * 1.45; + const k = 0.0021 * S.sens * 1.45 * (S.fov / 78); if (S.spectating) { S.spec.yaw += d.x * k; S.spec.pitch = clamp(S.spec.pitch - d.y * k, -1.5, 1.5); @@ -887,7 +944,9 @@ function updateGame(dt) { function updatePlayer(dt) { const live = S.phase === "live" && S.alive; - const keysNow = live ? keyMask() | (touch.enabled ? touchMask() : 0) : 0; + const keysNow = live + ? keyMask() | (touch.enabled ? touchMask() : 0) | (zoomHeld() ? K.ZOOM : 0) + : 0; acc += dt; let steps = 0; @@ -938,12 +997,12 @@ function updatePlayer(dt) { // 打空自动换弹 if (S.mag === 0 && S.reloadLeft <= 0 && S.reserve > 0) startReload(); - // 脚步声 + // 脚步声:蹲下走没脚步声(CS 传统艺能) if (live && S.body.onGround && speed > 1.2) { S.stepTimer = (S.stepTimer || 0) - dt * speed; if (S.stepTimer <= 0) { S.stepTimer = 3.4; - if (!keys.walk) sound.step(); + if (!keys.walk && !keys.crouch) sound.step(); } } } @@ -955,7 +1014,7 @@ function updateSpectator(dt) { const cp = Math.cos(pitch); const fwd = [Math.sin(yaw) * cp, Math.sin(pitch), -Math.cos(yaw) * cp]; const right = [Math.cos(yaw), 0, Math.sin(yaw)]; - const speed = (k & K.WALK ? 26 : 10) * dt; + const speed = (k & K.WALK ? 18 : 7) * dt; const pos = S.spec.pos; if (k & K.FWD) { pos[0] += fwd[0] * speed; @@ -983,9 +1042,11 @@ function cameraPos() { if (S.spectating) return S.spec.pos; if (!S.body) return [0, 2, 0]; if (!Number.isFinite(S.body.pos.x + S.err.x)) return [0, 2, 0]; + // 蹲下时视点从 1.62 平滑降到 0.95 + const eye = MOVE.eye + (MOVE.crouchEye - MOVE.eye) * S.crouchAmt; return [ S.body.pos.x + S.err.x, - S.body.pos.y + MOVE.eye + S.err.y - S.drop * 1.15, + S.body.pos.y + eye + S.err.y - S.drop * 1.15, S.body.pos.z + S.err.z, ]; } @@ -1021,6 +1082,7 @@ function drawGame() { alive: p.alive, bot: p.bot, hp: p.hp, + crouch: p.crouch, }; }); @@ -1028,7 +1090,7 @@ function drawGame() { pos, yaw, pitch, - fov: 78, + fov: S.fov, players, selfSlot: S.you, spectating: S.spectating, @@ -1038,11 +1100,15 @@ function drawGame() { tracers: S.tracers, }); - // 准星张合:移动 / 腾空 / 后坐力 + // 开镜时普通准星下岗,放大镜环 + 镜内准星上岗;蹲下亮一个小标签 + hud.setZoom(zoomingNow()); + hud.setStance(S.crouchAmt > 0.5); + + // 准星张合:移动 / 腾空 / 后坐力(开镜时由镜内准星接管,这里不算了) const speed = S.body ? Math.hypot(S.body.vel.x, S.body.vel.z) : 0; const air = S.body && !S.body.onGround ? 1 : 0; const spread = weaponOf(S.weapon).spread * 6 + speed * 0.12 + air * 0.5 + Math.abs(S.recoil.pitch) * 6; - hud.setCrosshair(spread); + if (!zoomingNow()) hud.setCrosshair(spread); if (S.spectating) { viewmodel.clear(); @@ -1055,10 +1121,17 @@ function drawGame() { swayX: S.vm.swayX, muzzle: S.muzzle, alive: S.alive && !S.matchOver, + ads: S.ads, + crouch: S.crouchAmt, }); } } +/** 当前是否处于开镜(放大镜)状态 */ +function zoomingNow() { + return zoomHeld() && S.scene === "game"; +} + // 大厅背景:围着地图慢慢转,顺便确认渲染正常 function drawLobbyCam() { if (!S.level) return; diff --git a/public/js/client/movement.js b/public/js/client/movement.js index 40f2b10..2f42ace 100644 --- a/public/js/client/movement.js +++ b/public/js/client/movement.js @@ -4,34 +4,39 @@ export const MOVE = { gravity: 18, jump: 6.2, - run: 5.6, - walk: 2.7, + run: 4.6, + walk: 2.0, + crouch: 1.5, accelGround: 11, accelAir: 2.2, friction: 8, radius: 0.36, height: 1.75, eye: 1.62, + crouchHeight: 1.05, + crouchEye: 0.95, stepUp: 0.55, maxFall: 55, + zoomSpeedMul: 0.55, }; -export const K = { FWD: 1, BACK: 2, LEFT: 4, RIGHT: 8, JUMP: 16, WALK: 32 }; +export const K = { FWD: 1, BACK: 2, LEFT: 4, RIGHT: 8, JUMP: 16, WALK: 32, CROUCH: 64, ZOOM: 128 }; export function makeBody(x, y, z) { - return { pos: { x, y, z }, vel: { x: 0, y: 0, z: 0 }, onGround: false }; + return { pos: { x, y, z }, vel: { x: 0, y: 0, z: 0 }, onGround: false, crouch: false }; } export function eyeOf(pos) { return { x: pos.x, y: pos.y + MOVE.eye, z: pos.z }; } -function blockerAt(level, pos) { +function blockerAt(level, pos, crouch) { const R = MOVE.radius; + const H = crouch ? MOVE.crouchHeight : MOVE.height; const minx = pos.x - R; const maxx = pos.x + R; const miny = pos.y; - const maxy = pos.y + MOVE.height; + const maxy = pos.y + H; const minz = pos.z - R; const maxz = pos.z + R; const boxes = level.boxes; @@ -60,14 +65,14 @@ function moveAxis(body, axis, delta, level) { else if (axis === 2) body.pos.z += d; else body.pos.y += d; - const b = blockerAt(level, body.pos); + const b = blockerAt(level, body.pos, body.crouch); if (!b) continue; // 水平撞到矮台面:抬腿上台阶 if (axis !== 1 && b.max[1] > body.pos.y && b.max[1] - body.pos.y <= MOVE.stepUp) { const saved = { ...body.pos }; body.pos.y = b.max[1] + 1e-3; - if (!blockerAt(level, body.pos)) continue; + if (!blockerAt(level, body.pos, body.crouch)) continue; body.pos = saved; } @@ -82,7 +87,7 @@ function moveAxis(body, axis, delta, level) { body.pos.y = b.max[1]; body.onGround = true; } else { - body.pos.y = b.min[1] - MOVE.height - 1e-3; + body.pos.y = b.min[1] - (body.crouch ? MOVE.crouchHeight : MOVE.height) - 1e-3; } body.vel.y = 0; } @@ -91,6 +96,14 @@ function moveAxis(body, axis, delta, level) { } export function stepBody(body, keys, yaw, level, dt) { + // 蹲 / 站:按住就蹲;松键时头顶要过得去才站得起来(与服务端同序) + if (keys & K.CROUCH) { + body.crouch = true; + } else if (body.crouch) { + body.crouch = false; + if (blockerAt(level, body.pos, false)) body.crouch = true; + } + const sy = Math.sin(yaw); const cy = Math.cos(yaw); @@ -114,7 +127,8 @@ export function stepBody(body, keys, yaw, level, dt) { } const wishLen = Math.hypot(wx, wz); - const maxSpeed = keys & K.WALK ? MOVE.walk : MOVE.run; + let maxSpeed = keys & K.CROUCH ? Math.min(MOVE.crouch, MOVE.walk) : keys & K.WALK ? MOVE.walk : MOVE.run; + if (keys & K.ZOOM) maxSpeed *= MOVE.zoomSpeedMul; if (wishLen > 1e-4) { const nx = wx / wishLen; @@ -150,7 +164,7 @@ export function stepBody(body, keys, yaw, level, dt) { moveAxis(body, 1, body.vel.y * dt, level); let guard = 0; - while (blockerAt(level, body.pos) && guard < 40) { + while (blockerAt(level, body.pos, body.crouch) && guard < 40) { body.pos.y += 0.1; guard++; } diff --git a/public/js/client/renderer.js b/public/js/client/renderer.js index 5d496b5..e436805 100644 --- a/public/js/client/renderer.js +++ b/public/js/client/renderer.js @@ -815,7 +815,12 @@ export class Renderer { for (const p of s.players) { if (!p.alive) continue; if (p.slot === s.selfSlot && !s.spectating) continue; - const base = mul(translate(p.pos[0], p.pos[1], p.pos[2]), rotY(-p.yaw)); + // 蹲下:整只模型沿 Y 压扁到 1.05/1.75 ≈ 0.6,和碰撞体对齐 + const sy = p.crouch ? 0.6 : 1; + const base = mul( + translate(p.pos[0], p.pos[1], p.pos[2]), + mul(rotY(-p.yaw), scale(1, sy, 1)) + ); const nm = yawNormalMat(p.yaw); const leg = [0.2, 0.22, 0.26]; const team = p.bot ? [0.8, 0.4, 0.2] : [0.86, 0.33, 0.16]; diff --git a/public/js/client/touch.js b/public/js/client/touch.js index 83d8ff3..27367b3 100644 --- a/public/js/client/touch.js +++ b/public/js/client/touch.js @@ -12,6 +12,8 @@ export class Touch { this.move = { x: 0, y: 0 }; this.fireHeld = false; + this.zoomHeld = false; + this.crouchHeld = false; this.jumpQueued = false; this.reloadQueued = false; this.switchQueued = false; @@ -56,6 +58,26 @@ export class Touch { 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(); } @@ -265,6 +287,8 @@ export class Touch { watchBlur() { const release = () => { this.fireHeld = false; + this.zoomHeld = false; + this.crouchHeld = false; this.move.x = 0; this.move.y = 0; this.stickId = null; diff --git a/public/js/client/viewmodel.js b/public/js/client/viewmodel.js index 12d71b7..d7e3383 100644 --- a/public/js/client/viewmodel.js +++ b/public/js/client/viewmodel.js @@ -42,7 +42,7 @@ export class ViewModel { this.g.clearRect(0, 0, this.canvas.width, this.canvas.height); } - /** s = {weapon, bobX, bobY, kick, swayX, muzzle, alive} */ + /** s = {weapon, bobX, bobY, kick, swayX, muzzle, alive, ads, crouch} */ render(s) { this.resize(); const g = this.g; @@ -52,17 +52,22 @@ export class ViewModel { const W = this.canvas.width; const H = this.canvas.height; const touch = document.body.classList.contains("touch"); - const scale = Math.min(W / 1280, H / 720) * (touch ? 0.66 : 0.78); + const ads = s.ads || 0; + const crouch = s.crouch || 0; + // 开镜:枪身收窄、角度放平、往屏幕中心收;蹲下:整把枪随视点下沉 + const scale = Math.min(W / 1280, H / 720) * (touch ? 0.66 : 0.78) * (1 - ads * 0.12); const kick = s.kick || 0; g.save(); // 锚点在枪口,枪身往右下角延伸 - // 锚点放在枪口(局部坐标 14,100),枪身向右下延伸 g.translate( - W * (touch ? 0.55 : 0.52) + (s.bobX || 0) * 900 + (s.swayX || 0) * 500 + kick * 22, - H * (touch ? 0.63 : 0.58) + (s.bobY || 0) * 900 + kick * 9 + W * ((touch ? 0.55 : 0.52) - ads * 0.08) + + (s.bobX || 0) * 900 * (1 - ads * 0.6) + + (s.swayX || 0) * 500 * (1 - ads * 0.6) + + kick * 22, + H * ((touch ? 0.63 : 0.58) + crouch * 0.04) + (s.bobY || 0) * 900 + kick * 9 ); - g.rotate(0.3 + kick * 0.022); + g.rotate(0.3 - ads * 0.24 + kick * 0.022); g.scale(scale, scale); const art = this.bake(s.weapon); diff --git a/public/js/client/weapons.js b/public/js/client/weapons.js index 20f48a7..909188f 100644 --- a/public/js/client/weapons.js +++ b/public/js/client/weapons.js @@ -15,6 +15,8 @@ export const WEAPONS = [ reload: 2.4, spread: 0.0075, kick: 0.014, + zoomFov: 30, + zoomMul: 0.35, }, { id: PISTOL, @@ -26,6 +28,8 @@ export const WEAPONS = [ reload: 2.1, spread: 0.0045, kick: 0.028, + zoomFov: 42, + zoomMul: 0.5, }, ]; diff --git a/public/style.css b/public/style.css index 7152ef3..8148e5d 100644 --- a/public/style.css +++ b/public/style.css @@ -535,6 +535,77 @@ input[type="range"]::-webkit-slider-thumb { letter-spacing: 0.08em; } +/* 蹲下状态小标签 */ +.stance-tag { + color: var(--cyan); + border: 1px solid rgba(56, 225, 255, 0.35); + padding: 2px 7px; + border-radius: 5px; + font-size: 10px; + letter-spacing: 0.08em; +} + +/* 放大镜(开镜):镜环 + 镜内准星,环外压暗 */ +#scope { + position: fixed; + inset: 0; + pointer-events: none; + opacity: 0; + transition: opacity 0.12s ease; + z-index: 6; +} + +#scope.on { + opacity: 1; +} + +#scope .scope-ring { + position: absolute; + left: 50%; + top: 50%; + width: min(62vh, 62vw); + height: min(62vh, 62vw); + transform: translate(-50%, -50%); + border-radius: 50%; + border: 1.5px solid rgba(56, 225, 255, 0.5); + box-shadow: 0 0 0 9999px rgba(3, 8, 12, 0.72), inset 0 0 70px rgba(0, 0, 0, 0.6), + 0 0 26px rgba(56, 225, 255, 0.14); +} + +#scope .scv, +#scope .sch { + position: absolute; + background: rgba(56, 225, 255, 0.45); +} + +#scope .scv { + left: 50%; + top: 10%; + width: 1px; + height: 80%; + transform: translateX(-50%); +} + +#scope .sch { + top: 50%; + left: 10%; + height: 1px; + width: 80%; + transform: translateY(-50%); +} + +#scope .scd { + position: absolute; + left: 50%; + top: 50%; + width: 3px; + height: 3px; + transform: translate(-50%, -50%); + border-radius: 50%; + background: rgba(255, 138, 61, 0.95); + box-shadow: 0 0 7px rgba(255, 138, 61, 0.85); +} + /* 准星 */ #crosshair { left: 50%; @@ -1450,6 +1521,26 @@ body.touch .hud-right { bottom: calc(118px + env(safe-area-inset-bottom, 0px)); } +/* 开镜(放大镜)/ 蹲下:按住生效 */ +#tZoom { + right: calc(96px + env(safe-area-inset-right, 0px)); + bottom: calc(196px + env(safe-area-inset-bottom, 0px)); + color: #d6f4ff; + border-color: rgba(56, 225, 255, 0.45); +} + +#tCrouch { + left: calc(20px + env(safe-area-inset-left, 0px)); + bottom: calc(196px + env(safe-area-inset-bottom, 0px)); + color: #d8e6f2; +} + +#tZoom.on, +#tCrouch.on { + background: radial-gradient(circle at 40% 34%, rgba(56, 225, 255, 0.5), rgba(8, 52, 70, 0.72)); + border-color: rgba(56, 225, 255, 0.9); +} + .tbtn.icon { right: calc(12px + env(safe-area-inset-right, 0px)); top: calc(10px + env(safe-area-inset-top, 0px)); diff --git a/src/game.rs b/src/game.rs index e92fb73..f487c63 100644 --- a/src/game.rs +++ b/src/game.rs @@ -8,7 +8,7 @@ use std::time::Instant; use crate::level::{ray_box, Level}; use crate::math::{forward, v3, wrap_angle, V3}; -use crate::physics::{self, Body, HEIGHT, RADIUS, K_BACK, K_FWD, K_LEFT, K_RIGHT}; +use crate::physics::{self, Body, HEIGHT, RADIUS, K_BACK, K_CROUCH, K_FWD, K_LEFT, K_RIGHT}; use crate::protocol::*; use crate::weapons; @@ -19,8 +19,9 @@ pub const OVER_TIME: f32 = 3.5; pub const ROUND_TIME: f32 = 100.0; /// 输入队列上限:防止一次塞几百帧实现加速 const MAX_QUEUE: usize = 12; -/// 头部命中盒(相对脚底) +/// 头部命中盒下沿(相对脚底),蹲下时整只盒子跟着压矮 const HEAD_FROM: f32 = 1.45; +const HEAD_FROM_CROUCH: f32 = 0.72; /* --------------------------------- 随机数 --------------------------------- */ @@ -99,6 +100,8 @@ pub struct Player { bot_next_burst: f32, bot_bias_yaw: f32, bot_bias_pitch: f32, + bot_crouch: bool, + bot_crouch_until: f32, } impl Player { @@ -135,6 +138,8 @@ impl Player { bot_next_burst: 0.0, bot_bias_yaw: 0.0, bot_bias_pitch: 0.0, + bot_crouch: false, + bot_crouch_until: 0.0, }; p.respawn(level); p @@ -163,6 +168,8 @@ impl Player { self.bot_next_burst = 0.0; self.bot_bias_yaw = 0.0; self.bot_bias_pitch = 0.0; + self.bot_crouch = false; + self.bot_crouch_until = 0.0; } #[inline] @@ -188,16 +195,19 @@ impl Player { } } -/// 命中盒:身体(脚底到胸口上方)+ 头 +/// 命中盒:身体(脚底到胸口上方)+ 头。蹲下时两个盒子都按比例压矮 pub fn hitboxes(p: &Player) -> (V3, V3, V3, V3) { let o = p.body.pos; + let crouch = p.body.crouch; + let head_from = if crouch { HEAD_FROM_CROUCH } else { HEAD_FROM }; + let top = if crouch { physics::CROUCH_HEIGHT } else { HEIGHT }; let body = ( v3(o.x - RADIUS, o.y, o.z - RADIUS), - v3(o.x + RADIUS, o.y + HEAD_FROM, o.z + RADIUS), + v3(o.x + RADIUS, o.y + head_from, o.z + RADIUS), ); let head = ( - v3(o.x - 0.19, o.y + HEAD_FROM, o.z - 0.19), - v3(o.x + 0.19, o.y + HEIGHT, o.z + 0.19), + v3(o.x - 0.19, o.y + head_from, o.z - 0.19), + v3(o.x + 0.19, o.y + top, o.z + 0.19), ); (body.0, body.1, head.0, head.1) } @@ -477,6 +487,7 @@ impl Room { // 第一遍:消费输入 + 移动 + 换弹计时 let mut wants_fire = [false; 2]; + let mut zoomed = [false; 2]; let level = Level::embedded(); for slot in 0..2 { @@ -487,7 +498,13 @@ impl Room { let (keys, yaw, pitch, fire, reload, switch) = if p.bot { let enemy = self.players[1 - slot].as_ref().map(|e| { - (e.body.pos, e.body.eye(), e.alive, e.body.vel.flat_len() > 0.8) + ( + e.body.pos, + e.body.eye(), + e.alive, + e.body.vel.flat_len() > 0.8, + e.body.crouch, + ) }); let f = bot_think(&mut p, enemy, level, self.t, &mut self.rng); (f[0] as u8, f[1], f[2], f[3] != 0.0, f[4] != 0.0, None) @@ -499,6 +516,7 @@ impl Room { // 丢包 / 断流:保持按键但不继续扣扳机 (p.last_keys, p.yaw, p.pitch, false, false, None) }; + zoomed[slot] = keys & physics::K_ZOOM != 0; if let Some(w) = switch { if w != p.weapon && (w as usize) < weapons::WEAPONS.len() { @@ -548,7 +566,7 @@ impl Room { for slot in 0..2 { if wants_fire[slot] { - self.shoot(slot); + self.shoot(slot, zoomed[slot]); } } } @@ -572,7 +590,7 @@ impl Room { dir.add(t1.scale(r * a.cos())).add(t2.scale(r * a.sin())).norm() } - pub fn shoot(&mut self, slot: usize) { + pub fn shoot(&mut self, slot: usize, zoomed: bool) { let level = Level::embedded(); let Some(p) = self.players[slot].as_ref() else { return; @@ -589,10 +607,11 @@ impl Room { let dir0 = forward(p.yaw, p.pitch); let moving = p.body.vel.flat_len() > 1.2; let grounded = p.body.on_ground; + let crouch = p.body.crouch; let bot = p.bot; let wid = p.weapon; - let spread = w.spread + let mut spread = w.spread + if !grounded { 0.05 } else if moving { @@ -600,6 +619,13 @@ impl Room { } else { 0.0 }; + // 蹲下四平八稳,开镜(放大镜)钉死枪口:都按系数收散布 + if crouch { + spread *= 0.55; + } + if zoomed { + spread *= w.zoom_mul; + } let dir = self.spread_dir(dir0, spread); // 先看墙,再看人(取最近的) @@ -720,6 +746,7 @@ impl Room { hp: p.hp, alive: p.alive, ground: p.body.on_ground, + crouch: p.body.crouch, weapon: p.weapon, mag: p.mag_now(), reserve: p.reserve_now(), @@ -776,7 +803,7 @@ fn cross(a: V3, b: V3) -> V3 { /// 返回 [keys, yaw, pitch, fire, reload](为了少定义一个结构体,用数组) fn bot_think( p: &mut Player, - enemy: Option<(V3, V3, bool, bool)>, + enemy: Option<(V3, V3, bool, bool, bool)>, level: &Level, t: f32, rng: &mut Rng, @@ -786,9 +813,10 @@ fn bot_think( let mut reload = false; let eye = p.body.eye(); - if let Some((epos, eeye, alive, _)) = enemy { + if let Some((epos, eeye, alive, _, ecrouch)) = enemy { if alive { - let target = epos.add(v3(0.0, 1.25, 0.0)); + // 蹲着的对手盒子矮一截,瞄胸不瞄头 + let target = epos.add(v3(0.0, if ecrouch { 0.55 } else { 1.25 }, 0.0)); let to = target.sub(eye); let dist = to.flat_len().max(0.3); let want_yaw = to.x.atan2(-to.z) + p.bot_bias_yaw; @@ -818,6 +846,15 @@ fn bot_think( } keys |= p.bot_strafe; + // 时不时蹲一下:盒子变矮不好打,代价是走不快 + if p.bot_crouch_until < t { + p.bot_crouch = rng.unit() > 0.5; + p.bot_crouch_until = t + rng.range(1.0, 2.6); + } + if p.bot_crouch && dist > 3.0 { + keys |= K_CROUCH; + } + if p.bot_burst_left == 0 && t >= p.bot_next_burst { p.bot_burst_left = 3 + (rng.unit() * 3.0) as u8; // 每轮点射换一次手感偏移:会打偏,也会蒙中 @@ -859,7 +896,7 @@ fn bot_think( #[cfg(test)] mod tests { use super::*; - use crate::physics::K_FWD; + use crate::physics::{K_CROUCH, K_FWD, K_ZOOM}; fn two() -> Room { let mut r = Room::new("TEST", 20261022); @@ -955,12 +992,12 @@ mod tests { ticks(&mut r, 3.1); let start = r.players[0].as_ref().unwrap().body.pos; let yaw = r.players[0].as_ref().unwrap().yaw; - for _ in 0..40 { + for _ in 0..60 { push_input(&mut r, 0, K_FWD, yaw, false); r.tick(TICK); } let pos = r.players[0].as_ref().unwrap().body.pos; - assert!(pos.sub(start).flat_len() > 1.5, "没走起来:{pos:?}"); + assert!(pos.sub(start).flat_len() > 2.0, "没走起来:{pos:?}"); } #[test] @@ -997,7 +1034,7 @@ mod tests { fn 开枪扣血且射速有限制() { let mut r = two(); face_off(&mut r); - r.shoot(0); + r.shoot(0, false); let events = r.drain_events(); let shots = events .iter() @@ -1007,7 +1044,7 @@ mod tests { assert!(r.players[1].as_ref().unwrap().hp < 100, "没打中"); // 同一帧再打一枪:射速限制,不该有新子弹 - r.shoot(0); + r.shoot(0, false); let shots2 = r .drain_events() .iter() @@ -1024,7 +1061,7 @@ mod tests { let b = r.players[1].as_mut().unwrap(); b.hp = 5; } - r.shoot(0); + r.shoot(0, false); let events = r.drain_events(); assert!(matches!(events.iter().find(|e| matches!(e, ServerMsg::Kill(_))), Some(_))); assert_eq!(r.players[1].as_ref().unwrap().hp, 0); @@ -1054,7 +1091,7 @@ mod tests { let b = r.players[1].as_mut().unwrap(); b.hp = 5; } - r.shoot(0); + r.shoot(0, false); assert_eq!(r.score, [8, 0]); ticks(&mut r, 4.0); assert_eq!(r.phase, Phase::MatchOver); @@ -1071,11 +1108,11 @@ mod tests { a.mag[0] = 1; a.next_fire = 0.0; } - r.shoot(0); + r.shoot(0, false); r.drain_events(); assert_eq!(r.players[0].as_ref().unwrap().mag_now(), 0); r.t = 200.0; - r.shoot(0); + r.shoot(0, false); let shots = r .drain_events() .iter() @@ -1164,4 +1201,85 @@ mod tests { // 10 帧最多 ~0.9m,不可能瞬间飞到地图另一头 assert!(moved < 1.5, "疑似加速:{moved}"); } + + /// 蹲下变矮、命中盒跟着矮,松键能站起来 + #[test] + fn 蹲下会变矮且命中盒跟着变() { + let mut r = two(); + ticks(&mut r, 3.1); + let yaw = r.players[0].as_ref().unwrap().yaw; + for _ in 0..10 { + push_input(&mut r, 0, K_CROUCH, yaw, false); + r.tick(TICK); + } + { + let p = r.players[0].as_ref().unwrap(); + assert!(p.body.crouch, "按住蹲键应该蹲下"); + let (_, bmax, _, hmax) = hitboxes(p); + let y = p.body.pos.y; + assert!(bmax.y - y < 0.8, "蹲下后躯干不到一米高"); + assert!(hmax.y - y < 1.1, "蹲下后头顶一米出头"); + } + // 松键站起来 + for _ in 0..10 { + push_input(&mut r, 0, 0, yaw, false); + r.tick(TICK); + } + let p = r.players[0].as_ref().unwrap(); + assert!(!p.body.crouch, "松键应该站起来"); + let (_, bmax, _, hmax) = hitboxes(p); + let y = p.body.pos.y; + assert!(bmax.y - y > 1.4 && hmax.y - y > 1.7, "站起来盒子该恢复"); + } + + /// 蹲着、开镜都比站着跑得慢(跑 1 秒的距离做基准) + #[test] + fn 蹲下和开镜都会减速() { + let dist = |keys: u8| -> f32 { + let mut r = two(); + ticks(&mut r, 3.1); + let yaw = r.players[0].as_ref().unwrap().yaw; + let start = r.players[0].as_ref().unwrap().body.pos; + for _ in 0..60 { + push_input(&mut r, 0, keys, yaw, false); + r.tick(TICK); + } + r.players[0].as_ref().unwrap().body.pos.sub(start).flat_len() + }; + let run = dist(K_FWD); + let crouch = dist(K_FWD | K_CROUCH); + let zoom = dist(K_FWD | K_ZOOM); + assert!(run > 2.0, "奔跑 1 秒至少两米:{run}"); + assert!(crouch < run * 0.6, "蹲走 {crouch} 应明显慢于奔跑 {run}"); + assert!(crouch > 0.4, "蹲着也得能走:{crouch}"); + assert!(zoom < run * 0.75, "开镜 {zoom} 应慢于奔跑 {run}"); + assert!(zoom > 0.4, "开镜也得能走:{zoom}"); + } + + /// 矮洞底下蹲得进去,但头顶有东西就是站不起来 + #[test] + fn 矮洞底下站不起来() { + let level = Level::parse( + r#"{"name":"t","size":48, + "spawns":[{"pos":[0,0,0],"yaw":0},{"pos":[10,0,10],"yaw":0}], + "boxes":[ + {"min":[-10,-1,-10],"max":[10,0,10],"mat":0}, + {"min":[-2,1.3,-2],"max":[2,3,2],"mat":0} + ]}"#, + ); + let mut b = Body::new(v3(0.0, 0.0, 0.0)); + let (mn, mx) = b.bounds(); + assert!(level.overlaps(mn, mx), "站着应该被矮洞顶住"); + b.step(K_CROUCH, 0.0, &level, TICK); + assert!(b.crouch, "按住蹲键应该蹲下"); + let (mn, mx) = b.bounds(); + assert!(!level.overlaps(mn, mx), "蹲着应该刚好在洞底下"); + // 松键也站不起来:头顶有东西 + b.step(0, 0.0, &level, TICK); + assert!(b.crouch, "头顶有东西必须保持蹲着"); + // 挪出洞外,松键就能站起来 + b.pos = v3(6.0, 0.0, 6.0); + b.step(0, 0.0, &level, TICK); + assert!(!b.crouch, "离开矮洞应该能站起来"); + } } diff --git a/src/physics.rs b/src/physics.rs index 6e490c7..835bec5 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -1,7 +1,8 @@ //! 角色移动物理。**前端 `public/js/client/movement.js` 是这份代码的 JS 镜像**, //! 客户端预测用同一套常数和同样的积分顺序,服务端才是权威。 //! -//! 碰撞体是一个轴对齐盒子:x/z 半径 0.36,高 1.75,视点 1.62。 +//! 碰撞体是一个轴对齐盒子:站立 x/z 半径 0.36,高 1.75,视点 1.62; +//! 蹲下高 1.05、视点 0.95(`crouch` 标志动态切换,松键起身要先看头顶空间)。 //! 移动按轴分离解算(先 x、再 z、最后 y),撞到不高于 0.55 的台面会自动抬腿上去。 use crate::level::Level; @@ -9,16 +10,22 @@ use crate::math::{flat_forward, right, v3, V3}; pub const GRAVITY: f32 = 18.0; pub const JUMP_VEL: f32 = 6.2; -pub const RUN_SPEED: f32 = 5.6; -pub const WALK_SPEED: f32 = 2.7; +pub const RUN_SPEED: f32 = 4.6; +pub const WALK_SPEED: f32 = 2.0; +pub const CROUCH_SPEED: f32 = 1.5; pub const ACCEL_GROUND: f32 = 11.0; pub const ACCEL_AIR: f32 = 2.2; pub const FRICTION: f32 = 8.0; pub const RADIUS: f32 = 0.36; pub const HEIGHT: f32 = 1.75; pub const EYE: f32 = 1.62; +/// 蹲下时的碰撞体高度 / 视点 +pub const CROUCH_HEIGHT: f32 = 1.05; +pub const CROUCH_EYE: f32 = 0.95; pub const STEP_UP: f32 = 0.55; pub const MAX_FALL: f32 = 55.0; +/// 开镜(放大镜)时的移速系数:瞄得准,就得多付点机动代价 +pub const ZOOM_SPEED_MUL: f32 = 0.55; pub const K_FWD: u8 = 1; pub const K_BACK: u8 = 2; @@ -26,12 +33,15 @@ pub const K_LEFT: u8 = 4; pub const K_RIGHT: u8 = 8; pub const K_JUMP: u8 = 16; pub const K_WALK: u8 = 32; +pub const K_CROUCH: u8 = 64; +pub const K_ZOOM: u8 = 128; #[derive(Clone, Copy, Debug)] pub struct Body { pub pos: V3, pub vel: V3, pub on_ground: bool, + pub crouch: bool, } impl Body { @@ -40,19 +50,31 @@ impl Body { pos, vel: V3::ZERO, on_ground: false, + crouch: false, + } + } + + /// 当前碰撞体高度(蹲下 1.05 / 站立 1.75) + #[inline] + pub fn height(&self) -> f32 { + if self.crouch { + CROUCH_HEIGHT + } else { + HEIGHT } } #[inline] pub fn eye(&self) -> V3 { - self.pos.add(v3(0.0, EYE, 0.0)) + let e = if self.crouch { CROUCH_EYE } else { EYE }; + self.pos.add(v3(0.0, e, 0.0)) } #[inline] pub fn bounds(&self) -> (V3, V3) { ( v3(self.pos.x - RADIUS, self.pos.y, self.pos.z - RADIUS), - v3(self.pos.x + RADIUS, self.pos.y + HEIGHT, self.pos.z + RADIUS), + v3(self.pos.x + RADIUS, self.pos.y + self.height(), self.pos.z + RADIUS), ) } @@ -63,6 +85,16 @@ impl Body { /// 推进一帧。dt 固定 1/60,keys 见上面的 K_* 位 pub fn step(&mut self, keys: u8, yaw: f32, level: &Level, dt: f32) { + // 蹲 / 站:按住就蹲;松键时要头顶过得去才站得起来 + if keys & K_CROUCH != 0 { + self.crouch = true; + } else if self.crouch { + self.crouch = false; + if self.blocker(level).is_some() { + self.crouch = true; // 头顶有东西,蹲着出不来 + } + } + let mut wish = V3::ZERO; if keys & K_FWD != 0 { wish = wish.add(flat_forward(yaw)); @@ -77,11 +109,16 @@ impl Body { wish = wish.sub(right(yaw)); } - let max_speed = if keys & K_WALK != 0 { + let mut max_speed = if keys & K_CROUCH != 0 { + CROUCH_SPEED.min(WALK_SPEED) + } else if keys & K_WALK != 0 { WALK_SPEED } else { RUN_SPEED }; + if keys & K_ZOOM != 0 { + max_speed *= ZOOM_SPEED_MUL; + } if wish.flat_len() > 1e-4 { let wd = wish.norm(); @@ -172,7 +209,7 @@ impl Body { self.pos.y = b.max.y; self.on_ground = true; } else { - self.pos.y = b.min.y - HEIGHT - 1e-3; + self.pos.y = b.min.y - self.height() - 1e-3; } self.vel.y = 0.0; } diff --git a/src/protocol.rs b/src/protocol.rs index 8fa1d54..a849e14 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -81,6 +81,9 @@ pub struct PlayerState { pub hp: i32, pub alive: bool, pub ground: bool, + /// 蹲下中(远端玩家渲染要压矮模型) + #[serde(skip_serializing_if = "is_false")] + pub crouch: bool, pub weapon: u8, pub mag: u32, pub reserve: u32, diff --git a/src/weapons.rs b/src/weapons.rs index 8096e14..4efeac0 100644 --- a/src/weapons.rs +++ b/src/weapons.rs @@ -19,6 +19,8 @@ pub struct Weapon { pub range: f32, /// 后坐力(弧度,主要抬枪口) pub kick: f32, + /// 开镜(放大镜)时的散布系数:瞄得稳,子弹就聚得拢 + pub zoom_mul: f32, } pub const RIFLE: u8 = 0; @@ -38,6 +40,7 @@ pub const WEAPONS: [Weapon; 2] = [ spread: 0.0075, range: 120.0, kick: 0.014, + zoom_mul: 0.35, }, Weapon { id: PISTOL, @@ -52,6 +55,7 @@ pub const WEAPONS: [Weapon; 2] = [ spread: 0.0045, range: 90.0, kick: 0.028, + zoom_mul: 0.5, }, ];