Skip to content

Commit 4c370bb

Browse files
committed
Add all upgrades to DOM upon creation, not unlock
1 parent 6b33671 commit 4c370bb

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

src/ts/handlers.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,19 @@ export class UpgradeHandler extends Handler<Upgrade> {
140140
return bought;
141141
}
142142

143-
/**
144-
* TODO: NEEDS STATISTIC SUPPORT
145-
* @param statistic Are we destroying all the upgrades in the Statistics page?
146-
*/
147-
destroyAllUpgrades(statistic: boolean=false) {
143+
updateStatisticUpgrades() {
144+
for (const upgrade of this) {
145+
upgrade.setStatisticVisibility(false);
146+
147+
if (upgrade.bought === false) continue;
148+
149+
upgrade.setStatisticVisibility(true);
150+
}
151+
}
152+
153+
destroyAllUpgrades() {
148154
for (const value of this) {
149-
value.destroy();
155+
value.setVisibility(false);
150156
}
151157
}
152158

@@ -155,11 +161,10 @@ export class UpgradeHandler extends Handler<Upgrade> {
155161
*
156162
* This is really only used in the context of loading, wherein unlocked and unbought upgrades will not yet exist.
157163
* */
158-
showUnlockedUpgrades() { //? is this still used? isn't this just checkUpgradeAvaliability?
164+
showUnlockedUpgrades() { //? can we just combine this with checkUpgradeAvailability?
159165
for (const value of this) {
160166
if (value.unlocked === true && value.bought !== true)
161-
// new Upgrade(game, game.UPGRADES_DATA[i]);
162-
value.create();
167+
value.setVisibility(true);
163168
}
164169
}
165170

@@ -171,7 +176,7 @@ export class UpgradeHandler extends Handler<Upgrade> {
171176
checkUpgradeAvailability() {
172177
for (const value of this) {
173178
if (value.building.bought >= value.buildingsRequired && value.unlocked === false) {
174-
value.create();
179+
value.setVisibility(true);
175180
value.unlocked = true;
176181
}
177182
}
@@ -194,11 +199,11 @@ export class UpgradeHandler extends Handler<Upgrade> {
194199
}
195200

196201
/**
197-
* Load save data for all registered upgrades
202+
* Apply save data state to all registered upgrades
198203
* @param saveObj The save object to load the data of
199204
* @param namespace Optional: if present will only load data for upgrades of a given namespace.
200205
*/
201-
loadUpgradesSave(saveObj: Record<string, UpgradeSave>, namespace: string=undefined) { // todo: test if works
206+
loadSave(saveObj: Record<string, UpgradeSave>, namespace: string=undefined) {
202207
for (const stringifiedIdentifier in saveObj) {
203208
const id = Identifier.fromString(stringifiedIdentifier);
204209

src/ts/helper.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,20 @@ export function branchQuickSwitch(main: any, beta: any, develop: any) {
152152
case VersionBranch.DEVELOP:
153153
return develop;
154154
}
155-
}
155+
}
156+
157+
/**
158+
* Counts the number of visible children (`display` CSS property is not equal to `none`) in a given element
159+
* @param element The element to count the visible children of
160+
* @returns The number of visible children
161+
*/
162+
export function countVisibleChildren(element: HTMLElement): number {
163+
let count = 0;
164+
for (const child of element.children) {
165+
const style = window.getComputedStyle(child);
166+
if (style.display !== "none") {
167+
count++;
168+
}
169+
}
170+
return count;
171+
}

src/ts/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If you're not a modder, still read the docs here: https://github.com/clickercook
88

99
import { createChangelogEntry, versionChangelogs } from "./changelogs.js";
1010
import { branchQuickSwitch, Interval, object2HTML, url } from "./helper.js";
11-
import { updateStatisticUpgrades, expandUpgradesHolder, UpgradeSave } from "./upgrades.js";
11+
import { expandUpgradesHolder, UpgradeSave } from "./upgrades.js";
1212
import { SaveProvider, Savinator } from "./saving.js";
1313
import { Mod } from "./mods.js"
1414
import ClickerCookie from "./clickercookie.js"
@@ -205,7 +205,7 @@ export class Game extends SaveProvider {
205205

206206
// todo asap: 0.6 save transfer
207207

208-
updateStatisticUpgrades();
208+
Handlers.UPGRADE.updateStatisticUpgrades();
209209

210210
/* change version branch specific stuff */
211211
// change title
@@ -338,13 +338,13 @@ export class Game extends SaveProvider {
338338
Handlers.BACKGROUND.setBackground(saveData.currentBackgroundKey);
339339

340340
// the following doesn't have anything to do with GameSaveData but will be done here because this spot makes the most sense
341-
Handlers.UPGRADE.loadUpgradesSave(saveData.upgradesSave);
341+
Handlers.UPGRADE.loadSave(saveData.upgradesSave);
342342

343343
Handlers.UPGRADE.destroyAllUpgrades();
344344
Handlers.UPGRADE.showUnlockedUpgrades();
345345

346346
document.getElementById("upgradesBoughtCounter").innerText = Handlers.UPGRADE.upgradesBought.toString();
347-
updateStatisticUpgrades();
347+
Handlers.UPGRADE.updateStatisticUpgrades();
348348
}
349349
}
350350

src/ts/upgrades.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Building } from "./buildings.js";
2-
import { clamp, commaify, url } from "./helper.js";
2+
import { clamp, commaify, countVisibleChildren, url } from "./helper.js";
33
import { hideTooltip } from "./tooltip.js";
44
import { Game } from "./main.js";
55
import ClickerCookie from "./clickercookie.js";
@@ -26,7 +26,7 @@ export interface UpgradeData {
2626
}
2727

2828
export function expandUpgradesHolder(retract: boolean=false) {
29-
const upgradesShown = document.getElementById("upgradesHolder").children.length;
29+
const upgradesShown = countVisibleChildren(document.getElementById("upgradesHolder"));
3030

3131
const rowsOfUpgrades = Math.ceil(upgradesShown / 5);
3232

@@ -40,16 +40,6 @@ export function expandUpgradesHolder(retract: boolean=false) {
4040
holder.style.height = `${size}px`;
4141
}
4242

43-
export function updateStatisticUpgrades() {
44-
for (const upgrade of Handlers.UPGRADE) {
45-
upgrade.destroyStatistic();
46-
47-
if (upgrade.bought === false) continue;
48-
49-
upgrade.createStatistic();
50-
}
51-
}
52-
5343
export class Upgrade {
5444
private clickercookie: ClickerCookie;
5545

@@ -125,17 +115,9 @@ export class Upgrade {
125115
console.warn(`Unable to get the image file "${UPGRADE_ICON}" for the "${this.name}" upgrade (probably 404), falling back to "unknown" image.`);
126116
};
127117
img.src = UPGRADE_ICON; //* so the onload/onerror events are actually fired
128-
}
129-
130-
/**
131-
* Appends `this.html` (the upgrade) to `element`.
132-
* @param element The element to append to. Should pretty much always be the upgrades holder.
133-
*/
134-
create(element: HTMLElement=document.getElementById("upgradesHolder")) {
135-
element!.appendChild(this.html);
136-
}
137118

138-
createStatistic() {
119+
// create it
120+
document.getElementById("upgradesHolder").appendChild(this.html);
139121
document.getElementById("upgradesBoughtStatsHolder").appendChild(this.statisticHTML);
140122
}
141123

@@ -145,7 +127,7 @@ export class Upgrade {
145127
this.clickercookie.cookies -= this.price;
146128
this.bought = true;
147129
this.hovered(); //? i don't remember why this is here but i know it's important just trust me
148-
this.destroy();
130+
this.setVisibility(false);
149131

150132
this.building.CPSGiven *= 2;
151133
this.building.CPSGain *= 2;
@@ -155,7 +137,7 @@ export class Upgrade {
155137

156138
document.getElementById("upgradesBoughtCounter")!.innerText = Handlers.UPGRADE.upgradesBought.toString();
157139

158-
updateStatisticUpgrades(); // we don't run createStatistic() here and instead run this so that the order shown in the upgrades menu is correct
140+
Handlers.UPGRADE.updateStatisticUpgrades(); // we don't run createStatistic() here and instead run this so that the order shown in the upgrades menu is correct
159141
}
160142

161143
hovered(statistic: boolean=false) { // todo: this is very similar to Building.hovered
@@ -185,13 +167,10 @@ export class Upgrade {
185167
}
186168
}
187169

188-
destroy() {
189-
this.html.remove();
190-
hideTooltip(); // hide the tooltip so it doesn't stick around after you buy the upgrade
170+
setVisibility(bool: boolean) {
171+
this.html.style.display = bool ? "inline-block" : "none";
191172
}
192-
193-
destroyStatistic() {
194-
this.statisticHTML.remove();
195-
hideTooltip(); // if you're somehow hovering it lol
173+
setStatisticVisibility(bool: boolean) {
174+
this.statisticHTML.style.display = bool ? "block" : "none";
196175
}
197176
}

0 commit comments

Comments
 (0)