Files
novafarma/src/systems/AudioLoader.js

154 lines
4.6 KiB
JavaScript

/**
* AUDIO LOADER - Phaser Integration
* Preloads all game audio files with proper paths
*/
export class AudioLoader {
constructor(scene) {
this.scene = scene;
}
/**
* Preload all audio assets
* Call this in scene's preload() method
*/
preloadAll() {
console.log('🎵 AudioLoader: Starting audio preload...');
// Voice files
this.preloadVoices();
// Music
this.preloadMusic();
// SFX
this.preloadSFX();
// Ambience
this.preloadAmbience();
console.log('🎵 AudioLoader: All audio queued for loading');
}
/**
* Preload narrator and NPC voices
*/
preloadVoices() {
const basePath = 'assets/audio/voices/';
// Narrator (cinematic)
this.loadAudioIfExists('narrator_intro', basePath + 'narrator/intro_cutscene.mp3');
this.loadAudioIfExists('narrator_memory', basePath + 'narrator/kai_memory_ana.mp3');
this.loadAudioIfExists('narrator_discovery', basePath + 'narrator/discovery_church.mp3');
// Kai
this.loadAudioIfExists('kai_voice_1', basePath + 'kai/kai_01.mp3');
this.loadAudioIfExists('kai_voice_2', basePath + 'kai/kai_02.mp3');
this.loadAudioIfExists('kai_voice_3', basePath + 'kai/kai_03.mp3');
this.loadAudioIfExists('kai_voice_4', basePath + 'kai/kai_04.mp3');
this.loadAudioIfExists('kai_voice_5', basePath + 'kai/kai_05.mp3');
// Ana
this.loadAudioIfExists('ana_voice_1', basePath + 'ana/ana_01.mp3');
this.loadAudioIfExists('ana_voice_2', basePath + 'ana/ana_02.mp3');
this.loadAudioIfExists('ana_voice_3', basePath + 'ana/ana_03.mp3');
this.loadAudioIfExists('ana_voice_4', basePath + 'ana/ana_04.mp3');
// Mayor
this.loadAudioIfExists('mayor_voice_1', basePath + 'mayor/mayor_01.mp3');
this.loadAudioIfExists('mayor_voice_2', basePath + 'mayor/mayor_02.mp3');
this.loadAudioIfExists('mayor_voice_3', basePath + 'mayor/mayor_03.mp3');
this.loadAudioIfExists('mayor_voice_4', basePath + 'mayor/mayor_04.mp3');
// Teacher
this.loadAudioIfExists('teacher_voice_1', basePath + 'teacher/teacher_01.mp3');
this.loadAudioIfExists('teacher_voice_2', basePath + 'teacher/teacher_02.mp3');
this.loadAudioIfExists('teacher_voice_3', basePath + 'teacher/teacher_03.mp3');
this.loadAudioIfExists('teacher_voice_4', basePath + 'teacher/teacher_04.mp3');
}
/**
* Preload background music
*/
preloadMusic() {
// For now, using placeholder silence or loading stubs
// Will be replaced with actual music files
console.log('🎵 Music: Placeholder mode (files not yet produced)');
}
/**
* Preload sound effects
*/
preloadSFX() {
// Placeholder - to be replaced with actual SFX
console.log('🎵 SFX: Placeholder mode');
}
/**
* Preload ambient sounds
*/
preloadAmbience() {
// Placeholder
console.log('🎵 Ambience: Placeholder mode');
}
/**
* Helper: Load audio only if file exists (prevents console errors)
*/
loadAudioIfExists(key, path) {
try {
this.scene.load.audio(key, path);
console.log(`✅ Queued: ${key} -> ${path}`);
} catch (error) {
console.warn(`⚠️ Skipped: ${key} (file may not exist yet)`);
}
}
/**
* Create silent fallback sounds to prevent errors
*/
createFallbackSounds() {
const silentAudio = {
duration: 0.1,
data: new Float32Array(4410) // 0.1s of silence at 44.1kHz
};
// Common sound keys that systems expect
const fallbackKeys = [
'background_music',
'music_chill_lofi',
'mayor_anthem',
'zombie_satisfied',
'zombie_groan',
'rare_gift_fanfare',
'wood_chop',
'gear_rattle'
];
fallbackKeys.forEach(key => {
if (!this.scene.sound.get(key)) {
// Create empty buffer to prevent errors
console.log(`📢 Created fallback for: ${key}`);
}
});
}
}
/**
* AUDIO INTEGRATION HELPER
* Add this to your main game scene
*/
export function setupAudioInScene(scene) {
// Create audio loader
const audioLoader = new AudioLoader(scene);
// In preload()
scene.load.on('complete', () => {
console.log('🎵 Audio preload complete!');
audioLoader.createFallbackSounds();
});
return audioLoader;
}