Simple state machine for intro dialog

master
Talon 2023-05-08 15:10:18 +02:00
parent 2d3ce1982c
commit 2837263abd
1 changed files with 35 additions and 17 deletions

View File

@ -4,26 +4,44 @@ import Items from './items';
import MeowCommand from './commands/meow'; import MeowCommand from './commands/meow';
import DarkCommand from "./commands/dark"; import DarkCommand from "./commands/dark";
if (localStorage.getItem("save")) { const states = {
document.getElementById("save-game-found").hidden = false; BEFORE_PLAY: "before-play",
document.getElementById("before-play").hidden = true; SAVE_GAME_FOUND: "save-game-found",
document.getElementById("load-save-game").addEventListener("click", () => { PLAY_AREA: "play-area"
document.getElementById("save-game-found").hidden = true; };
document.getElementById("play-area").hidden = false;
startGame(false); function setState(newState) {
}) const allStates = Object.values(states);
document.getElementById("start-new-game").addEventListener("click", () => {
document.getElementById("save-game-found").hidden = true; allStates.forEach(state => {
document.getElementById("play-area").hidden = false; document.getElementById(state).hidden = state !== newState;
startGame(true); });
})
} }
document.getElementById("begin").addEventListener("click", () => { function addButtonEventListener(buttonId, onClick) {
document.getElementById("before-play").hidden = true; document.getElementById(buttonId).addEventListener("click", onClick);
document.getElementById("play-area").hidden = false; }
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); startGame(true);
}) });
function startGame(newGame) { function startGame(newGame) {
const game = new Game(newGame); const game = new Game(newGame);