From 2c1e7889cf2b28097e7d61693721dd266c88a5f6 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 12 Sep 2020 15:36:52 -0500 Subject: [PATCH] Refactor Sound class (#6) --- src/module/sounds.js | 59 ++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/module/sounds.js b/src/module/sounds.js index 0ccf3f9..ad8282b 100644 --- a/src/module/sounds.js +++ b/src/module/sounds.js @@ -1,29 +1,46 @@ -function sound(src) { - - 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); - this.play = function(){ - this.sound.play(); +/** + * A thin wrapper for sound effects. + */ +class Sound { + /** + * Construct the sound. + * @param {string} src - URL of the file + */ + constructor(src) { + /** + * 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 = { - Victory: new sound("/lib/sounds/cartoon_success_fanfair.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"), - Miss: new sound("/lib/sounds/zapsplat_nature_water_pour_medium_amount_deep_sudden_fast_002_52765.mp3"), + Victory: new Sound('/lib/sounds/cartoon_success_fanfair.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'), + Miss: new Sound('/lib/sounds/zapsplat_nature_water_pour_medium_amount_deep_sudden_fast_002_52765.mp3'), } -export{ - GameSounds -} \ No newline at end of file +export { GameSounds }