File size: 2,464 Bytes
0bb733c
 
 
9fd3a67
 
0bb733c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fd3a67
 
 
 
 
 
0bb733c
9fd3a67
 
 
 
 
 
 
 
0bb733c
9fd3a67
 
0bb733c
2a6d2b0
 
 
9fd3a67
 
2a6d2b0
 
 
 
 
 
 
 
 
9fd3a67
2a6d2b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function playSound(sound) {
    if (isSoundOn) {
        let soundToPlay;

        // Determine which sound to play
        switch (sound) {
            case 'clownSound1':
                soundToPlay = clownSound1Snd;
                break;
            case 'clownSound2':
                soundToPlay = clownSound2Snd;
                break;
            case 'clownSeesYou':
                soundToPlay = clownSeesYouSnd;
                break;
            case 'gfMove':
                soundToPlay = gfMoveSnd;
                break;
            case 'lose':
                soundToPlay = loseSnd;
                break;
            case 'unlockDoor':
                soundToPlay = unlockDoorSnd;
                break;
            case 'useKnife':
                soundToPlay = useKnifeSnd;
                break;
            case 'message':
                soundToPlay = messageSnd;
                break;
            case 'success':
                soundToPlay = successSnd;
                break;
        }

        // Play the sound if it's defined and loaded
        if (soundToPlay && soundToPlay.isLoaded()) {
            soundToPlay.setLoop(false); // Ensure looping is disabled
            soundToPlay.playMode('restart'); // Set play mode to 'restart'
            if (soundToPlay.isPlaying()) {
                soundToPlay.stop(); // Stop any existing playback
            }
            soundToPlay.play(); // Play the sound once
        } else {
            console.log('Sound file is not loaded or undefined.');
        }
    }
}



function toggleSound() {
    isSoundOn = !isSoundOn;
    localStorage.setItem("isSoundOn", isSoundOn.toString());

    if (isSoundOn) {
        if (bgMusic.currentTime < 10) {
            bgMusic.currentTime = 10;
        }
        bgMusic.play();
       
    } else {
        bgMusic.pause();
    }
    updateSoundIcon();
}

function updateSoundIcon() {
    soundIcon.src = isSoundOn ? "/static/assets/img/sondon.png" : "/static/assets/img/soundoff.png";
}

function playMessageSound() {
    try {
        const messageSound = document.getElementById("messageSound");
        if (messageSound) {
            messageSound.currentTime = 0;
            messageSound.volume = 1.0;
            messageSound.play().catch((error) => {
                console.log("Could not play message sound:", error);
            });
        }
    } catch (error) {
        console.error("Error playing message sound:", error);
    }
}