Files
OpenFrontIO/src/client/sound/SoundManager.ts
T
icslucas b31200a3ac MUSIC (#2090)
## Description:
add music to the game
Describe the PR.
add music
<img width="549" height="770" alt="image"
src="https://github.com/user-attachments/assets/d8457d85-6f63-4024-8b69-572f8c9bb225"
/>

## Please complete the following:

- [X] I have added screenshots for all UI updates
- [X] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [X] I have added relevant tests to the test directory
- [X] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

Lucas
2025-09-30 11:13:32 -07:00

49 lines
1.4 KiB
TypeScript

import { Howl, Howler } from "howler";
import of4 from "../../../proprietary/sounds/music/of4.mp3";
import openfront from "../../../proprietary/sounds/music/openfront.mp3";
import war from "../../../proprietary/sounds/music/war.mp3";
class SoundManager {
private backgroundMusic: Howl[] = [];
private currentTrack: number = 0;
constructor() {
this.backgroundMusic = [
new Howl({ src: [of4], loop: false, onend: this.playNext.bind(this) }),
new Howl({
src: [openfront],
loop: false,
onend: this.playNext.bind(this),
}),
new Howl({ src: [war], loop: false, onend: this.playNext.bind(this) }),
];
this.setBackgroundMusicVolume(0);
}
public playBackgroundMusic(): void {
if (
this.backgroundMusic.length > 0 &&
!this.backgroundMusic[this.currentTrack].playing()
) {
this.backgroundMusic[this.currentTrack].play();
}
}
public stopBackgroundMusic(): void {
if (this.backgroundMusic.length > 0) {
this.backgroundMusic[this.currentTrack].stop();
}
}
public setBackgroundMusicVolume(volume: number): void {
const newVolume = Math.max(0, Math.min(1, volume));
Howler.volume(newVolume);
}
private playNext(): void {
this.currentTrack = (this.currentTrack + 1) % this.backgroundMusic.length;
this.playBackgroundMusic();
}
}
export default new SoundManager();