feat: CS 枪火对决 —— Rust 权威服务端 + 手写 WebGL 前端的第一人称 1v1 对枪

- 服务端 60Hz 固定步长模拟:重力/摩擦/轴分离碰撞/上台阶,客户端预测回滚
- 命中裁决:射线打墙打人(身体 + 头部盒),射速/弹匣/换弹/伤害/比分全在服务端
- 回合制:3 秒准备、先到 8 分、100 秒平局;观战自由视角
- 机器人陪练:视线检测 + 反应延迟 + 点射偏差,能单人开练
- 前端零依赖:手写 WebGL(程序化贴图、地图合批、粒子、曳光弹、假投影、黄昏天空)
- 2D Canvas 枪模 + WebAudio 合成音效,无任何图片/音频/CDN 资源
- 地图 level.json 前后端同源,tools/gen_level.py 四分镜像生成,绝对对称
- 测试:14 组规则单测 + 6 组联机 e2e
This commit is contained in:
rong committed 2026-10-22 21:53:45 +08:00
commit 00d63ba736
32 files changed
+8599

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
#!/usr/bin/env python3
"""生成 public/level.json —— 1v1 对枪地图「沙漠据点」。
地图只描述 x<=0、z<=0 的四分之一,然后按 mirror 标记做镜像:
none 不镜像(跨中轴 / 自身对称的方块)
x x -> -x
z z -> -z
xz 两个方向都镜像(四份)
这样地图天然左右 + 前后对称,两边出生点绝对公平。
改完跑:python3 tools/gen_level.py
"""
import json
import os
# (x0, y0, z0, x1, y1, z1, 材质, 镜像)
# 材质: 0 混凝土 / 1 木箱 / 2 金属 / 3 地砖
HALF = [
# 地面与外墙
(-24, -0.6, -24, 24, 0.0, 24, 3, "none"),
(-24, 0.0, -24, -23, 5.2, 24, 0, "x"),
(-24, 0.0, -24, 24, 5.2, -23, 0, "z"),
# 中央高台 + 台上的木箱
(-4.2, 0.0, -4.2, 4.2, 2.0, 4.2, 0, "none"),
(-1.4, 2.0, -1.4, 1.4, 3.2, 1.4, 1, "none"),
# 高台两侧台阶(能走上去)
(-4.2, 0.0, -6.3, 4.2, 0.65, -5.2, 1, "z"),
(-4.2, 0.0, -5.2, 4.2, 1.30, -4.2, 1, "z"),
# 出生点附近的掩体(四角各一份)
(-22.2, 0.0, -21.6, -21.2, 3.0, -15.4, 0, "xz"),
(-17.4, 0.0, -21.8, -15.2, 1.1, -19.6, 1, "xz"),
(-21.4, 0.0, -13.4, -18.6, 2.4, -10.6, 2, "xz"),
(-13.2, 0.0, -21.8, -10.4, 0.9, -20.0, 0, "xz"),
# 西北角的 U 形小屋(开口朝中心)
(-14.2, 0.0, -14.4, -13.2, 3.6, -8.0, 0, "xz"),
(-13.2, 0.0, -14.4, -8.0, 3.6, -13.4, 0, "xz"),
(-11.8, 0.0, -13.4, -8.0, 3.6, -12.6, 0, "xz"),
(-12.4, 0.0, -11.6, -10.4, 1.4, -9.6, 1, "xz"),
(-8.0, 2.6, -13.4, -7.2, 3.0, -12.6, 1, "xz"), # 门楣
# 中场:低木墙、立柱、木箱堆、沙袋
(-8.2, 0.0, -10.2, -2.4, 1.1, -9.2, 1, "xz"),
(-6.6, 0.0, -5.4, -5.6, 5.0, -4.4, 2, "xz"),
(-12.6, 0.0, -4.6, -11.0, 2.0, -3.0, 1, "xz"),
(-6.4, 0.0, -6.4, -2.6, 0.9, -5.5, 0, "xz"),
(-9.8, 0.0, -1.9, -8.2, 1.5, -0.4, 1, "xz"),
# 沿墙矮平台(一跳就能上)
(-23.0, 0.0, -23.0, -8.4, 1.0, -21.2, 2, "xz"),
# 平台到地面的踏步
(-11.0, 0.0, -21.2, -9.6, 0.5, -20.2, 2, "xz"),
]
SPAWNS = [
{"pos": [-18.5, 0.0, -18.5], "yaw": 135.0},
{"pos": [18.5, 0.0, 18.5], "yaw": -45.0},
]
def mirror(box):
x0, y0, z0, x1, y1, z1, mat, mode = box
out = [(x0, y0, z0, x1, y1, z1)]
if "x" in mode:
out += [(-x1, y0, z0, -x0, y1, z1)]
if "z" in mode:
out += [(x0, y0, -z1, x1, y1, -z0) for (x0, y0, z0, x1, y1, z1) in out]
return [(b, mat) for b in out]
def r(v):
return round(v, 3)
def main():
boxes = []
for half in HALF:
for (b, mat) in mirror(half):
boxes.append({
"min": [r(b[0]), r(b[1]), r(b[2])],
"max": [r(b[3]), r(b[4]), r(b[5])],
"mat": mat,
})
# 出生点别被挡住
for sp in SPAWNS:
px, _, pz = sp["pos"]
for b in boxes:
if (b["min"][0] - 0.45 < px < b["max"][0] + 0.45
and b["min"][2] - 0.45 < pz < b["max"][2] + 0.45
and b["max"][1] > 0.2):
raise SystemExit(f"出生点 {px},{pz} 被 {b} 挡住")
data = {
"name": "沙漠据点",
"size": 48,
"spawns": SPAWNS,
"boxes": boxes,
}
path = os.path.join(os.path.dirname(__file__), "..", "public", "level.json")
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, separators=(",", ":"))
f.write("\n")
print(f"写出 {path}:{len(boxes)} 个方块 / {len(SPAWNS)} 个出生点")
if __name__ == "__main__":
main()
+59
View File
@@ -0,0 +1,59 @@
// 本地验证:用系统 Chrome 无头打开页面,截图 + 打印控制台错误 + 游戏状态
const puppeteer = require("puppeteer-core");
(async () => {
const url = process.argv[2] || "http://localhost:3210/";
const out = process.argv[3] || "/tmp/cs-shot.png";
const wait = Number(process.argv[4] || 6000);
const browser = await puppeteer.launch({
executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
headless: "new",
args: [
"--no-sandbox",
"--enable-unsafe-swiftshader",
"--use-gl=angle",
"--use-angle=swiftshader",
"--disable-dev-shm-usage",
"--window-size=1280,760",
],
defaultViewport: { width: 1280, height: 720 },
});
const page = await browser.newPage();
const logs = [];
page.on("console", (m) => logs.push(`[${m.type()}] ${m.text()}`));
page.on("pageerror", (e) => logs.push(`[pageerror] ${e.message}`));
page.on("requestfailed", (r) => logs.push(`[reqfail] ${r.url()} ${r.failure() && r.failure().errorText}`));
await page.goto(url, { waitUntil: "networkidle2" });
await new Promise((r) => setTimeout(r, wait));
await page.screenshot({ path: out });
const info = await page.evaluate(() => {
const S = window.__CS;
if (!S) return { ok: false, why: "no __CS" };
const r = {
scene: S.scene,
phase: S.phase,
you: S.you,
code: S.code,
spectating: S.spectating,
players: S.players.map((p) => ({
slot: p.slot,
name: p.name,
hp: p.hp,
alive: p.alive,
pos: p.pos.map((v) => +v.toFixed(2)),
})),
score: S.score,
hp: S.hp,
mag: S.mag,
alive: S.alive,
fps: S.fps,
pos: S.body ? S.body.pos : null,
particles: S.particles.length,
canvas: [document.getElementById("gl").width, document.getElementById("gl").height],
};
return r;
});
console.log(JSON.stringify(info, null, 2));
if (logs.length) console.log("--- console ---\n" + logs.join("\n"));
await browser.close();
})();