Refactor Sound class (#6)

master
Garrett Mills 4 years ago
parent 261e8e1d34
commit 2c1e7889cf
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -1,29 +1,46 @@
function sound(src) { /**
* A thin wrapper for sound effects.
this.sound = document.createElement("audio"); */
class Sound {
this.sound.src = src; /**
this.sound.setAttribute("preload", "auto"); * Construct the sound.
this.sound.setAttribute("controls", "none"); * @param {string} src - URL of the file
this.sound.style.display = "none"; */
document.body.appendChild(this.sound); constructor(src) {
this.play = function(){ /**
this.sound.play(); * The sound element.
* @type {HTMLAudioElement}
*/
this.sound = document.createElement('audio')
this.sound.src = src
this.sound.setAttribute('preload', 'auto')
this.sound.setAttribute('controls', 'none')
this.sound.style.display = 'none'
document.body.appendChild(this.sound)
}
/**
* Start playing the sound.
*/
play() {
this.sound.play()
} }
this.stop = function(){
this.sound.pause(); /**
* Pause the sound.
*/
stop() {
this.sound.pause()
} }
} }
const GameSounds = { const GameSounds = {
Victory: new sound("/lib/sounds/cartoon_success_fanfair.mp3"), Victory: new Sound('/lib/sounds/cartoon_success_fanfair.mp3'),
Fire: new sound("/lib/sounds/zapsplat_warfare_mortar_projectile_launch_002_25232.mp3"), Fire: new Sound('/lib/sounds/zapsplat_warfare_mortar_projectile_launch_002_25232.mp3'),
Hit: new sound("/lib/sounds/zapsplat_warfare_bomb_whizz_in_hit_close_by_explosion_med_003_48060.mp3"), Hit: new Sound('/lib/sounds/zapsplat_warfare_bomb_whizz_in_hit_close_by_explosion_med_003_48060.mp3'),
Miss: new sound("/lib/sounds/zapsplat_nature_water_pour_medium_amount_deep_sudden_fast_002_52765.mp3"), Miss: new Sound('/lib/sounds/zapsplat_nature_water_pour_medium_amount_deep_sudden_fast_002_52765.mp3'),
} }
export{ export { GameSounds }
GameSounds
}

Loading…
Cancel
Save