summaryrefslogtreecommitdiff
path: root/src/engine/systems/Music.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-07 20:45:47 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-07 20:45:47 -0700
commite6e29440563e33bb67e0ad51f9fb6c5c2c3fe809 (patch)
tree5deaee322ff1a039dc44a3cb52ecc48a671fda2a /src/engine/systems/Music.ts
parent823620b2a6ebb7ece619991e47a37ad46542b69f (diff)
downloadthe-abstraction-engine-e6e29440563e33bb67e0ad51f9fb6c5c2c3fe809.tar.gz
the-abstraction-engine-e6e29440563e33bb67e0ad51f9fb6c5c2c3fe809.zip
level one (applications prototype finished!)
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();
+ }
+ }
+}