Skip to content

Commit

Permalink
Randomize first music track on load
Browse files Browse the repository at this point in the history
  • Loading branch information
bob0005 committed Jan 7, 2025
1 parent 7acc2dd commit 5492200
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions client/apps/game/src/hooks/store/useUIStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<AppStore>((set, get) => ({
theme: "light",
Expand All @@ -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) => {
Expand Down

0 comments on commit 5492200

Please sign in to comment.