about summary refs log tree commit diff stats
path: root/html/story-teller/js/state.js
blob: e6c4fe77f7e855a0614fd2592e654c6a915633d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
  };