From 7f237b2ad3adb0438fa8867e0088bd49763441e6 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Tue, 24 Dec 2019 18:13:56 -0500 Subject: [PATCH] Cache references to the building buttons --- gorge.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/gorge.js b/gorge.js index dc3a568..b00a5e3 100644 --- a/gorge.js +++ b/gorge.js @@ -243,8 +243,11 @@ function renderResources() { return renderLines(list); } +const buildingButtons = {} + function displayBuildings() { const count = buildingCount(); + for (const [key, value] of Object.entries(belongings)) { if (!belongings[key].visible) { @@ -256,23 +259,23 @@ function displayBuildings() { continue; } belongings[key].visible = true; - document.querySelector("#building-" + key).classList.remove("hidden"); + let button = buildingButtons[key].button; + button.classList.remove("hidden"); } - let button = document.querySelector("#building-" + key); - - let name = document.querySelector("#building-" + key + " > .building-button-name"); - let cost = document.querySelector("#building-" + key + " > .building-button-cost"); + let button = buildingButtons[key].button; + let name = buildingButtons[key].name; + let cost = buildingButtons[key].cost; const buildingCost = costOfBuilding(key, count); name.innerText = value.count + " " + (value.count == 1 ? buildings[key].name : buildings[key].plural); cost.innerText = render(buildingCost.food) + " food"; - if (canAfford(buildingCost)) { + if (canAfford(buildingCost) && button.classList.contains("building-button-disabled")) { button.classList.remove("building-button-disabled"); cost.classList.add("building-button-cost-valid"); - } else { + } else if (!canAfford(buildingCost) && !button.classList.contains("building-button-disabled")) { button.classList.add("building-button-disabled"); cost.classList.add("building-button-cost-invalid"); } @@ -417,10 +420,27 @@ function setup() { registerListeners(); load(); unlockAtStart(); + initializeCaches(); updateAll(); } +function initializeCaches() { + for (const [key, value] of Object.entries(belongings)) { + + let button = document.querySelector("#building-" + key); + let name = document.querySelector("#building-" + key + " > .building-button-name"); + let cost = document.querySelector("#building-" + key + " > .building-button-cost"); + + buildingButtons[key] = { + button: button, + name: name, + cost: cost + } + } + +} + function unlockAtStart() { unlockBuilding("micro");