Fix ambient not playing

This commit is contained in:
schmelczerandras 2020-10-28 13:55:56 +01:00
parent d746854307
commit db2c4579b6
4 changed files with 28 additions and 16 deletions

1
deployment Submodule

@ -0,0 +1 @@
Subproject commit 7c7991fddae5bbf06811dda6d5eddba1e1621c53

View file

@ -108,7 +108,16 @@ const main = async () => {
let game: Game; let game: Game;
const firstClickListener = () => { 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.removeEventListener('click', firstClickListener);
}; };
document.addEventListener('click', firstClickListener); document.addEventListener('click', firstClickListener);

View file

@ -25,11 +25,11 @@ export abstract class OptionsHandler {
...this._options, ...this._options,
...stored, ...stored,
}; };
}
if (this._options.musicEnabled) { if (this._options.musicEnabled) {
SoundHandler.playAmbient(); SoundHandler.playAmbient();
} }
}
for (const k in inputElements) { for (const k in inputElements) {
const element = inputElements[k as keyof Options]; const element = inputElements[k as keyof Options];

View file

@ -10,8 +10,6 @@ export enum Sounds {
click = 'click', click = 'click',
} }
const concurrencyScale = 5;
export abstract class SoundHandler { export abstract class SoundHandler {
private static sounds: { [key in Sounds]: HTMLAudioElement }; private static sounds: { [key in Sounds]: HTMLAudioElement };
private static isAmbientPlaying = false; private static isAmbientPlaying = false;
@ -19,25 +17,29 @@ export abstract class SoundHandler {
private static ambientSound = new Audio(ambientSound); private static ambientSound = new Audio(ambientSound);
private static initialized = false; private static initialized = false;
public static async initialize() { public static async initialize(
onPlayKeypress: () => unknown = () => null,
onPauseKeypress: () => unknown = () => null,
) {
this.sounds = { this.sounds = {
[Sounds.hit]: await this.initializeSound(hitSound), [Sounds.hit]: await this.initializeSound(hitSound),
[Sounds.shoot]: await this.initializeSound(shootSound), [Sounds.shoot]: await this.initializeSound(shootSound),
[Sounds.click]: await this.initializeSound(clickSound), [Sounds.click]: await this.initializeSound(clickSound),
}; };
this.ambientSound.play(); await this.ambientSound.play();
this.ambientSound.muted = true; this.ambientSound.muted = true;
this.initialized = true; this.initialized = true;
this.ambientSound.onpause = onPauseKeypress;
this.ambientSound.onplay = onPlayKeypress;
setTimeout(() => {
this.ambientSound.muted = false; this.ambientSound.muted = false;
this.ambientSound.volume = 0.5; this.ambientSound.volume = 0.5;
this.ambientSound.loop = true; this.ambientSound.loop = true;
if (!this.isAmbientPlaying) { if (!this.isAmbientPlaying) {
this.ambientSound.pause(); this.ambientSound.pause();
} }
}, 100);
} }
private static async initializeSound(hitSound: string): Promise<HTMLAudioElement> { private static async initializeSound(hitSound: string): Promise<HTMLAudioElement> {