Skip to content

Commit f1f4ce4

Browse files
committed
Make building upgrade cost programmatic, change mod registration flow (how init works)
1 parent 5a504bd commit f1f4ce4

File tree

6 files changed

+80
-82
lines changed

6 files changed

+80
-82
lines changed

src/ts/buildings.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { Game } from "./main.js";
44
import { hideTooltip } from "./tooltip.js";
55

66
export interface BuildingData {
7+
/** Display name for your building. Should be capitialized.*/
78
name: string;
9+
/** *Plural* display name for your building. Should be capitialized.*/
810
namePlural: string;
911
quote: string;
12+
/** Base upgrade cost for the building. */
1013
upgradeCost: number;
1114
CPSGain: number;
1215
img?: string;
@@ -20,11 +23,14 @@ export class Building {
2023
name: string;
2124
namePlural: string;
2225
quote: string;
23-
private _upgradeCost: number;
24-
public get upgradeCost() { return this._upgradeCost }
25-
public set upgradeCost(num: number) {
26-
this._upgradeCost = num;
27-
document.getElementById(`${this.name}Cost`).innerText = commaify(this._upgradeCost);
26+
readonly baseUpgradeCost: number;
27+
/** Upgrade cost is obtained programmatically using the base cost, multiplier, and number bought. */
28+
public get upgradeCost() {
29+
let cost = this.baseUpgradeCost;
30+
for (let i = 0; i < this.bought; i++) {
31+
cost = Math.floor(cost * this.upgradeCostMultiplier);
32+
}
33+
return cost;
2834
}
2935
CPSGiven: number;
3036
CPSGain: number;
@@ -36,9 +42,10 @@ export class Building {
3642
public set bought(num: number) {
3743
this._bought = num;
3844
document.getElementById(`${this.namePlural}Bought`).innerText = commaify(this._bought);
45+
46+
document.getElementById(`${this.name}Cost`).innerText = commaify(this.upgradeCost); //? does this make sense here?
3947
}
4048
unlocked: boolean;
41-
plural: "s" | "es";
4249

4350
html: HTMLDivElement;
4451
constructor(clickercookie: ClickerCookie, data: BuildingData) {
@@ -68,7 +75,7 @@ export class Building {
6875
const namePriceDiv = document.createElement("div");
6976
const buildingName = document.createElement("p");
7077
buildingName.className = "building-name";
71-
buildingName.innerText = capitalize(data.name);
78+
buildingName.innerText = data.name;
7279
namePriceDiv.appendChild(buildingName);
7380

7481
const buildingPrice = document.createElement("p");
@@ -94,7 +101,7 @@ export class Building {
94101
this.name = data.name;
95102
this.namePlural = data.namePlural;
96103
this.quote = data.quote;
97-
this.upgradeCost = data.upgradeCost;
104+
this.baseUpgradeCost = data.upgradeCost;
98105
this.CPSGiven = 0;
99106
this.CPSGain = data.CPSGain;
100107
if (data.upgradeCostMultiplier)
@@ -103,8 +110,8 @@ export class Building {
103110
this.upgradeCostMultiplier = 1.15;
104111

105112
//* 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
106-
if (Math.floor(this.upgradeCost * this.upgradeCostMultiplier) === this.upgradeCost) {
107-
console.warn(`${this.name} upgrade cost is too low to increase after buy. Increase BuildingData.upgradeCost or BuildingData.upgradeCostMultiplier.`)
113+
if (Math.floor(this.baseUpgradeCost * this.upgradeCostMultiplier) === this.baseUpgradeCost) {
114+
console.warn(`${this.name} base upgrade cost is too low to increase after buy. Increase BuildingData.upgradeCost or BuildingData.upgradeCostMultiplier.`)
108115
}
109116

110117
this.bought = 0;
@@ -114,8 +121,6 @@ export class Building {
114121
buy() {
115122
if (this._clickercookie.cookies >= this.upgradeCost) {
116123
this._clickercookie.cookies -= this.upgradeCost;
117-
this.upgradeCost *= this.upgradeCostMultiplier;
118-
this.upgradeCost = Math.floor(this.upgradeCost);
119124
this.bought++;
120125
this.CPSGiven += this.CPSGain;
121126
this.hovered();

src/ts/clickercookie.ts

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,30 @@ interface ClickerCookieSaveData {
3535
// keyboard
3636
keyboardsBought: number;
3737
keyboardCPSGain: number;
38-
keyboardUpgradeCost: number;
3938
keyboardCPSGiven: number;
4039
// grandpa
4140
grandpasBought: number;
4241
grandpaCPSGain: number;
43-
grandpaUpgradeCost: number;
4442
grandpaCPSGiven: number;
4543
// ranch
4644
ranchesBought: number;
4745
ranchCPSGain: number;
48-
ranchUpgradeCost: number;
4946
ranchCPSGiven: number;
5047
// television
5148
televisionsBought: number;
5249
televisionCPSGain: number;
53-
televisionUpgradeCost: number;
5450
televisionCPSGiven: number;
5551
// worker
5652
workersBought: number;
5753
workerCPSGain: number;
58-
workerUpgradeCost: number;
5954
workerCPSGiven: number;
6055
// wallet
6156
walletsBought: number;
6257
walletCPSGain: number;
63-
walletUpgradeCost: number;
6458
walletCPSGiven: number;
6559
// church
6660
churchesBought: number;
6761
churchCPSGain: number;
68-
churchUpgradeCost: number;
6962
churchCPSGiven: number;
7063

7164
/* upgrades */
@@ -131,56 +124,56 @@ export default class ClickerCookie extends Mod {
131124
// buildings and stuff
132125
this.BUILDINGS_DATA = {
133126
keyboard: {
134-
name: "keyboard",
135-
namePlural: "keyboards",
127+
name: "Keyboard",
128+
namePlural: "Keyboards",
136129
quote: "type in cookies",
137130
upgradeCost: 15,
138131
CPSGain: 0.1,
139132
img: "img/keyboard.png"
140133
},
141134
grandpa: {
142-
name: "grandpa",
143-
namePlural: "grandpas",
135+
name: "Grandpa",
136+
namePlural: "Grandpas",
144137
quote: "as long as gramps gets a cut",
145138
upgradeCost: 100,
146139
CPSGain: 1,
147140
img: "img/grandpa.png"
148141
},
149142
ranch: {
150-
name: "ranch",
151-
namePlural: "ranches",
143+
name: "Ranch",
144+
namePlural: "Ranches",
152145
quote: "not the dressing kind",
153146
upgradeCost: 1_100,
154147
CPSGain: 8,
155148
img: "img/ranch.png"
156149
},
157150
television: {
158-
name: "television",
159-
namePlural: "televisions",
151+
name: "Television",
152+
namePlural: "Televisions",
160153
quote: "hold infomercials on your cookies",
161154
upgradeCost: 12_000,
162155
CPSGain: 47,
163156
img: "img/tv.png"
164157
},
165158
worker: {
166-
name: "worker",
167-
namePlural: "workers",
159+
name: "Worker",
160+
namePlural: "Workers",
168161
quote: "cookies via manual labor",
169162
upgradeCost: 130_000,
170163
CPSGain: 260,
171164
img: "img/worker.png"
172165
},
173166
wallet: {
174-
name: "wallet",
175-
namePlural: "wallets",
167+
name: "Wallet",
168+
namePlural: "Wallets",
176169
quote: "more storage space for your vast amount of cookie income",
177170
upgradeCost: 1_400_000,
178171
CPSGain: 1_440,
179172
img: "img/wallet.png"
180173
},
181174
church: {
182-
name: "church",
183-
namePlural: "churches",
175+
name: "Church",
176+
namePlural: "Churches",
184177
quote: "pray to the almighty cookie gods",
185178
upgradeCost: 20_000_000,
186179
CPSGain: 7_800,
@@ -203,14 +196,6 @@ export default class ClickerCookie extends Mod {
203196
this.church = new Building(this, this.BUILDINGS_DATA.church);
204197
this.church.setVisibility(false);
205198

206-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "keyboard"), this.keyboard);
207-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "grandpa"), this.grandpa);
208-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "ranch"), this.ranch);
209-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "television"), this.television);
210-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "worker"), this.worker);
211-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "wallet"), this.wallet);
212-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "church"), this.church);
213-
214199
// upgrades
215200
this.UPGRADES_DATA = [
216201
// keyboard
@@ -577,11 +562,6 @@ export default class ClickerCookie extends Mod {
577562
}
578563
];
579564

580-
for (const upgradeData of this.UPGRADES_DATA) {
581-
Handlers.UPGRADE.register(new Identifier(this.NAMESPACE, upgradeData.uid), new Upgrade(this, upgradeData));
582-
}
583-
584-
Mod.registerKooh("init", () => { this.init() });
585565
Mod.registerKooh("click", () => { this.cookieClicked() });
586566
Mod.registerKooh("loop", () => { this.gameLoop() });
587567
Mod.registerKooh("cps", () => { this.cpsUpdate() });
@@ -593,11 +573,23 @@ export default class ClickerCookie extends Mod {
593573
Handlers.UPGRADE.getFromIdentifier(new Identifier(this.NAMESPACE, "keyboard5")).desc = `Multiplys Keyboard and clicking ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().name.toLowerCase()} production by 2`;
594574

595575
Handlers.BUILDING.getFromIdentifier(new Identifier(this.NAMESPACE, "keyboard")).quote = `type in ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural.toLowerCase()}`;
596-
Handlers.BUILDING.getFromIdentifier(new Identifier(this.NAMESPACE, "television")).quote = `hold infomercials on your ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().name.toLowerCase()}`;
576+
Handlers.BUILDING.getFromIdentifier(new Identifier(this.NAMESPACE, "television")).quote = `hold infomercials on your ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural.toLowerCase()}`;
597577
});
598578
}
599579

600580
init() {
581+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "keyboard"), this.keyboard);
582+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "grandpa"), this.grandpa);
583+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "ranch"), this.ranch);
584+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "television"), this.television);
585+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "worker"), this.worker);
586+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "wallet"), this.wallet);
587+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "church"), this.church);
588+
589+
for (const upgradeData of this.UPGRADES_DATA) {
590+
Handlers.UPGRADE.register(new Identifier(this.NAMESPACE, upgradeData.uid), new Upgrade(this, upgradeData));
591+
}
592+
601593
// Register personalization things (must be before save load because loading requires these to be registered to set them)
602594
Handlers.CURRENTLY_CLICKED.register(new Identifier(this.NAMESPACE, "cookie"), {name: "Cookie", namePlural: "Cookies", src: "img/cookie.png"});
603595
Handlers.CURRENTLY_CLICKED.register(new Identifier(this.NAMESPACE, "potato"), {name: "Potato", namePlural: "Potatoes", src: "img/potato.png"});
@@ -695,37 +687,30 @@ export default class ClickerCookie extends Mod {
695687
// keyboard
696688
keyboardsBought: this.keyboard.bought,
697689
keyboardCPSGain: this.keyboard.CPSGain,
698-
keyboardUpgradeCost: this.keyboard.upgradeCost,
699690
keyboardCPSGiven: this.keyboard.CPSGiven,
700691
// grandpa
701692
grandpasBought: this.grandpa.bought,
702693
grandpaCPSGain: this.grandpa.CPSGain,
703-
grandpaUpgradeCost: this.grandpa.upgradeCost,
704694
grandpaCPSGiven: this.grandpa.CPSGiven,
705695
// ranch
706696
ranchesBought: this.ranch.bought,
707697
ranchCPSGain: this.ranch.CPSGain,
708-
ranchUpgradeCost: this.ranch.upgradeCost,
709698
ranchCPSGiven: this.ranch.CPSGiven,
710699
// television
711700
televisionsBought: this.television.bought,
712701
televisionCPSGain: this.television.CPSGain,
713-
televisionUpgradeCost: this.television.upgradeCost,
714702
televisionCPSGiven: this.television.CPSGiven,
715703
// worker
716704
workersBought: this.worker.bought,
717705
workerCPSGain: this.worker.CPSGain,
718-
workerUpgradeCost: this.worker.upgradeCost,
719706
workerCPSGiven: this.worker.CPSGiven,
720707
// wallet
721708
walletsBought: this.wallet.bought,
722709
walletCPSGain: this.wallet.CPSGain,
723-
walletUpgradeCost: this.wallet.upgradeCost,
724710
walletCPSGiven: this.wallet.CPSGiven,
725711
// church
726712
churchesBought: this.church.bought,
727713
churchCPSGain: this.church.CPSGain,
728-
churchUpgradeCost: this.church.upgradeCost,
729714
churchCPSGiven: this.church.CPSGiven,
730715

731716
/* upgrades */
@@ -756,37 +741,30 @@ export default class ClickerCookie extends Mod {
756741
// keyboard
757742
this.keyboard.bought = saveData.keyboardsBought;
758743
this.keyboard.CPSGain = saveData.keyboardCPSGain;
759-
this.keyboard.upgradeCost = saveData.keyboardUpgradeCost;
760744
this.keyboard.CPSGiven = saveData.keyboardCPSGiven;
761745
// grandpa
762746
this.grandpa.bought = saveData.grandpasBought;
763747
this.grandpa.CPSGain = saveData.grandpaCPSGain;
764-
this.grandpa.upgradeCost = saveData.grandpaUpgradeCost;
765748
this.grandpa.CPSGiven = saveData.grandpaCPSGiven;
766749
// ranch
767750
this.ranch.bought = saveData.ranchesBought;
768751
this.ranch.CPSGain = saveData.ranchCPSGain;
769-
this.ranch.upgradeCost = saveData.ranchUpgradeCost;
770752
this.ranch.CPSGiven = saveData.ranchCPSGiven;
771753
// television
772754
this.television.bought = saveData.televisionsBought;
773755
this.television.CPSGain = saveData.televisionCPSGain;
774-
this.television.upgradeCost = saveData.televisionUpgradeCost;
775756
this.television.CPSGiven = saveData.televisionCPSGiven;
776757
// worker
777758
this.worker.bought = saveData.workersBought;
778759
this.worker.CPSGain = saveData.workerCPSGain;
779-
this.worker.upgradeCost = saveData.workerUpgradeCost;
780760
this.worker.CPSGiven = saveData.workerCPSGiven;
781761
// wallet
782762
this.wallet.bought = saveData.walletsBought;
783763
this.wallet.CPSGain = saveData.walletCPSGain;
784-
this.wallet.upgradeCost = saveData.walletUpgradeCost;
785764
this.wallet.CPSGiven = saveData.walletCPSGiven;
786765
// church
787766
this.church.bought = saveData.churchesBought;
788767
this.church.CPSGain = saveData.churchCPSGain;
789-
this.church.upgradeCost = saveData.churchUpgradeCost;
790768
this.church.CPSGiven = saveData.churchCPSGiven;
791769
// ...
792770

src/ts/exmod.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//* This is an example of a mod that supports saving.
22

3-
import { Building } from "./buildings.js";
3+
import { Building, BuildingData } from "./buildings.js";
44
import { Identifier } from "./handler.js";
55
import { Game } from "./main.js";
66
import { Mod } from "./mods.js";
@@ -30,7 +30,6 @@ export class NewMod extends Mod {
3030
CPSGain: 5,
3131
img: "https://www.minecraft.net/content/dam/minecraftnet/franchise/logos/minecraft-creeper-face.jpg" // lol
3232
});
33-
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "banana"), this.banana);
3433

3534
this.UPGRADES_DATA = [
3635
{
@@ -44,17 +43,22 @@ export class NewMod extends Mod {
4443
img: "https://cdn.modrinth.com/data/AANobbMI/295862f4724dc3f78df3447ad6072b2dcd3ef0c9_96.webp" // lol
4544
}
4645
];
47-
for (const upgradeData of this.UPGRADES_DATA) {
48-
Handlers.UPGRADE.register(new Identifier(this.NAMESPACE, upgradeData.uid), new Upgrade(Game.getInstance().clickercookie, upgradeData));
49-
}
50-
46+
5147
document.getElementById("cookieCount")!.addEventListener("click", () => {this.superCookies++});
5248

5349
Mod.registerKooh("click", () => {
5450
console.log("Hello from "+this.NAMESPACE+"!");
5551
});
5652
}
5753

54+
init() {
55+
Handlers.BUILDING.register(new Identifier(this.NAMESPACE, "banana"), this.banana);
56+
57+
for (const upgradeData of this.UPGRADES_DATA) {
58+
Handlers.UPGRADE.register(new Identifier(this.NAMESPACE, upgradeData.uid), new Upgrade(Game.getInstance().clickercookie, upgradeData));
59+
}
60+
}
61+
5862
getSaveData(): ModSave {
5963
return {
6064
superCookies: this.superCookies

src/ts/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class BackgroundHandler extends Handler<Background> {
1717
/**
1818
* {@link Handler.register} with modifications to add a new {@link HTMLSelectElement} to the `backgroundSelect`.
1919
*
20-
* @param uid UID for the handler. **Will be {@link HTMLSelectElement.value} for `backgroundSelect`**.
20+
* @param identifier Identifier for the handler. **Stringified version will be {@link HTMLSelectElement.value} for `backgroundSelect`**.
2121
*/
2222
override register(identifier: Identifier, background: Background) {
2323
super.register(identifier, background);
@@ -329,6 +329,7 @@ export class ModHandler extends UniqueKeyHandler<Mod> {
329329
super.register(key, mod);
330330
this.saveHandler.register(mod.NAMESPACE, mod);
331331
document.getElementById("modsNumberLoaded")!.innerText = (Handlers.MOD.length - 1).toString(); //* subtract one so we don't show clickercookie (makes more sense to the user)
332+
mod.init();
332333
}
333334
}
334335

0 commit comments

Comments
 (0)