Spaces:
Running
Running
import { writable, derived } from "svelte/store"; | |
import type * as GAME from "vibegame"; | |
type GameInstance = Awaited<ReturnType<typeof GAME.run>>; | |
export interface GameState { | |
instance: GameInstance | null; | |
isRunning: boolean; | |
isStarting: boolean; | |
isAutoRunning: boolean; | |
} | |
function createGameStore() { | |
const { subscribe, set, update } = writable<GameState>({ | |
instance: null, | |
isRunning: false, | |
isStarting: false, | |
isAutoRunning: true, | |
}); | |
return { | |
subscribe, | |
setInstance: (instance: GameInstance | null) => | |
update((state) => ({ | |
...state, | |
instance, | |
isRunning: instance !== null, | |
})), | |
setStarting: (isStarting: boolean) => | |
update((state) => ({ | |
...state, | |
isStarting, | |
})), | |
setAutoRunning: (isAutoRunning: boolean) => | |
update((state) => ({ | |
...state, | |
isAutoRunning, | |
})), | |
reset: () => | |
set({ | |
instance: null, | |
isRunning: false, | |
isStarting: false, | |
isAutoRunning: true, | |
}), | |
}; | |
} | |
export const gameStore = createGameStore(); | |
export const isGameRunning = derived(gameStore, ($game) => $game.isRunning); | |