From ecd8d064a370ce0b9d4a353f6151638b60838208 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Fri, 17 Apr 2020 22:56:57 -0500 Subject: Add cosmic cargo --- src/cosmic-cargo/static/audio.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/cosmic-cargo/static/audio.js (limited to 'src/cosmic-cargo/static/audio.js') diff --git a/src/cosmic-cargo/static/audio.js b/src/cosmic-cargo/static/audio.js new file mode 100644 index 0000000..3abc79a --- /dev/null +++ b/src/cosmic-cargo/static/audio.js @@ -0,0 +1,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(); +} \ No newline at end of file -- cgit v1.2.3