From 79dc66f1ec0d085150d08f107b415989b24a1e6d Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Sun, 13 Jan 2019 11:59:37 -0500 Subject: [PATCH] Move buttons are no longer re-created every time the game refreshes --- satiate.js | 1 + world.js | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/satiate.js b/satiate.js index bddc461..c39ad43 100644 --- a/satiate.js +++ b/satiate.js @@ -83,6 +83,7 @@ function init(story) { initWorld(story, state); initAudio(story, state); initGame(story, state); + resetControls(state); story.intro.setup(state); diff --git a/world.js b/world.js index 396e1ec..1f45237 100644 --- a/world.js +++ b/world.js @@ -11,6 +11,14 @@ dirs = { "descend": "Down" } +moveListeners = { + +} + +actionListeners = { + +} + function initWorld(story, state) { state.world = story["world"]; initRoomState(state); @@ -23,6 +31,25 @@ function initRoomState(state) { }); } +function resetControls(state) { + const moveHolder = document.querySelector("#move-holder"); + + moveHolder.innerHTML = ""; + + Object.entries(dirs).forEach(([dir, name]) => { + const button = document.createElement("button"); + button.classList.add("move-button") + button.id = "move-" + dir; + button.classList.add("disabled"); + button.setAttribute("disabled", "true"); + button.textContent = dirs[dir]; + moveHolder.appendChild(button); + + moveListeners[dir] = undefined; + }); +} + + function showActionDescription(desc) { const descHolder = document.querySelector("#desc"); @@ -69,6 +96,8 @@ function moveToRoom(src, exit, dest, state) { state.player.location = dest; + resetControls(); + refresh(); } @@ -92,6 +121,8 @@ function goToRoom(dest, state) { state.player.location = dest; + resetControls(); + refresh(); } @@ -109,18 +140,11 @@ function updateRoom(state) { areaName.textContent = room.name; areaDesc.textContent = room.desc; - const moveHolder = document.querySelector("#move-holder"); - - moveHolder.innerHTML = ""; - Object.entries(dirs).forEach(([dir, name]) => { - const button = document.createElement("button"); - button.classList.add("move-button") - button.id = "move-" + dir; + const button = document.querySelector("#move-" + dir); button.classList.add("disabled"); button.setAttribute("disabled", "true"); button.textContent = dirs[dir]; - moveHolder.appendChild(button); }); if (room.exits) { @@ -148,11 +172,19 @@ function updateRoom(state) { button.classList.remove("disabled"); button.removeAttribute("disabled"); - button.addEventListener("click", () => { - // todo: log + if (moveListeners[dir]) { + button.removeEventListener("click", moveListeners[dir]); + moveListeners[dir] = undefined; + } + + moveFunc = () => { moveToRoom(room, exit, exit.target, state); - }) + }; + + button.addEventListener("click", moveFunc); + moveListeners[dir] = moveFunc; + button.addEventListener("mouseenter", () => { showActionDescription(exit.desc); });