- 服务端 60Hz 固定步长模拟:重力/摩擦/轴分离碰撞/上台阶,客户端预测回滚 - 命中裁决:射线打墙打人(身体 + 头部盒),射速/弹匣/换弹/伤害/比分全在服务端 - 回合制:3 秒准备、先到 8 分、100 秒平局;观战自由视角 - 机器人陪练:视线检测 + 反应延迟 + 点射偏差,能单人开练 - 前端零依赖:手写 WebGL(程序化贴图、地图合批、粒子、曳光弹、假投影、黄昏天空) - 2D Canvas 枪模 + WebAudio 合成音效,无任何图片/音频/CDN 资源 - 地图 level.json 前后端同源,tools/gen_level.py 四分镜像生成,绝对对称 - 测试:14 组规则单测 + 6 组联机 e2e
200 lines
4.8 KiB
JavaScript
200 lines
4.8 KiB
JavaScript
// 角色移动物理 —— src/physics.rs 的 JS 镜像。
|
|
// 客户端预测与服务端用同一套常数和同样的积分顺序,服务端快照才是权威。
|
|
|
|
export const MOVE = {
|
|
gravity: 18,
|
|
jump: 6.2,
|
|
run: 5.6,
|
|
walk: 2.7,
|
|
accelGround: 11,
|
|
accelAir: 2.2,
|
|
friction: 8,
|
|
radius: 0.36,
|
|
height: 1.75,
|
|
eye: 1.62,
|
|
stepUp: 0.55,
|
|
maxFall: 55,
|
|
};
|
|
|
|
export const K = { FWD: 1, BACK: 2, LEFT: 4, RIGHT: 8, JUMP: 16, WALK: 32 };
|
|
|
|
export function makeBody(x, y, z) {
|
|
return { pos: { x, y, z }, vel: { x: 0, y: 0, z: 0 }, onGround: false };
|
|
}
|
|
|
|
export function eyeOf(pos) {
|
|
return { x: pos.x, y: pos.y + MOVE.eye, z: pos.z };
|
|
}
|
|
|
|
function blockerAt(level, pos) {
|
|
const R = MOVE.radius;
|
|
const minx = pos.x - R;
|
|
const maxx = pos.x + R;
|
|
const miny = pos.y;
|
|
const maxy = pos.y + MOVE.height;
|
|
const minz = pos.z - R;
|
|
const maxz = pos.z + R;
|
|
const boxes = level.boxes;
|
|
for (let i = 0; i < boxes.length; i++) {
|
|
const b = boxes[i];
|
|
if (
|
|
b.min[0] < maxx &&
|
|
b.max[0] > minx &&
|
|
b.min[1] < maxy &&
|
|
b.max[1] > miny &&
|
|
b.min[2] < maxz &&
|
|
b.max[2] > minz
|
|
) {
|
|
return b;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function moveAxis(body, axis, delta, level) {
|
|
if (Math.abs(delta) < 1e-6) return;
|
|
const steps = Math.max(1, Math.ceil(Math.abs(delta) / 0.2));
|
|
const d = delta / steps;
|
|
for (let i = 0; i < steps; i++) {
|
|
if (axis === 0) body.pos.x += d;
|
|
else if (axis === 2) body.pos.z += d;
|
|
else body.pos.y += d;
|
|
|
|
const b = blockerAt(level, body.pos);
|
|
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;
|
|
body.pos = saved;
|
|
}
|
|
|
|
if (axis === 0) {
|
|
body.pos.x = d > 0 ? b.min[0] - MOVE.radius - 1e-3 : b.max[0] + MOVE.radius + 1e-3;
|
|
body.vel.x = 0;
|
|
} else if (axis === 2) {
|
|
body.pos.z = d > 0 ? b.min[2] - MOVE.radius - 1e-3 : b.max[2] + MOVE.radius + 1e-3;
|
|
body.vel.z = 0;
|
|
} else {
|
|
if (d <= 0) {
|
|
body.pos.y = b.max[1];
|
|
body.onGround = true;
|
|
} else {
|
|
body.pos.y = b.min[1] - MOVE.height - 1e-3;
|
|
}
|
|
body.vel.y = 0;
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
export function stepBody(body, keys, yaw, level, dt) {
|
|
const sy = Math.sin(yaw);
|
|
const cy = Math.cos(yaw);
|
|
|
|
let wx = 0;
|
|
let wz = 0;
|
|
if (keys & K.FWD) {
|
|
wx += sy;
|
|
wz -= cy;
|
|
}
|
|
if (keys & K.BACK) {
|
|
wx -= sy;
|
|
wz += cy;
|
|
}
|
|
if (keys & K.RIGHT) {
|
|
wx += cy;
|
|
wz += sy;
|
|
}
|
|
if (keys & K.LEFT) {
|
|
wx -= cy;
|
|
wz -= sy;
|
|
}
|
|
|
|
const wishLen = Math.hypot(wx, wz);
|
|
const maxSpeed = keys & K.WALK ? MOVE.walk : MOVE.run;
|
|
|
|
if (wishLen > 1e-4) {
|
|
const nx = wx / wishLen;
|
|
const nz = wz / wishLen;
|
|
const accel = body.onGround ? MOVE.accelGround : MOVE.accelAir;
|
|
const current = body.vel.x * nx + body.vel.z * nz;
|
|
const add = Math.max(0, Math.min(accel * maxSpeed * dt, maxSpeed - current));
|
|
body.vel.x += nx * add;
|
|
body.vel.z += nz * add;
|
|
} else if (body.onGround) {
|
|
const speed = Math.hypot(body.vel.x, body.vel.z);
|
|
if (speed > 0.02) {
|
|
const k = Math.max(0, (speed - MOVE.friction * dt) / speed);
|
|
body.vel.x *= k;
|
|
body.vel.z *= k;
|
|
} else {
|
|
body.vel.x = 0;
|
|
body.vel.z = 0;
|
|
}
|
|
}
|
|
|
|
if (keys & K.JUMP && body.onGround) {
|
|
body.vel.y = MOVE.jump;
|
|
body.onGround = false;
|
|
}
|
|
|
|
body.vel.y = Math.max(body.vel.y - MOVE.gravity * dt, -MOVE.maxFall);
|
|
|
|
moveAxis(body, 0, body.vel.x * dt, level);
|
|
moveAxis(body, 2, body.vel.z * dt, level);
|
|
|
|
body.onGround = false;
|
|
moveAxis(body, 1, body.vel.y * dt, level);
|
|
|
|
let guard = 0;
|
|
while (blockerAt(level, body.pos) && guard < 40) {
|
|
body.pos.y += 0.1;
|
|
guard++;
|
|
}
|
|
return body;
|
|
}
|
|
|
|
// 视线检测(调试 / 观战提示用)
|
|
export function hasLineOfSight(level, from, to) {
|
|
const dx = to.x - from.x;
|
|
const dy = to.y - from.y;
|
|
const dz = to.z - from.z;
|
|
const len = Math.hypot(dx, dy, dz);
|
|
if (len < 1e-4) return true;
|
|
const nx = dx / len;
|
|
const ny = dy / len;
|
|
const nz = dz / len;
|
|
const maxT = Math.max(0, len - 0.2);
|
|
for (const b of level.boxes) {
|
|
let t0 = 0;
|
|
let t1 = maxT;
|
|
let ok = true;
|
|
const o = [from.x, from.y, from.z];
|
|
const d = [nx, ny, nz];
|
|
for (let i = 0; i < 3; i++) {
|
|
if (Math.abs(d[i]) < 1e-8) {
|
|
if (o[i] < b.min[i] || o[i] > b.max[i]) {
|
|
ok = false;
|
|
break;
|
|
}
|
|
} else {
|
|
const inv = 1 / d[i];
|
|
let ta = (b.min[i] - o[i]) * inv;
|
|
let tb = (b.max[i] - o[i]) * inv;
|
|
if (ta > tb) [ta, tb] = [tb, ta];
|
|
t0 = Math.max(t0, ta);
|
|
t1 = Math.min(t1, tb);
|
|
if (t0 > t1) {
|
|
ok = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (ok) return false;
|
|
}
|
|
return true;
|
|
}
|