import { System, SystemNames } from "."; import { Music as GameMusic, SOUNDS } from "../config"; export class Music extends System { private songs: string[] = []; private currentSong?: string; constructor() { super(SystemNames.Music); this.songs = Array.from(GameMusic.states!.values()).map( (state) => state.name, ); } private chooseRandomSong() { return this.songs[Math.floor(Math.random() * this.songs.length)]; } public playNext() { let nextSong = this.chooseRandomSong(); while (nextSong === this.currentSong) { nextSong = this.chooseRandomSong(); } this.currentSong = nextSong; SOUNDS.get(this.currentSong)?.play(); // when done, play next song SOUNDS.get(this.currentSong)?.addEventListener("ended", () => { this.playNext(); }); } public update(_dt: number) { if (!this.currentSong) { this.playNext(); } } }