Skip to content

Commit

Permalink
Preload audio files
Browse files Browse the repository at this point in the history
Audio files are preloaded.
  • Loading branch information
erdemunlu committed Feb 9, 2024
1 parent e4a3152 commit b497b84
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/core/AudioManager.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { Howl } from "howler";

export class AudioManager {
soundFiles = [
{ name: "soundtrack.ogg", src: "./assets/Audio/Musics/soundtrack.ogg" },
{ name: "shot_orange.wav", src: "./assets/Audio/Sounds/shot_orange.wav" },
{ name: "shot_green.wav", src: "./assets/Audio/Sounds/shot_green.wav" },
{ name: "shot_blue.wav", src: "./assets/Audio/Sounds/shot_blue.wav" },
{ name: "shot_enemy_weak.wav", src: "./assets/Audio/Sounds/shot_enemy_weak.wav" },
{ name: "shot_enemy_strong.wav", src: "./assets/Audio/Sounds/shot_enemy_strong.wav" },
{ name: "gethit.ogg", src: "./assets/Audio/Sounds/gethit.ogg" },
{ name: "death.wav", src: "./assets/Audio/Sounds/death.wav" },
];
sounds: { [key: string]: Howl } = {};

constructor() {
this.preloadAudioFiles();
this.playSoundtrack();
}

playSoundtrack() {
const sound = new Howl({
src: ["./assets/Audio/Musics/soundtrack.ogg"],
loop: true,
volume: 0.05,
preloadAudioFiles(): void {
this.soundFiles.forEach((soundFile) => {
this.sounds[soundFile.name] = new Howl({
src: [soundFile.src],
preload: true,
});
});
sound.play();
}

playSound(soundName: string, soundVolume: number = 1) {
const sound = new Howl({
src: [`./assets/Audio/Sounds/${soundName}`],
volume: soundVolume,
});
playSoundtrack(): void {
const soundtrack = this.sounds["soundtrack.ogg"];
soundtrack.loop(true);
soundtrack.volume(0.05);
soundtrack.play();
}
playSound(soundName: string, soundVolume: number = 1): void {
const sound = this.sounds[soundName];
sound.volume(soundVolume);
sound.play();
}
}

0 comments on commit b497b84

Please sign in to comment.