feat(sound): Refactor sound manager initialization to reuse instance across preload and create phases

This commit is contained in:
Z. Cliffe Schreuders
2025-11-01 11:07:54 +00:00
parent cb8a9399a9
commit 6aa6fd4beb
5 changed files with 21 additions and 9 deletions

View File

@@ -398,8 +398,9 @@ export function preload() {
this.load.audio('message_received', 'assets/sounds/message_received.mp3');
// Initialize sound manager and preload all sounds
const soundManager = new SoundManager(this);
soundManager.preloadSounds();
// Store as window property so we can access it later in create()
window.soundManagerPreload = new SoundManager(this);
window.soundManagerPreload.preloadSounds();
// Get scenario from URL parameter or use default
const urlParams = new URLSearchParams(window.location.search);
@@ -610,10 +611,19 @@ export function create() {
// Process initial inventory items
processInitialInventoryItems();
// Initialize sound manager after all assets are loaded
const soundManager = new SoundManager(this);
soundManager.initializeSounds();
window.soundManager = soundManager;
// Initialize sound manager - reuse the instance created in preload()
if (window.soundManagerPreload) {
// Reuse the sound manager that was created in preload
window.soundManagerPreload.initializeSounds();
window.soundManager = window.soundManagerPreload;
delete window.soundManagerPreload; // Clean up temporary reference
} else {
// Fallback in case preload didn't run properly
const soundManager = new SoundManager(this);
soundManager.preloadSounds();
soundManager.initializeSounds();
window.soundManager = soundManager;
}
console.log('🔊 Sound Manager initialized');
// Show introduction

View File

@@ -147,6 +147,9 @@ class SoundManager {
play(soundName, options = {}) {
if (!this.enabled) return;
// console log
console.log(`🔊 Playing sound: ${soundName}`);
const sound = this.sounds[soundName];
if (!sound) {
console.warn(`Sound not found: ${soundName}`);

View File

@@ -59,7 +59,7 @@ I can unlock doors, give you items, and provide hints.
{ trust_level >= 2:
Here's a lockpick set. Use it wisely! 🔓
~ has_given_lockpick = true
# give_item:lockpick_set
# give_item:lockpick
-> main_menu
- else:
I need to trust you more before I give you something like that.

View File

@@ -1 +0,0 @@
{"inkVersion":21,"root":[[["done",{"#n":"g-0"}],null],"done",{"start":[["^Hey there! I'm here to help you out if you need it. 👋","\n","^What can I do for you?","\n","ev","str","^Who are you?","/str","/ev",{"*":".^.c-0","flg":4},"ev","str","^Can you help me get into the CEO's office?","/str","/ev",{"*":".^.c-1","flg":4},"ev","str","^Do you have any items for me?","/str","/ev",{"*":".^.c-2","flg":4},"ev","str","^Thanks, I'm good for now.","/str","/ev",{"*":".^.c-3","flg":4},{"c-0":["^ ",{"->":"who_are_you"},"\n",null],"c-1":["^ ",{"->":"help_ceo_office"},"\n",null],"c-2":["^ ",{"->":"give_items"},"\n",null],"c-3":["^ ",{"->":"goodbye"},"\n",null]}],null],"who_are_you":["^I'm a friendly NPC who can help you progress through the mission.","\n","^I can unlock doors, give you items, and provide hints.","\n","ev",{"VAR?":"trust_level"},1,"+","/ev",{"VAR=":"trust_level","re":true},{"->":"start"},null],"help_ceo_office":["ev",{"VAR?":"has_unlocked_ceo"},"/ev",[{"->":".^.b","c":true},{"b":["\n","^I already unlocked the CEO's office for you! Just head on in.","\n",{"->":"start"},{"->":".^.^.^.5"},null]}],[{"->":".^.b"},{"b":["\n","^The CEO's office? That's a tough one...","\n","ev",{"VAR?":"trust_level"},1,">=","/ev",[{"->":".^.b","c":true},{"b":["\n","^Alright, I trust you. Let me unlock that door for you.","\n","ev",true,"/ev",{"VAR=":"has_unlocked_ceo","re":true},"#","^unlock_door:ceo","/#","^There you go! The door to the CEO's office is now unlocked.","\n","ev",{"VAR?":"trust_level"},2,"+","/ev",{"VAR=":"trust_level","re":true},{"->":"start"},{"->":".^.^.^.10"},null]}],[{"->":".^.b"},{"b":["\n","^I don't know you well enough yet. Ask me something else first.","\n",{"->":"start"},{"->":".^.^.^.10"},null]}],"nop","\n",{"->":".^.^.^.5"},null]}],"nop","\n",null],"give_items":["ev",{"VAR?":"has_given_keycard"},"/ev",[{"->":".^.b","c":true},{"b":["\n","^I already gave you a keycard! Check your inventory.","\n",{"->":"start"},{"->":".^.^.^.5"},null]}],[{"->":".^.b"},{"b":["\n","^Let me see what I have...","\n","ev",{"VAR?":"trust_level"},2,">=","/ev",[{"->":".^.b","c":true},{"b":["\n","^Ah, here's a keycard that might be useful!","\n","ev",true,"/ev",{"VAR=":"has_given_keycard","re":true},"#","^give_item:keycard","/#","^Added a keycard to your inventory.","\n",{"->":"start"},{"->":".^.^.^.10"},null]}],[{"->":".^.b"},{"b":["\n","^I can't just hand out items to strangers. Get to know me better first.","\n",{"->":"start"},{"->":".^.^.^.10"},null]}],"nop","\n",{"->":".^.^.^.5"},null]}],"nop","\n",null],"goodbye":["^No problem! Let me know if you need anything.","\n","end",null],"global decl":["ev",0,{"VAR=":"trust_level"},false,{"VAR=":"has_unlocked_ceo"},false,{"VAR=":"has_given_keycard"},"/ev","end",null]}],"listDefs":{}}

File diff suppressed because one or more lines are too long