From 5492200ff8ce20666e4ca808d3a23a68d963fa9c Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 7 Jan 2025 20:24:16 +0100 Subject: [PATCH] Randomize first music track on load --- client/apps/game/src/hooks/store/useUIStore.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/client/apps/game/src/hooks/store/useUIStore.tsx b/client/apps/game/src/hooks/store/useUIStore.tsx index 307f1e4bc..fcc95a762 100644 --- a/client/apps/game/src/hooks/store/useUIStore.tsx +++ b/client/apps/game/src/hooks/store/useUIStore.tsx @@ -4,6 +4,7 @@ import { ContractAddress } from "@bibliothecadao/eternum"; import React from "react"; import { create } from "zustand"; import { subscribeWithSelector } from "zustand/middleware"; +import { tracks } from "../useMusic"; import { BuildModeStore, createBuildModeStoreSlice } from "./_buildModeStore"; import { PopupsStore, createPopupsSlice } from "./_popupsStore"; import { ThreeStore, createThreeStoreSlice } from "./_threeStore"; @@ -73,6 +74,12 @@ interface UIStore { export type AppStore = UIStore & PopupsStore & ThreeStore & BuildModeStore & RealmStore & BlockchainStore & WorldStore; +const getRandomTrackIndex = (tracksLength: number) => { + return Math.floor(Math.random() * tracksLength); +}; + +const initialTrackIndex = getRandomTrackIndex(tracks.length); + const useUIStore = create( subscribeWithSelector((set, get) => ({ theme: "light", @@ -84,9 +91,9 @@ const useUIStore = create( isSideMenuOpened: true, toggleSideMenu: () => set((state) => ({ isSideMenuOpened: !state.isSideMenuOpened })), isSoundOn: localStorage.getItem("soundEnabled") ? localStorage.getItem("soundEnabled") === "true" : true, - trackName: "Day Break", + trackName: tracks[initialTrackIndex].name, setTrackName: (name) => set({ trackName: name }), - trackIndex: 1, + trackIndex: initialTrackIndex, setTrackIndex: (index) => set({ trackIndex: index }), toggleSound: () => set((state) => {