Spaces:
Running
Running
| // 音效控制器 - 完整版本 | |
| const audioController = { | |
| bgm: null, | |
| attackSounds: {}, | |
| resultSounds: {}, | |
| isBGMPlaying: false, | |
| isInitialized: false, | |
| // 初始化音效控制器 | |
| init() { | |
| if (this.isInitialized) return; | |
| console.log('音效控制器初始化中...'); | |
| // 初始化背景音樂 | |
| try { | |
| this.bgm = new Audio('BGM.mp3'); | |
| this.bgm.loop = true; | |
| this.bgm.volume = 0.3; | |
| this.bgm.preload = 'auto'; | |
| console.log('背景音樂已載入: BGM.mp3'); | |
| } catch (error) { | |
| console.log('背景音樂載入失敗:', error); | |
| } | |
| // 初始化攻擊音效 | |
| try { | |
| this.attackSounds = { | |
| strong: new Audio('atk1.mp3'), // 強攻擊音效 | |
| medium: new Audio('atk2.mp3'), // 普通攻擊音效 | |
| weak: new Audio('atk3.mp3') // 弱攻擊音效 | |
| }; | |
| // 設置攻擊音效音量 | |
| Object.values(this.attackSounds).forEach(sound => { | |
| sound.volume = 0.6; | |
| sound.preload = 'auto'; | |
| }); | |
| console.log('攻擊音效已載入: atk1.mp3, atk2.mp3, atk3.mp3'); | |
| } catch (error) { | |
| console.log('攻擊音效載入失敗:', error); | |
| } | |
| // 初始化結果音效 | |
| try { | |
| this.resultSounds = { | |
| win: new Audio('win.mp3'), | |
| lose: new Audio('Lose.mp3') | |
| }; | |
| // 設置結果音效音量 | |
| Object.values(this.resultSounds).forEach(sound => { | |
| sound.volume = 0.6; | |
| sound.preload = 'auto'; | |
| }); | |
| console.log('勝利/失敗音效已載入'); | |
| } catch (error) { | |
| console.log('結果音效載入失敗:', error); | |
| } | |
| this.isInitialized = true; | |
| console.log('音效控制器初始化完成'); | |
| }, | |
| // 播放背景音樂 | |
| playBGM() { | |
| if (!this.bgm) { | |
| console.log('背景音樂未載入'); | |
| return; | |
| } | |
| try { | |
| const playPromise = this.bgm.play(); | |
| if (playPromise !== undefined) { | |
| playPromise.then(() => { | |
| this.isBGMPlaying = true; | |
| console.log('背景音樂開始播放: BGM.mp3'); | |
| }).catch(error => { | |
| console.log('背景音樂播放失敗:', error); | |
| // 瀏覽器可能阻止自動播放,這是正常的 | |
| }); | |
| } | |
| } catch (error) { | |
| console.log('背景音樂播放錯誤:', error); | |
| } | |
| }, | |
| // 暫停背景音樂 | |
| pauseBGM() { | |
| if (this.bgm && this.isBGMPlaying) { | |
| this.bgm.pause(); | |
| this.isBGMPlaying = false; | |
| console.log('背景音樂已暫停'); | |
| } | |
| }, | |
| // 切換背景音樂 | |
| toggleBGM() { | |
| if (!this.bgm) { | |
| console.log('背景音樂功能已載入,但可能因瀏覽器限制無法自動播放'); | |
| return; | |
| } | |
| if (this.isBGMPlaying) { | |
| this.pauseBGM(); | |
| } else { | |
| this.playBGM(); | |
| } | |
| }, | |
| // 播放攻擊音效 | |
| playAttackSound(type = 'medium') { | |
| if (!this.attackSounds[type]) { | |
| console.log(`攻擊音效 ${type} 未載入`); | |
| return; | |
| } | |
| try { | |
| // 重置音效到開始位置 | |
| this.attackSounds[type].currentTime = 0; | |
| const playPromise = this.attackSounds[type].play(); | |
| if (playPromise !== undefined) { | |
| playPromise.then(() => { | |
| console.log(`播放 ${type} 攻擊音效`); | |
| }).catch(error => { | |
| console.log(`${type} 攻擊音效播放失敗:`, error); | |
| }); | |
| } | |
| } catch (error) { | |
| console.log(`${type} 攻擊音效播放錯誤:`, error); | |
| } | |
| }, | |
| // 播放結果音效 | |
| playResultSound(type = 'win') { | |
| if (!this.resultSounds[type]) { | |
| console.log(`結果音效 ${type} 未載入`); | |
| return; | |
| } | |
| try { | |
| // 重置音效到開始位置 | |
| this.resultSounds[type].currentTime = 0; | |
| const playPromise = this.resultSounds[type].play(); | |
| if (playPromise !== undefined) { | |
| playPromise.then(() => { | |
| console.log(`播放 ${type} 結果音效`); | |
| }).catch(error => { | |
| console.log(`${type} 結果音效播放失敗:`, error); | |
| }); | |
| } | |
| } catch (error) { | |
| console.log(`${type} 結果音效播放錯誤:`, error); | |
| } | |
| }, | |
| // 停止所有音效 | |
| stopAll() { | |
| // 停止背景音樂 | |
| if (this.bgm) { | |
| this.bgm.pause(); | |
| this.bgm.currentTime = 0; | |
| this.isBGMPlaying = false; | |
| } | |
| // 停止攻擊音效 | |
| Object.values(this.attackSounds).forEach(sound => { | |
| if (sound) { | |
| sound.pause(); | |
| sound.currentTime = 0; | |
| } | |
| }); | |
| // 停止結果音效 | |
| Object.values(this.resultSounds).forEach(sound => { | |
| if (sound) { | |
| sound.pause(); | |
| sound.currentTime = 0; | |
| } | |
| }); | |
| console.log('所有音效已停止'); | |
| }, | |
| // 設置音量 | |
| setVolume(bgmVolume = 0.3, effectVolume = 0.5) { | |
| if (this.bgm) { | |
| this.bgm.volume = Math.max(0, Math.min(1, bgmVolume)); | |
| } | |
| Object.values(this.attackSounds).forEach(sound => { | |
| if (sound) { | |
| sound.volume = Math.max(0, Math.min(1, effectVolume)); | |
| } | |
| }); | |
| Object.values(this.resultSounds).forEach(sound => { | |
| if (sound) { | |
| sound.volume = Math.max(0, Math.min(1, effectVolume)); | |
| } | |
| }); | |
| console.log(`音量設置 - 背景音樂: ${bgmVolume}, 音效: ${effectVolume}`); | |
| }, | |
| // 檢查音效支援 | |
| checkAudioSupport() { | |
| const audio = new Audio(); | |
| const support = { | |
| mp3: audio.canPlayType('audio/mpeg') !== '', | |
| ogg: audio.canPlayType('audio/ogg') !== '', | |
| wav: audio.canPlayType('audio/wav') !== '', | |
| m4a: audio.canPlayType('audio/mp4') !== '' | |
| }; | |
| console.log('音效格式支援:', support); | |
| return support; | |
| } | |
| }; | |