fix: 重做第一人称枪模,不再像贴纸
- 枪模改成「侧面 + 顶面挤出」的 3/4 视角手绘:枪管/准星/导气/护木/机匣/弹匣/握把/手 都有体积,明暗和场景黄昏光对齐,边缘光改成冷白 - 投影从一块模糊矩形改成沿枪身剪影模糊,不再在场景里压出方形暗块 - 位置按屏幕坐标反推:枪口落在准星右下,枪托压到右下角,桌面/触屏各自一套缩放 - 木色/弹匣降饱和,避免暖色块在冷色场景里过于抢眼 - 修切枪竞态:服务端确认前保留本地切枪意图,快照不再把武器顶回旧值
No files matched your search
@@ -124,7 +124,7 @@ public/style.css 深色科技风样式
|
||||
public/level.json 地图数据(前后端同一份)
|
||||
public/js/client/main.js 连接 / 输入 / 预测回滚 / 射击 / 回合 UI
|
||||
public/js/client/renderer.js 手写 WebGL:贴图生成、地图合批、玩家模型、粒子、天空
|
||||
public/js/client/viewmodel.js 第一人称枪模(2D Canvas 矢量绘制 + 离屏缓存)
|
||||
public/js/client/viewmodel.js 第一人称枪模(2D Canvas 手绘 3/4 视角,侧面 + 顶面挤出 + 剪影投影,离屏缓存)
|
||||
public/js/client/touch.js 触屏操作层:虚拟摇杆、滑动瞄准、开火/跳/换弹/切枪
|
||||
public/js/client/movement.js 物理的 JS 镜像(客户端预测用)
|
||||
public/js/client/weapons.js 武器表的 JS 镜像(只做表现)
|
||||
|
||||
|
Before Width: | Height: | Size: 461 KiB After Width: | Height: | Size: 463 KiB |
|
Before Width: | Height: | Size: 340 KiB After Width: | Height: | Size: 343 KiB |
|
Before Width: | Height: | Size: 411 KiB After Width: | Height: | Size: 403 KiB |
|
Before Width: | Height: | Size: 604 KiB After Width: | Height: | Size: 631 KiB |
@@ -7,8 +7,8 @@ import { Sound } from "./sound.js";
|
||||
import { Net } from "./net.js";
|
||||
import { WEAPONS, weaponOf } from "./weapons.js";
|
||||
import { stepBody, makeBody, K, MOVE } from "./movement.js";
|
||||
import { ViewModel } from "./viewmodel.js";
|
||||
import { Touch } from "./touch.js";
|
||||
import { ViewModel } from "./viewmodel.js";
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const TICK = 1 / 60;
|
||||
@@ -17,8 +17,8 @@ const clamp = (v, a, b) => Math.min(b, Math.max(a, v));
|
||||
const canvas = $("gl");
|
||||
const hud = new Hud();
|
||||
const sound = new Sound();
|
||||
const viewmodel = new ViewModel(document.getElementById("vm"));
|
||||
const touch = new Touch();
|
||||
const viewmodel = new ViewModel(document.getElementById("vm"));
|
||||
const renderer = (() => {
|
||||
try {
|
||||
return new Renderer(canvas);
|
||||
@@ -337,13 +337,23 @@ function onSnap(s) {
|
||||
S.alive = me.alive;
|
||||
S.kills = me.kills;
|
||||
S.deaths = me.deaths;
|
||||
S.weapon = me.weapon;
|
||||
if (S.pendingWeapon != null) {
|
||||
// 服务端还没确认切枪:继续用本地意图
|
||||
S.weapon = S.pendingWeapon;
|
||||
if (me.weapon === S.pendingWeapon) S.pendingWeapon = null;
|
||||
} else {
|
||||
S.weapon = me.weapon;
|
||||
}
|
||||
S.mag = me.mag;
|
||||
S.reserve = me.reserve;
|
||||
if (!S.mags) S.mags = WEAPONS.map((w) => w.mag);
|
||||
if (!S.reserves) S.reserves = WEAPONS.map((w) => w.reserve);
|
||||
S.mags[me.weapon] = me.mag;
|
||||
S.reserves[me.weapon] = me.reserve;
|
||||
if (S.weapon !== me.weapon) {
|
||||
S.mag = S.mags[S.weapon];
|
||||
S.reserve = S.reserves[S.weapon];
|
||||
}
|
||||
S.reloadLeft = me.reloadLeft ? me.reloadLeft / 1000 : 0;
|
||||
S.reloadTotal = Math.max(0.2, weaponOf(me.weapon).reload);
|
||||
|
||||
@@ -476,6 +486,7 @@ function onRound(r) {
|
||||
S.mag = WEAPONS[0].mag;
|
||||
S.reserve = WEAPONS[0].reserve;
|
||||
S.weapon = 0;
|
||||
S.pendingWeapon = null;
|
||||
hud.banner(`第 ${r.roundNo} 回合`, "准备", `${r.score[0]} : ${r.score[1]} · 3 秒后开打`, 2200);
|
||||
sound.roundStart();
|
||||
} else if (r.phase === "live") {
|
||||
@@ -667,6 +678,8 @@ canvas.addEventListener("click", () => {
|
||||
function switchWeapon(id) {
|
||||
if (S.spectating || S.weapon === id) return;
|
||||
S.weapon = id;
|
||||
// 记住这次切换意图:服务端确认前,别被快照里的旧武器顶回去
|
||||
S.pendingWeapon = id;
|
||||
S.reloadLeft = 0;
|
||||
S.mag = S.mags ? S.mags[id] : weaponOf(id).mag;
|
||||
S.reserve = S.reserves ? S.reserves[id] : weaponOf(id).reserve;
|
||||
@@ -1019,7 +1032,6 @@ function drawGame() {
|
||||
players,
|
||||
selfSlot: S.you,
|
||||
spectating: S.spectating,
|
||||
weaponDef: WEAPONS[S.weapon],
|
||||
vm: S.vm,
|
||||
muzzle: S.muzzle,
|
||||
particles: S.particles,
|
||||
@@ -1032,7 +1044,7 @@ function drawGame() {
|
||||
const spread = weaponOf(S.weapon).spread * 6 + speed * 0.12 + air * 0.5 + Math.abs(S.recoil.pitch) * 6;
|
||||
hud.setCrosshair(spread);
|
||||
|
||||
if (S.spectating || S.scene !== "game") {
|
||||
if (S.spectating) {
|
||||
viewmodel.clear();
|
||||
} else {
|
||||
viewmodel.render({
|
||||
|
||||
@@ -501,6 +501,7 @@ export class Renderer {
|
||||
this.particleData = new Float32Array(4096 * 8);
|
||||
this.lineData = new Float32Array(4096 * 3);
|
||||
|
||||
|
||||
this.lightDir = normalize3([-0.42, 0.72, 0.55]);
|
||||
this.lightColor = [1.0, 0.88, 0.68];
|
||||
this.fillDir = normalize3([0.55, 0.22, -0.62]);
|
||||
@@ -905,6 +906,11 @@ export class Renderer {
|
||||
gl.disable(gl.BLEND);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
function mat3Of(m) {
|
||||
return new Float32Array([m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10]]);
|
||||
}
|
||||
|
||||
function normalize3(v) {
|
||||
|
||||
@@ -1,18 +1,31 @@
|
||||
// 第一人称枪模:用 2D Canvas 矢量绘制,比一堆方块更可控也更好看。
|
||||
// 只负责表现(后坐力回弹、走位摆动、枪口火光),命中判定全在服务端。
|
||||
// 第一人称枪模:2D Canvas 手绘,但用「侧面 + 顶面挤压」画出 3/4 视角的体积感,
|
||||
// 配色压到和场景同一个明度区间,避免像贴纸。
|
||||
// 整把枪烤进离屏画布,每帧只 drawImage 一次(手机上省很多)。
|
||||
|
||||
const SKIN = "#c99a72";
|
||||
const SKIN_DARK = "#a87b55";
|
||||
const TOP_DX = 18;
|
||||
const TOP_DY = -13;
|
||||
|
||||
const C = {
|
||||
metal: ["#343b44", "#4b545f"],
|
||||
metalDark: ["#2a2f36", "#3a424b"],
|
||||
metalMid: ["#3d444d", "#535c67"],
|
||||
wood: ["#473527", "#54402f"],
|
||||
woodTop: ["#52402f", "#5f4b38"],
|
||||
mag: ["#33271d", "#3e3025"],
|
||||
grip: ["#2e2b26", "#3d3a33"],
|
||||
glove: ["#3a3733", "#4a463f"],
|
||||
steel: ["#8a939d", "#a8b2bc"],
|
||||
};
|
||||
|
||||
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);
|
||||
const coarse = window.matchMedia && window.matchMedia("(pointer: coarse)").matches;
|
||||
this.dpr = Math.min(window.devicePixelRatio || 1, coarse ? 1.5 : 2);
|
||||
this._cache = null;
|
||||
this._cacheId = -1;
|
||||
this.resize();
|
||||
this.last = 0;
|
||||
}
|
||||
|
||||
resize() {
|
||||
@@ -29,7 +42,7 @@ export class ViewModel {
|
||||
this.g.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
}
|
||||
|
||||
/** s = {weapon: 0|1, bobX, bobY, kick, muzzle, swayX, alive} */
|
||||
/** s = {weapon, bobX, bobY, kick, swayX, muzzle, alive} */
|
||||
render(s) {
|
||||
this.resize();
|
||||
const g = this.g;
|
||||
@@ -38,236 +51,315 @@ export class ViewModel {
|
||||
|
||||
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 touch = document.body.classList.contains("touch");
|
||||
const scale = Math.min(W / 1280, H / 720) * (touch ? 0.66 : 0.78);
|
||||
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);
|
||||
// 锚点在枪口,枪身往右下角延伸
|
||||
// 锚点放在枪口(局部坐标 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
|
||||
);
|
||||
g.rotate(0.3 + kick * 0.022);
|
||||
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));
|
||||
const art = this.bake(s.weapon);
|
||||
g.drawImage(art.shadow, -54, -140, 900, 400);
|
||||
g.drawImage(art.gun, -54, -140, 900, 400);
|
||||
|
||||
if (s.muzzle > 0) {
|
||||
const a = Math.min(1, s.muzzle * 14);
|
||||
this.drawFlash(g, 14, 100, a);
|
||||
}
|
||||
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";
|
||||
const pad = 40;
|
||||
const w = 900;
|
||||
const h = 400;
|
||||
const dpr = this.dpr;
|
||||
|
||||
// 枪身投影
|
||||
g.save();
|
||||
g.globalAlpha = 0.3;
|
||||
g.filter = "blur(9px)";
|
||||
g.fillStyle = "#000";
|
||||
g.fillRect(26, 108, 550, 176);
|
||||
g.restore();
|
||||
// 1) 先把枪画到一张透明画布上
|
||||
const gun = document.createElement("canvas");
|
||||
gun.width = Math.round(w * dpr);
|
||||
gun.height = Math.round(h * dpr);
|
||||
const gg = gun.getContext("2d");
|
||||
gg.scale(dpr, dpr);
|
||||
gg.translate(pad, pad);
|
||||
gg.lineJoin = "round";
|
||||
if (weaponId === 1) this.drawPistol(gg);
|
||||
else this.drawRifle(gg);
|
||||
|
||||
if (weaponId === 1) this.drawPistol(g);
|
||||
else this.drawRifle(g);
|
||||
// 2) 用它的剪影做一层模糊投影(沿轮廓,不是一块矩形)
|
||||
const shadow = document.createElement("canvas");
|
||||
shadow.width = gun.width;
|
||||
shadow.height = gun.height;
|
||||
const sg = shadow.getContext("2d");
|
||||
sg.filter = "blur(9px)";
|
||||
sg.drawImage(gun, 8 * dpr, 16 * dpr);
|
||||
sg.filter = "none";
|
||||
sg.globalCompositeOperation = "source-in";
|
||||
sg.fillStyle = "rgba(0,0,0,0.55)";
|
||||
sg.fillRect(0, 0, shadow.width, shadow.height);
|
||||
|
||||
this._cache = c;
|
||||
this._cache = { gun, shadow };
|
||||
this._cacheId = weaponId;
|
||||
return c;
|
||||
return this._cache;
|
||||
}
|
||||
|
||||
/* ------------------------------- 步枪 AK ------------------------------- */
|
||||
/* ------------------------------- AK-47 ------------------------------- */
|
||||
|
||||
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, 58, 96, 300, 118, C.metalDark);
|
||||
this.box(g, 14, 86, 58, 126, C.metalDark);
|
||||
this.box(g, 240, 94, 282, 134, C.metalMid);
|
||||
this.box(g, 252, 58, 270, 96, C.metalDark);
|
||||
this.box(g, 276, 82, 456, 100, C.metalDark);
|
||||
|
||||
// 护木(木)
|
||||
this.poly(g, [[300, 84], [472, 84], [464, 152], [308, 152]], C.wood);
|
||||
this.poly(g, [[306, 68], [462, 68], [458, 88], [310, 88]], C.woodTop);
|
||||
|
||||
// 机匣
|
||||
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);
|
||||
this.box(g, 462, 80, 690, 160, C.metal);
|
||||
this.box(g, 468, 62, 684, 82, C.metalMid);
|
||||
this.box(g, 634, 44, 678, 64, C.metalDark); // 照门
|
||||
|
||||
// 枪托(收腰 + 托底)
|
||||
this.poly(g, [[690, 84], [806, 100], [812, 172], [700, 164], [690, 140]], C.metal);
|
||||
this.poly(g, [[800, 96], [818, 100], [822, 176], [806, 172]], C.metalDark);
|
||||
|
||||
// 弹匣(弧形)+ 握把
|
||||
this.poly(g, [[548, 150], [610, 150], [650, 264], [636, 294], [572, 288]], C.mag);
|
||||
this.poly(g, [[686, 152], [724, 152], [744, 262], [698, 272]], C.grip);
|
||||
|
||||
// 扳机护圈
|
||||
g.strokeStyle = "rgba(28,32,38,0.95)";
|
||||
g.lineWidth = 9;
|
||||
g.beginPath();
|
||||
g.moveTo(650, 158);
|
||||
g.quadraticCurveTo(672, 200, 708, 172);
|
||||
g.stroke();
|
||||
|
||||
// 手(左手托护木、右手握把)
|
||||
this.glove(g, 348, 132, 434, 200, 0.05, 1, true);
|
||||
this.glove(g, 692, 174, 754, 252, 0.3, -1, false);
|
||||
|
||||
// 细节:木纹、铆钉、高光
|
||||
g.save();
|
||||
g.globalAlpha = 0.22;
|
||||
g.strokeStyle = "#3a2410";
|
||||
g.lineWidth = 2;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
g.beginPath();
|
||||
g.moveTo(312, 104 + i * 16);
|
||||
g.quadraticCurveTo(380, 100 + i * 16, 458, 108 + i * 16);
|
||||
g.stroke();
|
||||
}
|
||||
g.restore();
|
||||
|
||||
g.fillStyle = "rgba(24,28,34,0.9)";
|
||||
for (const [x, y] of [[486, 120], [486, 146], [666, 120], [666, 146]]) {
|
||||
g.beginPath();
|
||||
g.arc(x, y, 2.6, 0, Math.PI * 2);
|
||||
g.fill();
|
||||
}
|
||||
|
||||
// 弹匣压筋 + 底板
|
||||
g.save();
|
||||
g.globalAlpha = 0.4;
|
||||
g.strokeStyle = "#33230f";
|
||||
g.lineWidth = 4;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
g.beginPath();
|
||||
g.moveTo(566, 182 + i * 34);
|
||||
g.quadraticCurveTo(602, 186 + i * 34, 636, 190 + i * 34);
|
||||
g.stroke();
|
||||
}
|
||||
g.globalAlpha = 0.9;
|
||||
g.fillStyle = "#3a2a18";
|
||||
g.beginPath();
|
||||
g.moveTo(566, 282);
|
||||
g.lineTo(636, 288);
|
||||
g.lineTo(640, 300);
|
||||
g.lineTo(570, 296);
|
||||
g.closePath();
|
||||
g.fill();
|
||||
g.restore();
|
||||
|
||||
this.rim(g, [[300, 84], [472, 84]], 0.4);
|
||||
this.rim(g, [[462, 80], [690, 80]], 0.5);
|
||||
this.rim(g, [[468, 62], [684, 62]], 0.55);
|
||||
this.rim(g, [[58, 96], [240, 96]], 0.45);
|
||||
this.rim(g, [[306, 68], [462, 68]], 0.4);
|
||||
}
|
||||
|
||||
/* ------------------------------- 手枪 ------------------------------- */
|
||||
/** 手:圆角 + 拇指凸起 + 指缝,读起来像只手套 */
|
||||
glove(g, x0, y0, x1, y1, tilt, dir, fingers) {
|
||||
g.save();
|
||||
g.translate((x0 + x1) / 2, (y0 + y1) / 2);
|
||||
g.rotate(tilt);
|
||||
const w = x1 - x0;
|
||||
const h = y1 - y0;
|
||||
const r = Math.min(w, h) * 0.3;
|
||||
const grd = g.createLinearGradient(0, -h / 2, 0, h / 2);
|
||||
grd.addColorStop(0, "#3d3934");
|
||||
grd.addColorStop(1, "#282622");
|
||||
g.fillStyle = grd;
|
||||
g.beginPath();
|
||||
g.moveTo(-w / 2 + r, -h / 2);
|
||||
g.quadraticCurveTo(w / 2, -h / 2 - 4, w / 2, -h / 2 + r);
|
||||
g.quadraticCurveTo(w / 2 + 3, h / 2 - r, w / 2 - r, h / 2);
|
||||
g.quadraticCurveTo(-w / 2, h / 2 + 4, -w / 2, h / 2 - r);
|
||||
g.quadraticCurveTo(-w / 2 - 3, -h / 2 + r, -w / 2 + r, -h / 2);
|
||||
g.closePath();
|
||||
g.fill();
|
||||
// 拇指
|
||||
g.beginPath();
|
||||
g.ellipse(-w * 0.18 * dir, -h * 0.42, w * 0.2, h * 0.16, -0.3 * dir, 0, Math.PI * 2);
|
||||
g.fillStyle = "#46423b";
|
||||
g.fill();
|
||||
// 指缝
|
||||
g.strokeStyle = "rgba(18,20,22,0.55)";
|
||||
g.lineWidth = 2;
|
||||
for (let i = 0; i < 2; i++) {
|
||||
g.beginPath();
|
||||
g.moveTo(-w * 0.1 + i * w * 0.26, -h * 0.05);
|
||||
g.quadraticCurveTo(w * 0.02 + i * w * 0.26, h * 0.16, -w * 0.06 + i * w * 0.26, h * 0.34);
|
||||
g.stroke();
|
||||
}
|
||||
g.strokeStyle = "rgba(12,14,17,0.5)";
|
||||
g.lineWidth = 1.5;
|
||||
g.stroke();
|
||||
|
||||
// 搭在护木上的手指
|
||||
if (fingers) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const fx = -w * 0.32 + i * w * 0.3;
|
||||
g.beginPath();
|
||||
g.roundRect(fx, -h * 0.62, w * 0.26, h * 0.3, 5);
|
||||
g.fillStyle = i === 1 ? "#403c36" : "#45413a";
|
||||
g.fill();
|
||||
g.strokeStyle = "rgba(12,14,17,0.4)";
|
||||
g.lineWidth = 1.2;
|
||||
g.stroke();
|
||||
}
|
||||
}
|
||||
g.restore();
|
||||
}
|
||||
|
||||
/* ------------------------------- 沙鹰 ------------------------------- */
|
||||
|
||||
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);
|
||||
// 套筒
|
||||
this.box(g, 120, 84, 430, 132, C.metal);
|
||||
// 套筒前端
|
||||
this.box(g, 90, 90, 122, 128, C.metalMid);
|
||||
// 枪口
|
||||
this.box(g, 66, 96, 92, 120, C.steel);
|
||||
// 机匣 / 扳机护圈
|
||||
this.box(g, 140, 132, 410, 160, C.metalDark);
|
||||
g.strokeStyle = "rgba(28,32,38,0.95)";
|
||||
g.lineWidth = 9;
|
||||
g.beginPath();
|
||||
g.moveTo(210, 158);
|
||||
g.quadraticCurveTo(238, 206, 300, 176);
|
||||
g.stroke();
|
||||
// 握把
|
||||
this.poly(g, [[330, 150], [420, 150], [446, 300], [352, 300]], C.grip);
|
||||
// 手
|
||||
this.glove(g, 350, 182, 448, 300, 0.34, -1);
|
||||
// 准星 / 照门
|
||||
this.box(g, 108, 70, 128, 90, C.metalDark);
|
||||
this.box(g, 392, 66, 424, 86, C.metalDark);
|
||||
|
||||
this.rim(g, [[120, 84], [430, 84]], 0.5);
|
||||
this.rim(g, [[330, 150], [420, 150]], 0.35);
|
||||
}
|
||||
|
||||
/* ------------------------------- 绘制件 ------------------------------- */
|
||||
|
||||
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);
|
||||
}
|
||||
/** 带顶面的长方体:先画顶面(亮),再画侧面(暗),最后描边 */
|
||||
box(g, x0, y0, x1, y1, colors) {
|
||||
const [side, top] = colors;
|
||||
// 顶面
|
||||
g.beginPath();
|
||||
g.moveTo(x0, y0);
|
||||
g.lineTo(x1, y0);
|
||||
g.lineTo(x1 + TOP_DX, y0 + TOP_DY);
|
||||
g.lineTo(x0 + TOP_DX, y0 + TOP_DY);
|
||||
g.closePath();
|
||||
g.fillStyle = top;
|
||||
g.fill();
|
||||
|
||||
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");
|
||||
// 侧面
|
||||
const grd = g.createLinearGradient(0, y0, 0, y1);
|
||||
grd.addColorStop(0, shade(side, 14));
|
||||
grd.addColorStop(0.55, side);
|
||||
grd.addColorStop(1, shade(side, -18));
|
||||
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);
|
||||
g.fillRect(x0, y0, x1 - x0, y1 - y0);
|
||||
|
||||
g.strokeStyle = "rgba(10,14,19,0.34)";
|
||||
g.lineWidth = 1.1;
|
||||
g.strokeRect(x0 + 0.5, y0 + 0.5, x1 - x0 - 1, y1 - y0 - 1);
|
||||
}
|
||||
|
||||
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);
|
||||
/** 任意侧面多边形 + 顶面(沿顶边挤出一条,模拟 3/4 视角) */
|
||||
poly(g, pts, colors, topEdge) {
|
||||
const [side, top] = colors;
|
||||
if (topEdge !== null) {
|
||||
const e = topEdge || [pts[0], pts[1]];
|
||||
g.beginPath();
|
||||
g.moveTo(e[0][0], e[0][1]);
|
||||
g.lineTo(e[1][0], e[1][1]);
|
||||
g.lineTo(e[1][0] + TOP_DX, e[1][1] + TOP_DY);
|
||||
g.lineTo(e[0][0] + TOP_DX, e[0][1] + TOP_DY);
|
||||
g.closePath();
|
||||
g.fillStyle = top;
|
||||
g.fill();
|
||||
}
|
||||
|
||||
// 侧面
|
||||
const xs = pts.map((p) => p[0]);
|
||||
const ys = pts.map((p) => p[1]);
|
||||
const grd = g.createLinearGradient(0, Math.min(...ys), 0, Math.max(...ys));
|
||||
grd.addColorStop(0, shade(side, 12));
|
||||
grd.addColorStop(0.6, side);
|
||||
grd.addColorStop(1, shade(side, -20));
|
||||
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.strokeStyle = "rgba(10,14,19,0.38)";
|
||||
g.lineWidth = 1.2;
|
||||
g.stroke();
|
||||
}
|
||||
|
||||
hand(g, x, y, rot) {
|
||||
/** 顶边高光:让边缘吃到场景里的暖光 */
|
||||
rim(g, pts, alpha) {
|
||||
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.strokeStyle = `rgba(232,208,178,${alpha})`;
|
||||
g.lineWidth = 1.8;
|
||||
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;
|
||||
pts.forEach(([x, y], i) => (i ? g.lineTo(x, y) : g.moveTo(x, y)));
|
||||
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) {
|
||||
drawFlash(g, x, y, a) {
|
||||
g.save();
|
||||
g.translate(at[0], at[1]);
|
||||
g.translate(x, y);
|
||||
g.globalCompositeOperation = "lighter";
|
||||
const r = 90 * (0.6 + a * 0.7);
|
||||
const r = 95 * (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})`);
|
||||
@@ -276,15 +368,24 @@ export class ViewModel {
|
||||
g.beginPath();
|
||||
g.arc(0, 0, r, 0, Math.PI * 2);
|
||||
g.fill();
|
||||
// 十字星芒
|
||||
g.strokeStyle = `rgba(255,225,160,${0.85 * a})`;
|
||||
g.strokeStyle = `rgba(255,225,160,${0.8 * 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.moveTo(-r * 0.85, 0);
|
||||
g.lineTo(r * 0.55, 0);
|
||||
g.moveTo(0, -r * 0.5);
|
||||
g.lineTo(0, r * 0.5);
|
||||
g.stroke();
|
||||
g.restore();
|
||||
}
|
||||
}
|
||||
|
||||
/** #rrggbb 调亮/调暗 */
|
||||
function shade(hex, amount) {
|
||||
const n = parseInt(hex.slice(1), 16);
|
||||
const clamp = (v) => Math.max(0, Math.min(255, v));
|
||||
const r = clamp(((n >> 16) & 255) + amount);
|
||||
const gg = clamp(((n >> 8) & 255) + amount);
|
||||
const b = clamp((n & 255) + amount);
|
||||
return `rgb(${r},${gg},${b})`;
|
||||
}
|
||||