- 自动识别触屏设备(coarse pointer + hover:none / 移动端 UA),桌面保持键鼠 - 左虚拟摇杆移动、右半屏滑动瞄准、开火/跳/换弹/切枪/菜单按键,真多点触控 - HUD 重排:血量与弹药上移,底部整条让给拇指;竖屏提示转屏 - 触屏搭车全屏 + 横屏锁定(Android),点开火保证至少一发,切后台自动松键 - 移动端 DPR 上限 1.3,枪模改离屏缓存,每帧只贴一次 - 观战自由视角同样支持摇杆;菜单打开时自动松开输入
291 lines
7.7 KiB
JavaScript
291 lines
7.7 KiB
JavaScript
// 第一人称枪模:用 2D Canvas 矢量绘制,比一堆方块更可控也更好看。
|
|
// 只负责表现(后坐力回弹、走位摆动、枪口火光),命中判定全在服务端。
|
|
|
|
const SKIN = "#c99a72";
|
|
const SKIN_DARK = "#a87b55";
|
|
|
|
export class ViewModel {
|
|
constructor(canvas) {
|
|
this.canvas = canvas;
|
|
this.g = canvas.getContext("2d");
|
|
const coarse =
|
|
window.matchMedia && window.matchMedia("(pointer: coarse)").matches;
|
|
this.dpr = Math.min(window.devicePixelRatio || 1, coarse ? 1.3 : 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 isTouch = document.body.classList.contains("touch");
|
|
const scale = (H / 720) * (isTouch ? 0.86 : 1.04);
|
|
const ax = W * (isTouch ? 0.55 : 0.53);
|
|
const ay = H * (isTouch ? 0.575 : 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.drawImage(this.bake(s.weapon), -70, -70, 720, 420);
|
|
|
|
// 枪口火光
|
|
if (s.muzzle > 0) this.drawFlash(g, s.weapon === 1 ? [16, 96] : [14, 96], Math.min(1, s.muzzle * 16));
|
|
|
|
g.restore();
|
|
}
|
|
|
|
/** 把整把枪画进离屏画布,缓存住(换枪才重画) */
|
|
bake(weaponId) {
|
|
if (this._cache && this._cacheId === weaponId) return this._cache;
|
|
const pad = 70;
|
|
const w = 720;
|
|
const h = 420;
|
|
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
const c = document.createElement("canvas");
|
|
c.width = Math.round(w * dpr);
|
|
c.height = Math.round(h * dpr);
|
|
const g = c.getContext("2d");
|
|
g.scale(dpr, dpr);
|
|
g.translate(pad, pad);
|
|
g.lineJoin = "round";
|
|
|
|
// 枪身投影
|
|
g.save();
|
|
g.globalAlpha = 0.3;
|
|
g.filter = "blur(9px)";
|
|
g.fillStyle = "#000";
|
|
g.fillRect(26, 108, 550, 176);
|
|
g.restore();
|
|
|
|
if (weaponId === 1) this.drawPistol(g);
|
|
else this.drawRifle(g);
|
|
|
|
this._cache = c;
|
|
this._cacheId = weaponId;
|
|
return c;
|
|
}
|
|
|
|
/* ------------------------------- 步枪 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();
|
|
}
|
|
}
|