54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
const SOUNDS = {
|
|
jump: new Audio('assets/sfx/jump.mp3'),
|
|
duck: new Audio('assets/sfx/duck.mp3'),
|
|
coin: new Audio('assets/sfx/coin.mp3'),
|
|
hit: new Audio('assets/sfx/hit.mp3'),
|
|
powerup: new Audio('assets/sfx/powerup.mp3'),
|
|
music: new Audio('assets/sfx/music_loop.mp3')
|
|
};
|
|
|
|
// Config
|
|
SOUNDS.jump.volume = 0.4;
|
|
SOUNDS.coin.volume = 0.3;
|
|
SOUNDS.hit.volume = 0.6;
|
|
SOUNDS.music.loop = true;
|
|
SOUNDS.music.volume = 0.2;
|
|
|
|
|
|
let isMuted = localStorage.getItem('escape_muted') === 'true';
|
|
|
|
function playSound(name) {
|
|
if (isMuted || !SOUNDS[name]) return;
|
|
|
|
const soundClone = SOUNDS[name].cloneNode();
|
|
soundClone.volume = SOUNDS[name].volume;
|
|
soundClone.play().catch(() => {});
|
|
}
|
|
|
|
function toggleMute() {
|
|
isMuted = !isMuted;
|
|
|
|
|
|
localStorage.setItem('escape_muted', isMuted);
|
|
|
|
|
|
if(isMuted) {
|
|
SOUNDS.music.pause();
|
|
} else {
|
|
SOUNDS.music.play().catch(()=>{});
|
|
}
|
|
|
|
return isMuted;
|
|
}
|
|
|
|
function startMusic() {
|
|
|
|
if(!isMuted) {
|
|
SOUNDS.music.play().catch(e => console.log("Audio Autoplay blocked", e));
|
|
}
|
|
}
|
|
|
|
|
|
function getMuteState() {
|
|
return isMuted;
|
|
} |