2c0e6fbf09
Water and rain erase fire under a tight brush hit radius; add looping fire/rain ambients and an Effects sound slider that also scales one-shot effect SFX. Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
/** Фоновый звук горения при наличии огня на сцене (`public/fire-ambient.mp3`). */
|
|
|
|
import { getEffectsSfxGain } from './effectsSfxGain';
|
|
|
|
const FIRE_AMBIENT_BASE_VOLUME = 0.72;
|
|
|
|
export function fireAmbientSoundUrl(): string {
|
|
return new URL('fire-ambient.mp3', window.location.href).href;
|
|
}
|
|
|
|
let ambientEl: HTMLAudioElement | null = null;
|
|
|
|
function ensureAmbientEl(): HTMLAudioElement {
|
|
if (ambientEl) return ambientEl;
|
|
const el = new Audio(fireAmbientSoundUrl());
|
|
el.loop = true;
|
|
el.preload = 'auto';
|
|
ambientEl = el;
|
|
return el;
|
|
}
|
|
|
|
function applyVolume(el: HTMLAudioElement): void {
|
|
el.volume = Math.max(0, Math.min(1, FIRE_AMBIENT_BASE_VOLUME * getEffectsSfxGain()));
|
|
}
|
|
|
|
/** Включить/выключить loop горения. */
|
|
export function setFireAmbientActive(active: boolean): void {
|
|
try {
|
|
const el = ensureAmbientEl();
|
|
applyVolume(el);
|
|
if (active) {
|
|
if (el.paused) void el.play().catch(() => undefined);
|
|
} else if (!el.paused) {
|
|
el.pause();
|
|
el.currentTime = 0;
|
|
}
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
|
|
/** Обновить громкость, если ambient уже играет. */
|
|
export function syncFireAmbientVolume(): void {
|
|
if (!ambientEl) return;
|
|
try {
|
|
applyVolume(ambientEl);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|