diff --git a/main.js b/main.js index 2169ceb..4ad5b0a 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,9 @@ const sounds = { - hitBlock: new Audio('assets/hit_block.wav'), - hitCoin: new Audio('assets/hit_coin.wav'), - hitBall: new Audio('assets/hit_ball.wav'), - roundOver: new Audio('assets/round_success.wav'), - gameOver: new Audio('assets/game_over.wav') + hitBlock: new SoundManager('assets/hit_block.wav'), + hitCoin: new SoundManager('assets/hit_coin.wav'), + hitBall: new SoundManager('assets/hit_ball.wav'), + roundOver: new SoundManager('assets/round_success.wav'), + gameOver: new SoundManager('assets/game_over.wav'), }; let coinCount = 0; diff --git a/sound-manager.js b/sound-manager.js new file mode 100644 index 0000000..62833cc --- /dev/null +++ b/sound-manager.js @@ -0,0 +1,19 @@ +class SoundManager { + constructor(soundSource) { + this.soundSource = soundSource; + this.sound = new Audio(this.soundSource); + this.lastPlayed = 0; + } + + play() { + if (Date.now() - this.lastPlayed < 50) + return; + + const node = this.sound.cloneNode(); + node.play(); + setTimeout(() => { + node.remove(); + }, this.sound.duration * 1000); + this.lastPlayed = Date.now(); + } +}