diff --git a/src/game/index.js b/src/game/index.js index 2a9ba2f..e9a100b 100644 --- a/src/game/index.js +++ b/src/game/index.js @@ -4,26 +4,44 @@ import Items from './items'; import MeowCommand from './commands/meow'; import DarkCommand from "./commands/dark"; -if (localStorage.getItem("save")) { - document.getElementById("save-game-found").hidden = false; - document.getElementById("before-play").hidden = true; - document.getElementById("load-save-game").addEventListener("click", () => { - document.getElementById("save-game-found").hidden = true; - document.getElementById("play-area").hidden = false; - startGame(false); - }) - document.getElementById("start-new-game").addEventListener("click", () => { - document.getElementById("save-game-found").hidden = true; - document.getElementById("play-area").hidden = false; - startGame(true); - }) +const states = { + BEFORE_PLAY: "before-play", + SAVE_GAME_FOUND: "save-game-found", + PLAY_AREA: "play-area" +}; + +function setState(newState) { + const allStates = Object.values(states); + + allStates.forEach(state => { + document.getElementById(state).hidden = state !== newState; + }); } -document.getElementById("begin").addEventListener("click", () => { - document.getElementById("before-play").hidden = true; - document.getElementById("play-area").hidden = false; +function addButtonEventListener(buttonId, onClick) { + document.getElementById(buttonId).addEventListener("click", onClick); +} + +if (localStorage.getItem("save")) { + setState(states.SAVE_GAME_FOUND); + + addButtonEventListener("load-save-game", () => { + setState(states.PLAY_AREA); + startGame(false); + }); + + addButtonEventListener("start-new-game", () => { + setState(states.PLAY_AREA); + startGame(true); + }); +} else { + setState(states.BEFORE_PLAY); +} + +addButtonEventListener("begin", () => { + setState(states.PLAY_AREA); startGame(true); -}) +}); function startGame(newGame) { const game = new Game(newGame);