浏览代码

Cache references to the building buttons

tags/v0.0.6
Fen Dweller 5 年前
父节点
当前提交
7f237b2ad3
找不到此签名对应的密钥 GPG 密钥 ID: E80B35A6F11C3656
共有 1 个文件被更改,包括 27 次插入7 次删除
  1. +27
    -7
      gorge.js

+ 27
- 7
gorge.js 查看文件

@@ -243,8 +243,11 @@ function renderResources() {
return renderLines(list); return renderLines(list);
} }


const buildingButtons = {}

function displayBuildings() { function displayBuildings() {
const count = buildingCount(); const count = buildingCount();

for (const [key, value] of Object.entries(belongings)) { for (const [key, value] of Object.entries(belongings)) {


if (!belongings[key].visible) { if (!belongings[key].visible) {
@@ -256,23 +259,23 @@ function displayBuildings() {
continue; continue;
} }
belongings[key].visible = true; 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); const buildingCost = costOfBuilding(key, count);


name.innerText = value.count + " " + (value.count == 1 ? buildings[key].name : buildings[key].plural); name.innerText = value.count + " " + (value.count == 1 ? buildings[key].name : buildings[key].plural);
cost.innerText = render(buildingCost.food) + " food"; cost.innerText = render(buildingCost.food) + " food";


if (canAfford(buildingCost)) {
if (canAfford(buildingCost) && button.classList.contains("building-button-disabled")) {
button.classList.remove("building-button-disabled"); button.classList.remove("building-button-disabled");
cost.classList.add("building-button-cost-valid"); cost.classList.add("building-button-cost-valid");
} else {
} else if (!canAfford(buildingCost) && !button.classList.contains("building-button-disabled")) {
button.classList.add("building-button-disabled"); button.classList.add("building-button-disabled");
cost.classList.add("building-button-cost-invalid"); cost.classList.add("building-button-cost-invalid");
} }
@@ -417,10 +420,27 @@ function setup() {
registerListeners(); registerListeners();
load(); load();
unlockAtStart(); unlockAtStart();
initializeCaches();
updateAll(); 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() { function unlockAtStart() {
unlockBuilding("micro"); unlockBuilding("micro");




正在加载...
取消
保存