- 服务端 60Hz 固定步长模拟:重力/摩擦/轴分离碰撞/上台阶,客户端预测回滚 - 命中裁决:射线打墙打人(身体 + 头部盒),射速/弹匣/换弹/伤害/比分全在服务端 - 回合制:3 秒准备、先到 8 分、100 秒平局;观战自由视角 - 机器人陪练:视线检测 + 反应延迟 + 点射偏差,能单人开练 - 前端零依赖:手写 WebGL(程序化贴图、地图合批、粒子、曳光弹、假投影、黄昏天空) - 2D Canvas 枪模 + WebAudio 合成音效,无任何图片/音频/CDN 资源 - 地图 level.json 前后端同源,tools/gen_level.py 四分镜像生成,绝对对称 - 测试:14 组规则单测 + 6 组联机 e2e
206 lines
5.8 KiB
JavaScript
206 lines
5.8 KiB
JavaScript
// 全部音效都是 WebAudio 现场合成,不带任何音频文件。
|
|
|
|
export class Sound {
|
|
constructor() {
|
|
this.ctx = null;
|
|
this.master = null;
|
|
this.noise = null;
|
|
this.volume = 0.6;
|
|
this.enabled = true;
|
|
}
|
|
|
|
ensure() {
|
|
if (!this.enabled) return false;
|
|
if (!this.ctx) {
|
|
const AC = window.AudioContext || window.webkitAudioContext;
|
|
if (!AC) {
|
|
this.enabled = false;
|
|
return false;
|
|
}
|
|
this.ctx = new AC();
|
|
this.master = this.ctx.createGain();
|
|
this.master.gain.value = this.volume;
|
|
this.master.connect(this.ctx.destination);
|
|
// 1 秒白噪声,枪声/脚步都从它切
|
|
const len = Math.floor(this.ctx.sampleRate * 1.0);
|
|
const buf = this.ctx.createBuffer(1, len, this.ctx.sampleRate);
|
|
const d = buf.getChannelData(0);
|
|
for (let i = 0; i < len; i++) d[i] = Math.random() * 2 - 1;
|
|
this.noise = buf;
|
|
}
|
|
if (this.ctx.state === "suspended") {
|
|
const now = performance.now();
|
|
if (!this._resumeAt || now - this._resumeAt > 2000) {
|
|
this._resumeAt = now;
|
|
this.ctx.resume().catch(() => {});
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
setVolume(v) {
|
|
this.volume = v;
|
|
if (this.master) this.master.gain.value = v;
|
|
}
|
|
|
|
chain(pan) {
|
|
const ctx = this.ctx;
|
|
const out = ctx.createGain();
|
|
if (ctx.createStereoPanner) {
|
|
const p = ctx.createStereoPanner();
|
|
p.pan.value = Math.max(-1, Math.min(1, pan || 0));
|
|
out.connect(p);
|
|
p.connect(this.master);
|
|
} else {
|
|
out.connect(this.master);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** 枪声:噪声爆 + 低频冲击。pan 用于判断敌人从哪边开枪 */
|
|
shot(weaponId, pan = 0, dist = 0, own = true) {
|
|
if (!this.ensure()) return;
|
|
const ctx = this.ctx;
|
|
const t = ctx.currentTime;
|
|
const far = Math.min(1, dist / 45);
|
|
const gain = (own ? 0.85 : 0.9) * (1 - far * 0.75);
|
|
const dur = weaponId === 1 ? 0.16 : 0.26;
|
|
const out = this.chain(pan);
|
|
|
|
const src = ctx.createBufferSource();
|
|
src.buffer = this.noise;
|
|
src.playbackRate.value = 0.9 + Math.random() * 0.2;
|
|
const lp = ctx.createBiquadFilter();
|
|
lp.type = "lowpass";
|
|
lp.frequency.setValueAtTime(4200 - far * 2600, t);
|
|
lp.frequency.exponentialRampToValueAtTime(380, t + dur * 0.8);
|
|
const g = ctx.createGain();
|
|
g.gain.setValueAtTime(gain, t);
|
|
g.gain.exponentialRampToValueAtTime(0.0008, t + dur);
|
|
src.connect(lp);
|
|
lp.connect(g);
|
|
g.connect(out);
|
|
src.start(t);
|
|
src.stop(t + dur + 0.02);
|
|
|
|
const osc = ctx.createOscillator();
|
|
osc.type = "sine";
|
|
osc.frequency.setValueAtTime(weaponId === 1 ? 150 : 120, t);
|
|
osc.frequency.exponentialRampToValueAtTime(42, t + 0.12);
|
|
const og = ctx.createGain();
|
|
og.gain.setValueAtTime(gain * 0.7, t);
|
|
og.gain.exponentialRampToValueAtTime(0.001, t + 0.13);
|
|
osc.connect(og);
|
|
og.connect(out);
|
|
osc.start(t);
|
|
osc.stop(t + 0.14);
|
|
}
|
|
|
|
blip(freq, dur = 0.06, type = "square", gain = 0.14) {
|
|
if (!this.ensure()) return;
|
|
const ctx = this.ctx;
|
|
const t = ctx.currentTime;
|
|
const o = ctx.createOscillator();
|
|
o.type = type;
|
|
o.frequency.setValueAtTime(freq, t);
|
|
const g = ctx.createGain();
|
|
g.gain.setValueAtTime(gain, t);
|
|
g.gain.exponentialRampToValueAtTime(0.0006, t + dur);
|
|
o.connect(g);
|
|
g.connect(this.master);
|
|
o.start(t);
|
|
o.stop(t + dur + 0.01);
|
|
}
|
|
|
|
hit(head) {
|
|
this.blip(head ? 1600 : 1150, head ? 0.1 : 0.05, "square", head ? 0.16 : 0.1);
|
|
if (head) setTimeout(() => this.blip(2100, 0.09, "sine", 0.14), 45);
|
|
}
|
|
|
|
kill() {
|
|
this.blip(760, 0.09, "sawtooth", 0.14);
|
|
setTimeout(() => this.blip(520, 0.14, "sawtooth", 0.12), 70);
|
|
}
|
|
|
|
hurt() {
|
|
if (!this.ensure()) return;
|
|
const ctx = this.ctx;
|
|
const t = ctx.currentTime;
|
|
const src = ctx.createBufferSource();
|
|
src.buffer = this.noise;
|
|
src.playbackRate.value = 0.6;
|
|
const lp = ctx.createBiquadFilter();
|
|
lp.type = "lowpass";
|
|
lp.frequency.value = 900;
|
|
const g = ctx.createGain();
|
|
g.gain.setValueAtTime(0.35, t);
|
|
g.gain.exponentialRampToValueAtTime(0.001, t + 0.18);
|
|
src.connect(lp);
|
|
lp.connect(g);
|
|
g.connect(this.master);
|
|
src.start(t);
|
|
src.stop(t + 0.2);
|
|
}
|
|
|
|
reload(stage) {
|
|
this.blip(stage === 0 ? 520 : 380, 0.045, "square", 0.09);
|
|
}
|
|
|
|
empty() {
|
|
this.blip(260, 0.04, "square", 0.1);
|
|
}
|
|
|
|
step() {
|
|
if (!this.ensure()) return;
|
|
const ctx = this.ctx;
|
|
const t = ctx.currentTime;
|
|
const src = ctx.createBufferSource();
|
|
src.buffer = this.noise;
|
|
src.playbackRate.value = 0.35 + Math.random() * 0.2;
|
|
const bp = ctx.createBiquadFilter();
|
|
bp.type = "bandpass";
|
|
bp.frequency.value = 260 + Math.random() * 120;
|
|
bp.Q.value = 0.9;
|
|
const g = ctx.createGain();
|
|
g.gain.setValueAtTime(0.16, t);
|
|
g.gain.exponentialRampToValueAtTime(0.0008, t + 0.1);
|
|
src.connect(bp);
|
|
bp.connect(g);
|
|
g.connect(this.master);
|
|
src.start(t);
|
|
src.stop(t + 0.12);
|
|
}
|
|
|
|
roundStart() {
|
|
this.blip(880, 0.09, "sine", 0.13);
|
|
setTimeout(() => this.blip(1320, 0.12, "sine", 0.13), 130);
|
|
}
|
|
|
|
roundEnd(win) {
|
|
if (win) {
|
|
this.blip(660, 0.1, "sine", 0.14);
|
|
setTimeout(() => this.blip(990, 0.16, "sine", 0.14), 110);
|
|
} else {
|
|
this.blip(300, 0.16, "sine", 0.12);
|
|
setTimeout(() => this.blip(200, 0.22, "sine", 0.12), 120);
|
|
}
|
|
}
|
|
|
|
death() {
|
|
if (!this.ensure()) return;
|
|
const ctx = this.ctx;
|
|
const t = ctx.currentTime;
|
|
const o = ctx.createOscillator();
|
|
o.type = "sine";
|
|
o.frequency.setValueAtTime(180, t);
|
|
o.frequency.exponentialRampToValueAtTime(50, t + 0.6);
|
|
const g = ctx.createGain();
|
|
g.gain.setValueAtTime(0.28, t);
|
|
g.gain.exponentialRampToValueAtTime(0.001, t + 0.65);
|
|
o.connect(g);
|
|
g.connect(this.master);
|
|
o.start(t);
|
|
o.stop(t + 0.7);
|
|
}
|
|
}
|