Skip to content

Commit c639154

Browse files
committed
Improve how buildings unlock (similar to upgrades)
1 parent a4cd758 commit c639154

File tree

5 files changed

+46
-58
lines changed

5 files changed

+46
-58
lines changed

src/ts/buildings.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export interface BuildingData {
1616
img?: string;
1717
/** Default is 1.15 */
1818
upgradeCostMultiplier?: number;
19+
/**
20+
* Returns a boolean based on whether the building may be unlocked.
21+
*
22+
* Will be run automatically in game loop and visibility will be set accordingly.
23+
*/
24+
condition(game: Game): boolean;
1925
}
2026

2127
export interface BuildingSave {
@@ -42,6 +48,7 @@ export class Building implements SaveProvider<BuildingSave> {
4248
CPSGiven: number;
4349
CPSGain: number;
4450
readonly upgradeCostMultiplier: number;
51+
condition: (game: Game) => boolean;
4552

4653
private _bought: number;
4754
/** Number of buildings bought */
@@ -52,7 +59,13 @@ export class Building implements SaveProvider<BuildingSave> {
5259

5360
document.getElementById(`${this.name}Cost`).innerText = commaify(this.upgradeCost); //? does this make sense here?
5461
}
55-
unlocked: boolean;
62+
private _unlocked: boolean;
63+
public get unlocked() { return this._unlocked }
64+
public set unlocked(bool: boolean) {
65+
this._unlocked = bool;
66+
67+
this.html.style.display = bool ? "block" : "none";
68+
}
5669

5770
html: HTMLDivElement;
5871
// todo: clickercookie should not be passed as a parameter
@@ -116,6 +129,7 @@ export class Building implements SaveProvider<BuildingSave> {
116129
this.upgradeCostMultiplier = data.upgradeCostMultiplier;
117130
else
118131
this.upgradeCostMultiplier = 1.15;
132+
this.condition = data.condition;
119133

120134
//* if upgradeCost is too low, Math.floor'ing it after multiplying it by upgradeCostMultiplier can actually just get you the same upgradeCost as before. warn in console if this will happen, but don't throw an error in case it's intended or smth stupid
121135
if (Math.floor(this.baseUpgradeCost * this.upgradeCostMultiplier) === this.baseUpgradeCost) {
@@ -165,10 +179,6 @@ export class Building implements SaveProvider<BuildingSave> {
165179
tooltip.style.display = "block";
166180
}
167181

168-
setVisibility(bool: boolean) {
169-
this.html.style.display = bool ? "block" : "none";
170-
}
171-
172182
destroy() {
173183
this.html.remove();
174184
}

src/ts/clickercookie.ts

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -93,72 +93,72 @@ export default class ClickerCookie extends Mod<ClickerCookieSaveData> {
9393
quote: "type in cookies",
9494
upgradeCost: 15,
9595
CPSGain: 0.1,
96-
img: "img/keyboard.png"
96+
img: "img/keyboard.png",
97+
condition() { return true; }
9798
},
9899
grandpa: {
99100
name: "Grandpa",
100101
namePlural: "Grandpas",
101102
quote: "as long as gramps gets a cut",
102103
upgradeCost: 100,
103104
CPSGain: 1,
104-
img: "img/grandpa.png"
105+
img: "img/grandpa.png",
106+
condition(game) { return (game.clickercookie.totalCookies >= 100) ? true : false }
105107
},
106108
ranch: {
107109
name: "Ranch",
108110
namePlural: "Ranches",
109111
quote: "not the dressing kind",
110112
upgradeCost: 1_100,
111113
CPSGain: 8,
112-
img: "img/ranch.png"
114+
img: "img/ranch.png",
115+
condition(game) { return (game.clickercookie.totalCookies >= 700) ? true : false }
113116
},
114117
television: {
115118
name: "Television",
116119
namePlural: "Televisions",
117120
quote: "hold infomercials on your cookies",
118121
upgradeCost: 12_000,
119122
CPSGain: 47,
120-
img: "img/tv.png"
123+
img: "img/tv.png",
124+
condition(game) { return (game.clickercookie.totalCookies >= 8_000) ? true : false }
121125
},
122126
worker: {
123127
name: "Worker",
124128
namePlural: "Workers",
125129
quote: "cookies via manual labor",
126130
upgradeCost: 130_000,
127131
CPSGain: 260,
128-
img: "img/worker.png"
132+
img: "img/worker.png",
133+
condition(game) { return (game.clickercookie.totalCookies >= 80_000) ? true : false }
129134
},
130135
wallet: {
131136
name: "Wallet",
132137
namePlural: "Wallets",
133138
quote: "more storage space for your vast amount of cookie income",
134139
upgradeCost: 1_400_000,
135140
CPSGain: 1_440,
136-
img: "img/wallet.png"
141+
img: "img/wallet.png",
142+
condition(game) { return (game.clickercookie.totalCookies >= 700_000) ? true : false }
137143
},
138144
church: {
139145
name: "Church",
140146
namePlural: "Churches",
141147
quote: "pray to the almighty cookie gods",
142148
upgradeCost: 20_000_000,
143149
CPSGain: 7_800,
144-
img: "img/church.png"
150+
img: "img/church.png",
151+
condition(game) { return (game.clickercookie.totalCookies >= 15_000_000) ? true : false }
145152
}
146153
};
147154

148-
this.keyboard = new Building(this, this.BUILDINGS_DATA.keyboard);
149-
this.keyboard.unlocked = true;
155+
this.keyboard = new Building(this, this.BUILDINGS_DATA.keyboard); // conditional returns true by default
150156
this.grandpa = new Building(this, this.BUILDINGS_DATA.grandpa);
151-
this.grandpa.setVisibility(false);
152157
this.ranch = new Building(this, this.BUILDINGS_DATA.ranch);
153-
this.ranch.setVisibility(false);
154158
this.television = new Building(this, this.BUILDINGS_DATA.television);
155-
this.television.setVisibility(false);
156159
this.worker = new Building(this, this.BUILDINGS_DATA.worker);
157-
this.worker.setVisibility(false);
158160
this.wallet = new Building(this, this.BUILDINGS_DATA.wallet);
159-
this.wallet.setVisibility(false);
160161
this.church = new Building(this, this.BUILDINGS_DATA.church);
161-
this.church.setVisibility(false);
162162

163163
// upgrades
164164
const keyboardBoughtFunc = () => {
@@ -497,7 +497,6 @@ export default class ClickerCookie extends Mod<ClickerCookieSaveData> {
497497
};
498498

499499
Mod.registerKooh("click", () => { this.cookieClicked() });
500-
Mod.registerKooh("loop", () => { this.gameLoop() });
501500
Mod.registerKooh("cps", () => { this.cpsUpdate() });
502501
Mod.registerKooh("personalization", () => {
503502
Handlers.UPGRADE.getFromIdentifier(new Identifier(this.NAMESPACE, "keyboard1")).desc = `Multiplys Keyboard and clicking ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().name.toLowerCase()} production by 2`;
@@ -558,35 +557,6 @@ export default class ClickerCookie extends Mod<ClickerCookieSaveData> {
558557
}
559558
}
560559

561-
gameLoop() {
562-
// todo: this should be handled by a callback func in building data
563-
// building unlocks
564-
if (this.totalCookies >= 100) {
565-
this.grandpa.unlocked = true;
566-
this.grandpa.setVisibility(true);
567-
}
568-
if (this.totalCookies >= 700) {
569-
this.ranch.unlocked = true;
570-
this.ranch.setVisibility(true);
571-
}
572-
if (this.totalCookies >= 8000) {
573-
this.television.unlocked = true;
574-
this.television.setVisibility(true);
575-
}
576-
if (this.totalCookies >= 80000) {
577-
this.worker.unlocked = true;
578-
this.worker.setVisibility(true);
579-
}
580-
if (this.totalCookies >= 700000) {
581-
this.wallet.unlocked = true;
582-
this.wallet.setVisibility(true);
583-
}
584-
if (this.totalCookies >= 15000000) {
585-
this.church.unlocked = true;
586-
this.church.setVisibility(true);
587-
}
588-
}
589-
590560
updateStatistics() { // todo: make this only run when the stats page is a. first pulled up, b. continued to be pulled up
591561
document.getElementById("cookiesStat").innerText = `${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural}: ${makeSlightlyImperfectFloatNice(this.cookies)}`;
592562
document.getElementById("allTimeCookies").innerText = `All Time ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural}: ${makeSlightlyImperfectFloatNice(this.totalCookies)}`;
@@ -617,13 +587,6 @@ export default class ClickerCookie extends Mod<ClickerCookieSaveData> {
617587
}
618588
}
619589
loadSaveData(saveData: ClickerCookieSaveData) {
620-
this.grandpa.setVisibility(false);
621-
this.ranch.setVisibility(false);
622-
this.television.setVisibility(false);
623-
this.worker.setVisibility(false);
624-
this.wallet.setVisibility(false);
625-
this.church.setVisibility(false);
626-
627590
this.cookies = saveData.cookies;
628591
this.totalCookies = saveData.totalCookies;
629592
this.cookiesPerClick = saveData.cookiesPerClick;

src/ts/exmod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export class NewMod extends Mod<ModSave> {
2828
quote: "i love anana",
2929
upgradeCost: 8,
3030
CPSGain: 5,
31-
img: "https://www.minecraft.net/content/dam/minecraftnet/franchise/logos/minecraft-creeper-face.jpg" // lol
31+
img: "https://www.minecraft.net/content/dam/minecraftnet/franchise/logos/minecraft-creeper-face.jpg", // lol
32+
condition() { return true; }
3233
});
3334

3435
this.UPGRADES_DATA = {

src/ts/handlers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ export class BuildingHandler extends Handler<Building> {
127127
return bought;
128128
}
129129

130+
/**
131+
* Iterates through every building registered to this handler and checks if it meets its own unlock condition. If it does, set its unlock status to true. If it does not, set it to false.
132+
*/
133+
updateBuildingsUnlocked() {
134+
for (const value of this) {
135+
if (value.condition(Game.getInstance())) {
136+
value.unlocked = true;
137+
} else {
138+
value.unlocked = false;
139+
}
140+
}
141+
}
142+
130143
/**
131144
* Dumps savedata for all registered buildings
132145
* @param namespace Optional: if present only dump savedata from this namespace

src/ts/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export class Game implements SaveProvider<GameSaveData> {
321321
if (!this.initialized) return;
322322

323323
Handlers.UPGRADE.checkUpgradeAvailability();
324+
Handlers.BUILDING.updateBuildingsUnlocked();
324325

325326
let cps = 0;
326327
for (const building of Handlers.BUILDING) {

0 commit comments

Comments
 (0)