CoolFace
Apppublic

Lashtw/ChickenMonster2

sourceHugging Facemitupdated 3mo agoView on Hugging Face
0likes
audio_controller.js225 linesDownload Raw Back to root
1// 音效控制器 - 完整版本2const audioController = {3    bgm: null,4    attackSounds: {},5    resultSounds: {},6    isBGMPlaying: false,7    isInitialized: false,8    9    // 初始化音效控制器10    init() {11        if (this.isInitialized) return;12        13        console.log('音效控制器初始化中...');14        15        // 初始化背景音樂16        try {17            this.bgm = new Audio('BGM.mp3');18            this.bgm.loop = true;19            this.bgm.volume = 0.3;20            this.bgm.preload = 'auto';21            console.log('背景音樂已載入: BGM.mp3');22        } catch (error) {23            console.log('背景音樂載入失敗:', error);24        }25        26        // 初始化攻擊音效27        try {28            this.attackSounds = {29                strong: new Audio('atk1.mp3'),    // 強攻擊音效30                medium: new Audio('atk2.mp3'),    // 普通攻擊音效31                weak: new Audio('atk3.mp3')       // 弱攻擊音效32            };33            34            // 設置攻擊音效音量35            Object.values(this.attackSounds).forEach(sound => {36                sound.volume = 0.6;37                sound.preload = 'auto';38            });39            40            console.log('攻擊音效已載入: atk1.mp3, atk2.mp3, atk3.mp3');41        } catch (error) {42            console.log('攻擊音效載入失敗:', error);43        }44        45        // 初始化結果音效46        try {47            this.resultSounds = {48                win: new Audio('win.mp3'),49                lose: new Audio('Lose.mp3')50            };51            52            // 設置結果音效音量53            Object.values(this.resultSounds).forEach(sound => {54                sound.volume = 0.6;55                sound.preload = 'auto';56            });57            58            console.log('勝利/失敗音效已載入');59        } catch (error) {60            console.log('結果音效載入失敗:', error);61        }62        63        this.isInitialized = true;64        console.log('音效控制器初始化完成');65    },66    67    // 播放背景音樂68    playBGM() {69        if (!this.bgm) {70            console.log('背景音樂未載入');71            return;72        }73        74        try {75            const playPromise = this.bgm.play();76            if (playPromise !== undefined) {77                playPromise.then(() => {78                    this.isBGMPlaying = true;79                    console.log('背景音樂開始播放: BGM.mp3');80                }).catch(error => {81                    console.log('背景音樂播放失敗:', error);82                    // 瀏覽器可能阻止自動播放,這是正常的83                });84            }85        } catch (error) {86            console.log('背景音樂播放錯誤:', error);87        }88    },89    90    // 暫停背景音樂91    pauseBGM() {92        if (this.bgm && this.isBGMPlaying) {93            this.bgm.pause();94            this.isBGMPlaying = false;95            console.log('背景音樂已暫停');96        }97    },98    99    // 切換背景音樂100    toggleBGM() {101        if (!this.bgm) {102            console.log('背景音樂功能已載入,但可能因瀏覽器限制無法自動播放');103            return;104        }105        106        if (this.isBGMPlaying) {107            this.pauseBGM();108        } else {109            this.playBGM();110        }111    },112    113    // 播放攻擊音效114    playAttackSound(type = 'medium') {115        if (!this.attackSounds[type]) {116            console.log(`攻擊音效 ${type} 未載入`);117            return;118        }119        120        try {121            // 重置音效到開始位置122            this.attackSounds[type].currentTime = 0;123            const playPromise = this.attackSounds[type].play();124            125            if (playPromise !== undefined) {126                playPromise.then(() => {127                    console.log(`播放 ${type} 攻擊音效`);128                }).catch(error => {129                    console.log(`${type} 攻擊音效播放失敗:`, error);130                });131            }132        } catch (error) {133            console.log(`${type} 攻擊音效播放錯誤:`, error);134        }135    },136    137    // 播放結果音效138    playResultSound(type = 'win') {139        if (!this.resultSounds[type]) {140            console.log(`結果音效 ${type} 未載入`);141            return;142        }143        144        try {145            // 重置音效到開始位置146            this.resultSounds[type].currentTime = 0;147            const playPromise = this.resultSounds[type].play();148            149            if (playPromise !== undefined) {150                playPromise.then(() => {151                    console.log(`播放 ${type} 結果音效`);152                }).catch(error => {153                    console.log(`${type} 結果音效播放失敗:`, error);154                });155            }156        } catch (error) {157            console.log(`${type} 結果音效播放錯誤:`, error);158        }159    },160    161    // 停止所有音效162    stopAll() {163        // 停止背景音樂164        if (this.bgm) {165            this.bgm.pause();166            this.bgm.currentTime = 0;167            this.isBGMPlaying = false;168        }169        170        // 停止攻擊音效171        Object.values(this.attackSounds).forEach(sound => {172            if (sound) {173                sound.pause();174                sound.currentTime = 0;175            }176        });177        178        // 停止結果音效179        Object.values(this.resultSounds).forEach(sound => {180            if (sound) {181                sound.pause();182                sound.currentTime = 0;183            }184        });185        186        console.log('所有音效已停止');187    },188    189    // 設置音量190    setVolume(bgmVolume = 0.3, effectVolume = 0.5) {191        if (this.bgm) {192            this.bgm.volume = Math.max(0, Math.min(1, bgmVolume));193        }194        195        Object.values(this.attackSounds).forEach(sound => {196            if (sound) {197                sound.volume = Math.max(0, Math.min(1, effectVolume));198            }199        });200        201        Object.values(this.resultSounds).forEach(sound => {202            if (sound) {203                sound.volume = Math.max(0, Math.min(1, effectVolume));204            }205        });206        207        console.log(`音量設置 - 背景音樂: ${bgmVolume}, 音效: ${effectVolume}`);208    },209    210    // 檢查音效支援211    checkAudioSupport() {212        const audio = new Audio();213        const support = {214            mp3: audio.canPlayType('audio/mpeg') !== '',215            ogg: audio.canPlayType('audio/ogg') !== '',216            wav: audio.canPlayType('audio/wav') !== '',217            m4a: audio.canPlayType('audio/mp4') !== ''218        };219        220        console.log('音效格式支援:', support);221        return support;222    }223};224 225