Change gameplay

This commit is contained in:
schmelczerandras 2020-10-24 22:24:27 +02:00
parent d79900e3ea
commit 34dae300da
56 changed files with 906 additions and 400 deletions

View file

@ -8,37 +8,71 @@ export enum Sounds {
hit = 'hit',
shoot = 'shoot',
click = 'click',
ambient = 'ambient',
}
const concurrencyScale = 5;
export abstract class SoundHandler {
private static sounds: { [key in Sounds]: HTMLAudioElement };
private static isAmbientPlaying = false;
public static initialize() {
private static ambientSound = new Audio(ambientSound);
private static initialized = false;
public static async initialize() {
this.sounds = {
[Sounds.hit]: new Audio(hitSound),
[Sounds.shoot]: new Audio(shootSound),
[Sounds.click]: new Audio(clickSound),
[Sounds.ambient]: new Audio(ambientSound),
[Sounds.hit]: await this.initializeSound(hitSound),
[Sounds.shoot]: await this.initializeSound(shootSound),
[Sounds.click]: await this.initializeSound(clickSound),
};
this.sounds.ambient.volume = 0.5;
this.ambientSound.play();
this.ambientSound.muted = true;
this.initialized = true;
setTimeout(() => {
this.ambientSound.muted = false;
this.ambientSound.volume = 0.5;
if (!this.isAmbientPlaying) {
this.ambientSound.pause();
}
}, 100);
}
public static play(sound: Sounds) {
if (OptionsHandler.options.soundsEnabled) {
if (this.sounds[sound].currentTime > 0) {
(this.sounds[sound].cloneNode() as HTMLAudioElement).play();
} else {
this.sounds[sound].play();
}
private static async initializeSound(hitSound: string): Promise<HTMLAudioElement> {
const sound = new Audio(hitSound);
sound.muted = true;
await sound.play();
sound.pause();
sound.muted = false;
sound.currentTime = 0;
return sound;
}
public static play(sound: Sounds, volume: number = 1) {
if (!this.initialized || !OptionsHandler.options.soundsEnabled) {
return;
}
const audio =
this.sounds[sound].currentTime > 0
? (this.sounds[sound].cloneNode(true) as HTMLAudioElement)
: this.sounds[sound];
audio.volume = volume;
audio.play();
}
public static playAmbient() {
this.sounds.ambient.play();
this.isAmbientPlaying = true;
if (this.initialized) {
this.ambientSound.play();
}
}
public static stopAmbient() {
this.sounds.ambient.pause();
this.isAmbientPlaying = false;
if (this.initialized) {
this.ambientSound.pause();
}
}
}