aboutsummaryrefslogtreecommitdiff
path: root/src/cosmic-cargo/static/audio.js
blob: 3abc79a4bd242421d170dda07733af2938cb16f7 (plain)
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
var audio = {
    "bgm": new Audio("Assets/music.ogg"),
    "shop": new Audio("Assets/shop.ogg"),
    "encounter": new Audio("Assets/encounter.ogg"),
    "endgame": new Audio("Assets/endgame.ogg"),
    "gameover": new Audio("Assets/gameover.ogg"),
    "alert": new Audio("Assets/SoundEffects/Alert.wav"),
    "select": new Audio("Assets/SoundEffects/SelectNoise.wav"),
    "move": new Audio("Assets/SoundEffects/MoveCursor.wav"),
}
var music = ["bgm", "shop", "endgame", "gameover", "encounter"]
var sfx = ["alert", "select", "move"]

function play_audio(sound, loop = false){
    var curr_audio = audio[sound];
    if(curr_audio != undefined){
        if(loop){
            curr_audio.addEventListener('ended', loop_audio_listener, false)
        } else {
            curr_audio.removeEventListener("ended", loop_audio_listener, false)
        }
        // play if paused or if not music
        if(curr_audio.paused || !music.includes(sound)){
            curr_audio.currentTime = 0;
            curr_audio.play();
        }
    }
}

function pause_audio(sound){
    var curr_audio = audio[sound];
    if(curr_audio != undefined){
        curr_audio.pause();
    }
}

function loop_audio_listener(){
    this.currentTime = 0;
    this.play();
}