53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { useEffect } from "react"
|
|
import { hasSaveInSlot, useGameStore } from "./useGameStore"
|
|
|
|
export const useSaveSystem = () => {
|
|
const { saveToSlot, loadFromSlot } = useGameStore()
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === 'r' && e.ctrlKey) {
|
|
localStorage.clear()
|
|
}
|
|
|
|
const slot =
|
|
{
|
|
'1': 1,
|
|
'2': 2,
|
|
'3': 3,
|
|
'!': 1,
|
|
'@': 2,
|
|
'#': 3,
|
|
}[e.key] ?? null
|
|
|
|
if (slot !== null) {
|
|
if (e.shiftKey) {
|
|
saveToSlot(slot)
|
|
console.log(`Game saved to slot ${slot}`)
|
|
} else if (hasSaveInSlot(slot)) {
|
|
loadFromSlot(slot)
|
|
console.log(`Game loaded from slot ${slot}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown)
|
|
const interval = setInterval(() => {
|
|
saveToSlot(0)
|
|
}, 30000)
|
|
|
|
console.log('Initalized save system')
|
|
|
|
return () => {
|
|
window.removeEventListener('keydown', handleKeyDown)
|
|
clearInterval(interval)
|
|
}
|
|
}, [saveToSlot, loadFromSlot])
|
|
|
|
// When starting the game, load from the autosave slot
|
|
useEffect(() => {
|
|
loadFromSlot(0)
|
|
}, [])
|
|
}
|