- 服务端 60Hz 固定步长模拟:重力/摩擦/轴分离碰撞/上台阶,客户端预测回滚 - 命中裁决:射线打墙打人(身体 + 头部盒),射速/弹匣/换弹/伤害/比分全在服务端 - 回合制:3 秒准备、先到 8 分、100 秒平局;观战自由视角 - 机器人陪练:视线检测 + 反应延迟 + 点射偏差,能单人开练 - 前端零依赖:手写 WebGL(程序化贴图、地图合批、粒子、曳光弹、假投影、黄昏天空) - 2D Canvas 枪模 + WebAudio 合成音效,无任何图片/音频/CDN 资源 - 地图 level.json 前后端同源,tools/gen_level.py 四分镜像生成,绝对对称 - 测试:14 组规则单测 + 6 组联机 e2e
266 lines
6.8 KiB
JavaScript
266 lines
6.8 KiB
JavaScript
// 第一人称枪模:用 2D Canvas 矢量绘制,比一堆方块更可控也更好看。
|
|
// 只负责表现(后坐力回弹、走位摆动、枪口火光),命中判定全在服务端。
|
|
|
|
const SKIN = "#c99a72";
|
|
const SKIN_DARK = "#a87b55";
|
|
|
|
export class ViewModel {
|
|
constructor(canvas) {
|
|
this.canvas = canvas;
|
|
this.g = canvas.getContext("2d");
|
|
this.dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
this.resize();
|
|
this.last = 0;
|
|
}
|
|
|
|
resize() {
|
|
const w = Math.max(1, Math.round(window.innerWidth * this.dpr));
|
|
const h = Math.max(1, Math.round(window.innerHeight * this.dpr));
|
|
if (this.canvas.width !== w || this.canvas.height !== h) {
|
|
this.canvas.width = w;
|
|
this.canvas.height = h;
|
|
}
|
|
}
|
|
|
|
clear() {
|
|
this.g.setTransform(1, 0, 0, 1, 0, 0);
|
|
this.g.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
}
|
|
|
|
/** s = {weapon: 0|1, bobX, bobY, kick, muzzle, swayX, alive} */
|
|
render(s) {
|
|
this.resize();
|
|
const g = this.g;
|
|
this.clear();
|
|
if (!s || s.alive === false) return;
|
|
|
|
const W = this.canvas.width;
|
|
const H = this.canvas.height;
|
|
const scale = (H / 720) * 1.04;
|
|
// 锚点:枪口大致在画面偏中下,枪身向右下角延伸
|
|
const ax = W * 0.53;
|
|
const ay = H * 0.585;
|
|
const kick = s.kick || 0;
|
|
|
|
g.save();
|
|
g.translate(ax + (s.bobX || 0) * 700 + (s.swayX || 0) * 400 + kick * 26, ay + (s.bobY || 0) * 700 + kick * 10);
|
|
g.rotate(0.115 + kick * 0.02);
|
|
g.scale(scale, scale);
|
|
g.lineJoin = "round";
|
|
|
|
// 枪身投影
|
|
g.save();
|
|
g.globalAlpha = 0.28;
|
|
g.filter = "blur(10px)";
|
|
g.fillStyle = "#000";
|
|
g.fillRect(20, 110, 560, 180);
|
|
g.restore();
|
|
|
|
if (s.weapon === 1) this.drawPistol(g);
|
|
else this.drawRifle(g);
|
|
|
|
// 枪口火光
|
|
if (s.muzzle > 0) this.drawFlash(g, s.weapon === 1 ? [16, 96] : [14, 96], Math.min(1, s.muzzle * 16));
|
|
|
|
g.restore();
|
|
}
|
|
|
|
/* ------------------------------- 步枪 AK ------------------------------- */
|
|
|
|
drawRifle(g) {
|
|
// 枪管
|
|
this.box(g, 26, 88, 236, 20, "#2b3037", "#3b4249");
|
|
// 消焰器
|
|
this.box(g, 2, 78, 42, 40, "#22262c", "#33383f");
|
|
// 导气管
|
|
this.box(g, 176, 58, 122, 14, "#343b43", "#454d56");
|
|
// 下护木
|
|
this.wood(g, 146, 70, 116, 66);
|
|
// 上护木
|
|
this.wood(g, 150, 52, 108, 26);
|
|
// 准星
|
|
this.box(g, 122, 28, 24, 46, "#2b3037", "#3b4249");
|
|
// 机匣
|
|
this.box(g, 248, 62, 194, 88, "#3d444d", "#4d5560");
|
|
// 上机匣盖
|
|
this.box(g, 250, 54, 186, 30, "#4a525c", "#5a626d");
|
|
// 照门
|
|
this.box(g, 388, 30, 44, 26, "#2b3037", "#3b4249");
|
|
// 枪托
|
|
this.poly(
|
|
g,
|
|
[
|
|
[430, 76],
|
|
[566, 100],
|
|
[604, 178],
|
|
[430, 148],
|
|
],
|
|
"#33383e",
|
|
"#41474f"
|
|
);
|
|
// 握把
|
|
this.poly(
|
|
g,
|
|
[
|
|
[418, 146],
|
|
[470, 146],
|
|
[488, 258],
|
|
[442, 266],
|
|
],
|
|
"#2f2a26",
|
|
"#3a342e"
|
|
);
|
|
// 弹匣(两段拼出弧度)
|
|
this.poly(
|
|
g,
|
|
[
|
|
[302, 146],
|
|
[366, 146],
|
|
[374, 214],
|
|
[310, 222],
|
|
],
|
|
"#4a3524",
|
|
"#5a412c"
|
|
);
|
|
this.poly(
|
|
g,
|
|
[
|
|
[310, 218],
|
|
[374, 210],
|
|
[392, 286],
|
|
[330, 296],
|
|
],
|
|
"#402d1e",
|
|
"#4e3826"
|
|
);
|
|
// 手
|
|
this.hand(g, 236, 128, -0.5);
|
|
this.hand(g, 452, 196, 0.25);
|
|
}
|
|
|
|
/* ------------------------------- 手枪 ------------------------------- */
|
|
|
|
drawPistol(g) {
|
|
this.box(g, 40, 62, 250, 36, "#8f99a3", "#aab4bf");
|
|
this.box(g, 18, 72, 26, 18, "#cfd8e0", "#e6edf3");
|
|
this.box(g, 34, 96, 210, 16, "#7c858f", "#9aa3ad");
|
|
this.poly(
|
|
g,
|
|
[
|
|
[230, 92],
|
|
[290, 92],
|
|
[308, 224],
|
|
[252, 236],
|
|
],
|
|
"#2f3339",
|
|
"#3c4147"
|
|
);
|
|
this.hand(g, 300, 150, 0.15);
|
|
}
|
|
|
|
/* ------------------------------- 绘制件 ------------------------------- */
|
|
|
|
box(g, x, y, w, h, fill, edge) {
|
|
g.fillStyle = fill;
|
|
g.fillRect(x, y, w, h);
|
|
if (edge) {
|
|
g.fillStyle = edge;
|
|
g.globalAlpha = 0.55;
|
|
g.fillRect(x, y, w, Math.max(2, h * 0.16));
|
|
g.globalAlpha = 1;
|
|
}
|
|
g.strokeStyle = "rgba(0,0,0,0.35)";
|
|
g.lineWidth = 2;
|
|
g.strokeRect(x + 1, y + 1, w - 2, h - 2);
|
|
}
|
|
|
|
wood(g, x, y, w, h) {
|
|
const grd = g.createLinearGradient(x, y, x, y + h);
|
|
grd.addColorStop(0, "#b57f45");
|
|
grd.addColorStop(0.55, "#9c6c39");
|
|
grd.addColorStop(1, "#7b5229");
|
|
g.fillStyle = grd;
|
|
g.fillRect(x, y, w, h);
|
|
g.strokeStyle = "rgba(60,32,10,0.35)";
|
|
g.lineWidth = 1.5;
|
|
for (let i = 1; i < 4; i++) {
|
|
g.beginPath();
|
|
g.moveTo(x + 4, y + (h / 4) * i);
|
|
g.bezierCurveTo(x + w * 0.35, y + (h / 4) * i - 3, x + w * 0.65, y + (h / 4) * i + 3, x + w - 4, y + (h / 4) * i);
|
|
g.stroke();
|
|
}
|
|
g.strokeStyle = "rgba(0,0,0,0.4)";
|
|
g.lineWidth = 2;
|
|
g.strokeRect(x + 1, y + 1, w - 2, h - 2);
|
|
}
|
|
|
|
poly(g, pts, fill, edge) {
|
|
const grd = g.createLinearGradient(pts[0][0], pts[0][1], pts[0][0], pts[pts.length - 1][1] + 60);
|
|
grd.addColorStop(0, edge || fill);
|
|
grd.addColorStop(1, fill);
|
|
g.fillStyle = grd;
|
|
g.beginPath();
|
|
pts.forEach(([x, y], i) => (i ? g.lineTo(x, y) : g.moveTo(x, y)));
|
|
g.closePath();
|
|
g.fill();
|
|
g.strokeStyle = "rgba(0,0,0,0.38)";
|
|
g.lineWidth = 2;
|
|
g.stroke();
|
|
}
|
|
|
|
hand(g, x, y, rot) {
|
|
g.save();
|
|
g.translate(x, y);
|
|
g.rotate(rot);
|
|
const grd = g.createLinearGradient(0, -30, 0, 40);
|
|
grd.addColorStop(0, SKIN);
|
|
grd.addColorStop(1, SKIN_DARK);
|
|
g.fillStyle = grd;
|
|
g.beginPath();
|
|
g.moveTo(-34, -26);
|
|
g.quadraticCurveTo(6, -38, 30, -10);
|
|
g.quadraticCurveTo(44, 14, 20, 32);
|
|
g.quadraticCurveTo(-14, 42, -34, 16);
|
|
g.quadraticCurveTo(-44, -6, -34, -26);
|
|
g.fill();
|
|
g.strokeStyle = "rgba(70,40,20,0.55)";
|
|
g.lineWidth = 2;
|
|
g.stroke();
|
|
// 指缝
|
|
g.strokeStyle = "rgba(90,55,30,0.45)";
|
|
g.lineWidth = 2;
|
|
for (let i = 0; i < 3; i++) {
|
|
g.beginPath();
|
|
g.moveTo(-26 + i * 15, -14);
|
|
g.quadraticCurveTo(-18 + i * 15, 4, -24 + i * 15, 20);
|
|
g.stroke();
|
|
}
|
|
g.restore();
|
|
}
|
|
|
|
drawFlash(g, at, a) {
|
|
g.save();
|
|
g.translate(at[0], at[1]);
|
|
g.globalCompositeOperation = "lighter";
|
|
const r = 90 * (0.6 + a * 0.7);
|
|
const grd = g.createRadialGradient(0, 0, 0, 0, 0, r);
|
|
grd.addColorStop(0, `rgba(255,246,214,${0.95 * a})`);
|
|
grd.addColorStop(0.35, `rgba(255,190,90,${0.7 * a})`);
|
|
grd.addColorStop(1, "rgba(255,120,20,0)");
|
|
g.fillStyle = grd;
|
|
g.beginPath();
|
|
g.arc(0, 0, r, 0, Math.PI * 2);
|
|
g.fill();
|
|
// 十字星芒
|
|
g.strokeStyle = `rgba(255,225,160,${0.85 * a})`;
|
|
g.lineWidth = 5;
|
|
g.beginPath();
|
|
g.moveTo(-r * 0.9, 0);
|
|
g.lineTo(r * 0.6, 0);
|
|
g.moveTo(0, -r * 0.55);
|
|
g.lineTo(0, r * 0.55);
|
|
g.stroke();
|
|
g.restore();
|
|
}
|
|
}
|