94 lines
2.8 KiB
JavaScript
Executable File
94 lines
2.8 KiB
JavaScript
Executable File
(function (root) {
|
|
"use strict";
|
|
|
|
const TRACKS = Object.freeze({
|
|
village: "village-theme", forest: "forest-theme", ruins: "forest-theme",
|
|
mountain: "mountain-theme", camp: "mountain-theme",
|
|
fortressExterior: "fortress-theme", fortressInterior: "fortress-theme",
|
|
bossArena: "boss-theme", chamber: "victory-theme"
|
|
});
|
|
|
|
function create(getState) {
|
|
let scene = null;
|
|
let ambience = null;
|
|
let voice = null;
|
|
let unlocked = false;
|
|
let ducked = false;
|
|
|
|
function attach(nextScene, region) {
|
|
scene = nextScene;
|
|
if (ambience) ambience.stop();
|
|
const key = TRACKS[region] || "village-theme";
|
|
ambience = scene.cache.audio.exists(key) ? scene.sound.add(key, { loop: true }) : null;
|
|
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.master || 0) * (state.settings.music || 0) * (ducked ? 0.38 : 1) : 0);
|
|
if (enabled && !ambience.isPlaying) ambience.play();
|
|
if (!enabled && ambience.isPlaying) ambience.pause();
|
|
}
|
|
}
|
|
|
|
function play(key, volume) {
|
|
const state = getState();
|
|
if (!scene || !unlocked || !state || !state.settings.soundEnabled || !scene.cache.audio.exists(key)) return;
|
|
scene.sound.play(key, { volume: state.settings.master * state.settings.effects * (volume || 1) });
|
|
}
|
|
|
|
function playVoice(key) {
|
|
const state = getState();
|
|
stopVoice();
|
|
if (!scene || !unlocked || !state || !state.settings.soundEnabled || !state.settings.narrationEnabled
|
|
|| !scene.cache.audio.exists(key)) return null;
|
|
voice = scene.sound.add(key, { volume: state.settings.master * state.settings.voice });
|
|
voice.once("complete", () => { voice = null; });
|
|
voice.play();
|
|
return voice;
|
|
}
|
|
|
|
function stopVoice() {
|
|
if (voice) {
|
|
voice.stop();
|
|
voice.destroy();
|
|
}
|
|
voice = null;
|
|
}
|
|
|
|
function suspend() {
|
|
if (ambience && ambience.isPlaying) ambience.pause();
|
|
}
|
|
|
|
function resume() {
|
|
apply();
|
|
}
|
|
|
|
function duck(value) {
|
|
ducked = Boolean(value);
|
|
apply();
|
|
}
|
|
|
|
function stop() {
|
|
stopVoice();
|
|
if (ambience) ambience.stop();
|
|
ambience = null;
|
|
scene = null;
|
|
}
|
|
|
|
return Object.freeze({ attach, unlock, apply, play, playVoice, stopVoice, suspend, resume, duck, stop, isUnlocked: () => unlocked });
|
|
}
|
|
|
|
root.PrincessLimaAudio = Object.freeze({ create, TRACKS });
|
|
}(typeof globalThis !== "undefined" ? globalThis : this));
|