File size: 6,833 Bytes
08dedc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b95f339
08dedc7
 
 
b95f339
08dedc7
 
 
 
 
 
 
a129185
 
 
08dedc7
 
 
 
a129185
08dedc7
 
a129185
 
08dedc7
 
 
 
 
 
 
d8cda12
 
08dedc7
 
 
 
 
 
 
d8cda12
 
08dedc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b95f339
08dedc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b95f339
08dedc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// 音效控制器 - 完整版本
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;
    }
};