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 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);