75 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-12-19 13:17:32 +01:00
import { instantiate, Label, Node, Prefab, TiledUserNodeData, _decorator } from "cc";
2022-12-16 13:52:54 +01:00
import { AppRoot } from "../../../AppRoot/AppRoot";
import { MetaUpgradeSettings } from "../../../Game/Data/GameSettings";
2022-12-19 13:17:32 +01:00
import { UserData, GameData, MetaUpgradesData } from "../../../Game/Data/UserData";
import { MetaUpgradeType, UpgradeType } from "../../../Game/Upgrades/UpgradeType";
2022-12-16 11:49:52 +01:00
import { ModalWindow } from "../../../Services/ModalWindowSystem/ModalWindow";
import { UpgradeUI } from "./UpgradeUI";
const { ccclass, property } = _decorator;
@ccclass("UpgradesModalWindow")
export class UpgradesModalWindow extends ModalWindow<Empty, Empty> {
2022-12-16 13:52:54 +01:00
@property(Prefab) private upgradeButtonPrefab: Prefab;
@property(Node) private upgradeButtonParent: Node;
@property(Label) private goldCoinsLabel: Label;
2022-12-19 13:17:32 +01:00
private typeToLevel = new Map<MetaUpgradeType, number>();
private typeToCosts = new Map<MetaUpgradeType, number[]>();
private typeToLevelKey = new Map<MetaUpgradeType, keyof MetaUpgradesData>();
private typeToUpgradeUI = new Map<MetaUpgradeType, UpgradeUI>();
2022-12-16 13:52:54 +01:00
2022-12-19 13:17:32 +01:00
private userData: UserData;
2022-12-16 13:52:54 +01:00
2022-12-19 13:17:32 +01:00
public setup(): void {
this.userData = AppRoot.Instance.SaveSystem.load();
2022-12-16 13:52:54 +01:00
const settings = AppRoot.Instance.Settings.metaUpgrades;
2022-12-19 13:17:32 +01:00
this.createUpgradeButton(MetaUpgradeType.Health, settings.health, "healthLevel");
this.createUpgradeButton(MetaUpgradeType.OverallDamage, settings.overallDamage, "overallDamageLevel");
this.createUpgradeButton(MetaUpgradeType.ProjectilePiercing, settings.projectilePiercing, "projectilePiercingLevel");
this.createUpgradeButton(MetaUpgradeType.MovementSpeed, settings.movementSpeed, "movementSpeedLevel");
this.createUpgradeButton(MetaUpgradeType.XPGatherer, settings.xpGatherer, "xpGathererLevel");
this.createUpgradeButton(MetaUpgradeType.GoldGatherer, settings.goldGatherer, "goldGathererLevel");
2022-12-16 13:52:54 +01:00
2022-12-19 13:17:32 +01:00
this.goldCoinsLabel.string = this.userData.game.goldCoins.toString();
2022-12-16 13:52:54 +01:00
}
2022-12-19 13:17:32 +01:00
private createUpgradeButton<T extends keyof MetaUpgradesData>(
upgradeType: MetaUpgradeType,
upgradeSettings: MetaUpgradeSettings,
levelKey: T
): void {
2022-12-16 13:52:54 +01:00
const upgradeButton: Node = instantiate(this.upgradeButtonPrefab);
2022-12-19 13:17:32 +01:00
const upgradeUI: UpgradeUI = upgradeButton.getComponent(UpgradeUI);
upgradeUI.init(upgradeType, upgradeSettings, this.userData.game.metaUpgrades[levelKey], AppRoot.Instance.TranslationData);
upgradeUI.InteractedEvent.on(this.tryUpgrade, this);
2022-12-16 13:52:54 +01:00
upgradeButton.setParent(this.upgradeButtonParent);
2022-12-19 13:17:32 +01:00
this.typeToLevel.set(upgradeType, this.userData.game.metaUpgrades[levelKey]);
this.typeToCosts.set(upgradeType, upgradeSettings.costs);
this.typeToLevelKey.set(upgradeType, levelKey);
this.typeToUpgradeUI.set(upgradeType, upgradeUI);
}
private tryUpgrade(upgradeType: MetaUpgradeType): void {
console.log("Trying to upgrade " + upgradeType);
const costs: number[] = this.typeToCosts.get(upgradeType);
const currentLevel: number = this.typeToLevel.get(upgradeType);
if (costs.length <= currentLevel) return; // already max level
if (this.userData.game.goldCoins < costs[currentLevel]) return; // not enough gold
this.userData.game.goldCoins -= costs[currentLevel];
const level = ++this.userData.game.metaUpgrades[this.typeToLevelKey.get(upgradeType)];
this.typeToUpgradeUI.get(upgradeType).updateLevel(level);
2022-12-19 15:50:24 +01:00
this.goldCoinsLabel.string = this.userData.game.goldCoins.toString();
2022-12-19 13:17:32 +01:00
AppRoot.Instance.SaveSystem.save(this.userData);
2022-12-16 11:49:52 +01:00
}
}
export class Empty {}