diff options
Diffstat (limited to 'html/story-teller/js/state.js')
-rw-r--r-- | html/story-teller/js/state.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/html/story-teller/js/state.js b/html/story-teller/js/state.js new file mode 100644 index 0000000..e6c4fe7 --- /dev/null +++ b/html/story-teller/js/state.js @@ -0,0 +1,43 @@ +export const createGameState = () => ({ + currentScene: 'start', + inventory: new Set(), + actionLog: [], + collectedItems: new Set(), + }); + + + +export const updateGameState = (state, updates) => ({ + ...state, + ...updates, +}); + +const STORAGE_KEY = 'gameState'; + +export const saveGameState = (state) => { + const stateToSave = { + currentScene: state.currentScene, + inventory: Array.from(state.inventory), + actionLog: state.actionLog, + }; + localStorage.setItem(STORAGE_KEY, JSON.stringify(stateToSave)); +}; + +export const loadGameState = () => { + const savedState = localStorage.getItem(STORAGE_KEY); + if (savedState) { + const parsedState = JSON.parse(savedState); + return { + currentScene: parsedState.currentScene, + inventory: new Set(parsedState.inventory), + actionLog: parsedState.actionLog, + }; + } + return createGameState(); +}; + +export const resetGameState = () => { + localStorage.removeItem(STORAGE_KEY); + return createGameState(); + }; + \ No newline at end of file |