From db2c4579b6e80bdc95b2e6f1e38cac1754aa145d Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Wed, 28 Oct 2020 13:55:56 +0100 Subject: [PATCH] Fix ambient not playing --- deployment | 1 + frontend/src/index.ts | 11 ++++++++++- frontend/src/scripts/options-handler.ts | 6 +++--- frontend/src/scripts/sound-handler.ts | 26 +++++++++++++------------ 4 files changed, 28 insertions(+), 16 deletions(-) create mode 160000 deployment diff --git a/deployment b/deployment new file mode 160000 index 0000000..7c7991f --- /dev/null +++ b/deployment @@ -0,0 +1 @@ +Subproject commit 7c7991fddae5bbf06811dda6d5eddba1e1621c53 diff --git a/frontend/src/index.ts b/frontend/src/index.ts index 9e37179..8f44a66 100644 --- a/frontend/src/index.ts +++ b/frontend/src/index.ts @@ -108,7 +108,16 @@ const main = async () => { let game: Game; const firstClickListener = () => { - SoundHandler.initialize(); + SoundHandler.initialize( + () => { + enableMusic.checked = true; + enableMusic.dispatchEvent(new Event('change')); + }, + () => { + enableMusic.checked = false; + enableMusic.dispatchEvent(new Event('change')); + }, + ); document.removeEventListener('click', firstClickListener); }; document.addEventListener('click', firstClickListener); diff --git a/frontend/src/scripts/options-handler.ts b/frontend/src/scripts/options-handler.ts index b4c5c6c..3a59bdc 100644 --- a/frontend/src/scripts/options-handler.ts +++ b/frontend/src/scripts/options-handler.ts @@ -25,10 +25,10 @@ export abstract class OptionsHandler { ...this._options, ...stored, }; + } - if (this._options.musicEnabled) { - SoundHandler.playAmbient(); - } + if (this._options.musicEnabled) { + SoundHandler.playAmbient(); } for (const k in inputElements) { diff --git a/frontend/src/scripts/sound-handler.ts b/frontend/src/scripts/sound-handler.ts index c17b5ec..304d361 100644 --- a/frontend/src/scripts/sound-handler.ts +++ b/frontend/src/scripts/sound-handler.ts @@ -10,8 +10,6 @@ export enum Sounds { click = 'click', } -const concurrencyScale = 5; - export abstract class SoundHandler { private static sounds: { [key in Sounds]: HTMLAudioElement }; private static isAmbientPlaying = false; @@ -19,25 +17,29 @@ export abstract class SoundHandler { private static ambientSound = new Audio(ambientSound); private static initialized = false; - public static async initialize() { + public static async initialize( + onPlayKeypress: () => unknown = () => null, + onPauseKeypress: () => unknown = () => null, + ) { this.sounds = { [Sounds.hit]: await this.initializeSound(hitSound), [Sounds.shoot]: await this.initializeSound(shootSound), [Sounds.click]: await this.initializeSound(clickSound), }; - this.ambientSound.play(); + await this.ambientSound.play(); this.ambientSound.muted = true; this.initialized = true; + this.ambientSound.onpause = onPauseKeypress; + this.ambientSound.onplay = onPlayKeypress; - setTimeout(() => { - this.ambientSound.muted = false; - this.ambientSound.volume = 0.5; - this.ambientSound.loop = true; - if (!this.isAmbientPlaying) { - this.ambientSound.pause(); - } - }, 100); + this.ambientSound.muted = false; + this.ambientSound.volume = 0.5; + this.ambientSound.loop = true; + + if (!this.isAmbientPlaying) { + this.ambientSound.pause(); + } } private static async initializeSound(hitSound: string): Promise {