summaryrefslogtreecommitdiff
path: root/src/engine/systems/Music.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/systems/Music.ts')
-rw-r--r--src/engine/systems/Music.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/engine/systems/Music.ts b/src/engine/systems/Music.ts
new file mode 100644
index 0000000..6e2004d
--- /dev/null
+++ b/src/engine/systems/Music.ts
@@ -0,0 +1,40 @@
+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();
+ }
+ }
+}