Files
org_web/assets/scripts/pages/archive-world-audio.js
gitea-actions c06af38d80
All checks were successful
Build Org Website / build (push) Successful in 39s
Harden archive-world setup and refresh site links
2026-07-30 09:08:12 +01:00

70 lines
2.2 KiB
JavaScript
Executable File

(function (root) {
"use strict";
const KEYS = Object.freeze({
ambient: ["ambient-v2", "ambience"],
vault: ["vault-ambient", "ambience"],
step: ["step-v2", "effects"],
attack: ["attack", "effects"],
damage: ["damage", "effects"],
defeat: ["enemy-defeat", "effects"],
item: ["item", "effects"],
quest: ["quest", "effects"],
dialogue: ["dialogue", "effects"],
puzzle: ["puzzle", "effects"],
boss: ["boss-attack", "effects"],
victory: ["victory", "effects"],
portal: ["portal-v2", "effects"]
});
function create(getState) {
let scene = null;
let ambience = null;
let unlocked = false;
function attach(nextScene, area) {
scene = nextScene;
if (ambience && ambience.isPlaying) ambience.stop();
ambience = scene.sound.add(area === "town" ? "ambient-v2" : "vault-ambient", { loop: true });
apply();
}
function unlock() {
unlocked = true;
if (scene && scene.sound.locked && scene.sound.unlock) scene.sound.unlock();
apply();
}
function apply() {
if (!scene) return;
const state = getState();
const enabled = Boolean(unlocked && state && state.settings.soundEnabled);
scene.sound.mute = !enabled;
if (ambience) {
ambience.setVolume(state ? state.settings.ambience : 0.35);
if (enabled && !ambience.isPlaying) ambience.play();
if (!enabled && ambience.isPlaying) ambience.pause();
}
}
function play(name, config) {
if (!scene || !unlocked) return;
const state = getState();
const spec = KEYS[name];
if (!state || !state.settings.soundEnabled || !spec || !scene.cache.audio.exists(spec[0])) return;
const volume = state.settings[spec[1]] * ((config && config.volume) || 1);
scene.sound.play(spec[0], { volume, rate: config && config.rate || 1 });
}
function stop() {
if (ambience && ambience.isPlaying) ambience.stop();
ambience = null;
scene = null;
}
return Object.freeze({ attach, unlock, apply, play, stop, isUnlocked: () => unlocked });
}
root.ArchiveWorldAudio = Object.freeze({ create, KEYS });
}(typeof globalThis !== "undefined" ? globalThis : this));