85 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-01-12 15:30:44 +01:00
import { approx, Canvas, Component, Label, Node, _decorator } from "cc";
2023-01-02 10:56:41 +01:00
import { AppRoot } from "../AppRoot/AppRoot";
import { requireAppRootAsync } from "../AppRoot/AppRootUtils";
2023-01-12 15:30:44 +01:00
import { MetaUpgradeSettings } from "../Game/Data/GameSettings";
import { MetaUpgradesData } from "../Game/Data/UserData";
2022-12-14 08:46:34 +01:00
import { UIButton } from "../Services/UI/Button/UIButton";
2022-12-07 10:47:46 +01:00
import { GameRunner } from "./GameRunner";
2022-12-16 11:49:52 +01:00
import { MenuModalLauncher } from "./ModalWindows/MenuModalLauncher";
2022-12-07 10:47:46 +01:00
const { ccclass, property } = _decorator;
@ccclass("Menu")
export class Menu extends Component {
2022-12-14 08:46:34 +01:00
@property(UIButton) private playBtn: UIButton;
2022-12-15 11:14:35 +01:00
@property(UIButton) private upgradeBtn: UIButton;
2023-01-12 15:30:44 +01:00
@property(Node) private upgradeAvailableIndicator: Node;
2023-01-12 15:59:35 +01:00
@property(Node) private goldCounter: Node;
@property(Label) private goldLabel: Label;
2022-12-21 14:08:49 +01:00
@property(UIButton) private audioSettingsBtn: UIButton;
2023-01-02 10:56:41 +01:00
@property(Canvas) private menuCanvas: Canvas;
2023-01-02 13:24:06 +01:00
@property(Label) private highscoreLabel: Label;
2022-12-16 11:49:52 +01:00
private menuModalLauncher: MenuModalLauncher;
2022-12-14 08:46:34 +01:00
2022-12-12 09:15:27 +01:00
public async start(): Promise<void> {
2023-01-02 10:56:41 +01:00
requireAppRootAsync();
this.menuCanvas.cameraComponent = AppRoot.Instance.MainCamera;
2022-12-15 11:14:35 +01:00
this.playBtn.InteractedEvent.on(this.startGame, this);
this.upgradeBtn.InteractedEvent.on(this.openUpgradesWindow, this);
2022-12-21 14:08:49 +01:00
this.audioSettingsBtn.InteractedEvent.on(this.openAudioSettingsWindow, this);
2022-12-16 11:49:52 +01:00
2023-01-02 10:56:41 +01:00
this.menuModalLauncher = new MenuModalLauncher(AppRoot.Instance.ModalWindowManager);
2023-01-02 13:24:06 +01:00
this.highscoreLabel.string = `Highscore: ${Math.floor(AppRoot.Instance.LiveUserData.game.highscore)}`;
2023-01-12 15:30:44 +01:00
2023-01-12 15:59:35 +01:00
this.updateGoldIndicators();
2023-01-12 15:30:44 +01:00
}
2023-01-12 15:59:35 +01:00
private updateGoldIndicators(): void {
2023-01-12 15:30:44 +01:00
this.upgradeAvailableIndicator.active = this.isUpgradeAffordable();
2023-01-12 15:59:35 +01:00
const goldCoins = AppRoot.Instance.LiveUserData.game.goldCoins;
this.goldCounter.active = 0 < goldCoins;
this.goldLabel.string = goldCoins.toString();
2023-01-12 15:30:44 +01:00
}
private isUpgradeAffordable(): boolean {
const goldCoins: number = AppRoot.Instance.LiveUserData.game.goldCoins;
const metaUpgrades: MetaUpgradesData = AppRoot.Instance.LiveUserData.game.metaUpgrades;
const metaUpgradesSettings = AppRoot.Instance.Settings.metaUpgrades;
const costs: number[] = [];
this.tryPushLowestCost(metaUpgrades.goldGathererLevel, metaUpgradesSettings.goldGatherer, costs);
this.tryPushLowestCost(metaUpgrades.healthLevel, metaUpgradesSettings.health, costs);
this.tryPushLowestCost(metaUpgrades.movementSpeedLevel, metaUpgradesSettings.movementSpeed, costs);
this.tryPushLowestCost(metaUpgrades.overallDamageLevel, metaUpgradesSettings.overallDamage, costs);
this.tryPushLowestCost(metaUpgrades.projectilePiercingLevel, metaUpgradesSettings.projectilePiercing, costs);
this.tryPushLowestCost(metaUpgrades.xpGathererLevel, metaUpgradesSettings.xpGatherer, costs);
return 0 < costs.length ? Math.min(...costs) <= goldCoins : false;
}
private tryPushLowestCost(upgradeLevel: number, metaUpgradeSettings: MetaUpgradeSettings, costs: number[]): void {
if (upgradeLevel < metaUpgradeSettings.costs.length) {
costs.push(metaUpgradeSettings.costs[upgradeLevel]);
}
2022-12-07 10:47:46 +01:00
}
2022-12-15 11:14:35 +01:00
private startGame(): void {
2023-01-02 10:56:41 +01:00
AppRoot.Instance.ScreenFader.playOpen();
2022-12-15 11:14:35 +01:00
GameRunner.Instance.playGame();
}
2023-01-12 15:30:44 +01:00
private async openUpgradesWindow(): Promise<void> {
await this.menuModalLauncher.openUpgradesWindow();
2023-01-12 15:59:35 +01:00
this.updateGoldIndicators();
2022-12-16 11:49:52 +01:00
}
2022-12-21 14:08:49 +01:00
private openAudioSettingsWindow(): void {
this.menuModalLauncher.openAudioSettingsWindow();
}
2022-12-07 10:47:46 +01:00
}