Add compat version with saves.
This commit is contained in:
parent
7c015787dd
commit
1a86c5854e
|
|
@ -1,11 +1,20 @@
|
|||
import { GameState } from '../types'
|
||||
|
||||
const SAVE_SLOT_PREFIX = 'dionysian_idle_save_'
|
||||
const CURRENT_SAVE_VERSION = 1
|
||||
|
||||
interface SaveData {
|
||||
version: number
|
||||
state: GameState
|
||||
}
|
||||
|
||||
const saveGame = (slot: number, state: GameState) => {
|
||||
try {
|
||||
const saveData = JSON.stringify(state)
|
||||
localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, saveData)
|
||||
const saveData: SaveData = {
|
||||
version: CURRENT_SAVE_VERSION,
|
||||
state
|
||||
}
|
||||
localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, JSON.stringify(saveData))
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('Failed to save game:', error)
|
||||
|
|
@ -17,7 +26,22 @@ const loadGame = (slot: number): GameState | null => {
|
|||
try {
|
||||
const saveData = localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`)
|
||||
if (!saveData) return null
|
||||
return JSON.parse(saveData)
|
||||
|
||||
const parsedData = JSON.parse(saveData)
|
||||
|
||||
// Handle legacy saves (no version field)
|
||||
if (!parsedData.version) {
|
||||
console.warn('Loading legacy save without version information')
|
||||
return parsedData as GameState
|
||||
}
|
||||
|
||||
// Check version compatibility
|
||||
if (parsedData.version !== CURRENT_SAVE_VERSION) {
|
||||
console.warn(`Save version mismatch: found ${parsedData.version}, expected ${CURRENT_SAVE_VERSION}`)
|
||||
return null
|
||||
}
|
||||
|
||||
return parsedData.state
|
||||
} catch (error) {
|
||||
console.error('Failed to load game:', error)
|
||||
return null
|
||||
|
|
|
|||
|
|
@ -91,6 +91,7 @@ export const useGameStore = create<
|
|||
configureEquipment: (equipmentId: string, recipeId: string) => void
|
||||
useEquipment: (equipmentId: string) => void
|
||||
addCash: (amount: number) => void
|
||||
addConsoleMessage: (text: string) => void
|
||||
}
|
||||
>((set, get) => {
|
||||
// Expose store methods to window for the save system
|
||||
|
|
@ -635,5 +636,9 @@ export const useGameStore = create<
|
|||
produce((state) => { state.cash += amount }),
|
||||
)
|
||||
},
|
||||
|
||||
addConsoleMessage: (text: string) => {
|
||||
addConsoleMessage(get(), text)
|
||||
},
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ import { useGameStore } from './useGameStore'
|
|||
import './saves' // Import the saves module to initialize the global saves object
|
||||
|
||||
export const useSaveSystem = () => {
|
||||
const addConsoleMessage = useGameStore((state) => state.addConsoleMessage)
|
||||
|
||||
useEffect(() => {
|
||||
// Auto-save every 30 seconds
|
||||
const interval = setInterval(() => {
|
||||
|
|
@ -18,6 +20,9 @@ export const useSaveSystem = () => {
|
|||
|
||||
// When starting the game, load from the autosave slot
|
||||
useEffect(() => {
|
||||
window.saves.load(0)
|
||||
}, [])
|
||||
const savedState = window.saves.load(0)
|
||||
if (savedState === null) {
|
||||
addConsoleMessage('No compatible save found. Starting new game.')
|
||||
}
|
||||
}, [addConsoleMessage])
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue