Slash-The-Hordes/assets/Scripts/Menu/Menu.ts

85 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-01-12 14:30:44 +00:00
import { approx, Canvas, Component, Label, Node, _decorator } from "cc";
2023-01-02 09:56:41 +00:00
import { AppRoot } from "../AppRoot/AppRoot";
import { requireAppRootAsync } from "../AppRoot/AppRootUtils";
2023-01-12 14:30:44 +00:00
import { MetaUpgradeSettings } from "../Game/Data/GameSettings";
import { MetaUpgradesData } from "../Game/Data/UserData";
2022-12-14 07:46:34 +00:00
import { UIButton } from "../Services/UI/Button/UIButton";
2022-12-07 09:47:46 +00:00
import { GameRunner } from "./GameRunner";
2022-12-16 10:49:52 +00:00
import { MenuModalLauncher } from "./ModalWindows/MenuModalLauncher";
2022-12-07 09:47:46 +00:00
const { ccclass, property } = _decorator;
@ccclass("Menu")
export class Menu extends Component {
2022-12-14 07:46:34 +00:00
@property(UIButton) private playBtn: UIButton;
2022-12-15 10:14:35 +00:00
@property(UIButton) private upgradeBtn: UIButton;
2023-01-12 14:30:44 +00:00
@property(Node) private upgradeAvailableIndicator: Node;
2023-01-12 14:59:35 +00:00
@property(Node) private goldCounter: Node;
@property(Label) private goldLabel: Label;
2022-12-21 13:08:49 +00:00
@property(UIButton) private audioSettingsBtn: UIButton;
2023-01-02 09:56:41 +00:00
@property(Canvas) private menuCanvas: Canvas;
2023-01-02 12:24:06 +00:00
@property(Label) private highscoreLabel: Label;
2022-12-16 10:49:52 +00:00
private menuModalLauncher: MenuModalLauncher;
2022-12-14 07:46:34 +00:00
2022-12-12 08:15:27 +00:00
public async start(): Promise<void> {
2023-01-02 09:56:41 +00:00
requireAppRootAsync();
this.menuCanvas.cameraComponent = AppRoot.Instance.MainCamera;
2022-12-15 10:14:35 +00:00
this.playBtn.InteractedEvent.on(this.startGame, this);
this.upgradeBtn.InteractedEvent.on(this.openUpgradesWindow, this);
2022-12-21 13:08:49 +00:00
this.audioSettingsBtn.InteractedEvent.on(this.openAudioSettingsWindow, this);
2022-12-16 10:49:52 +00:00
2023-01-02 09:56:41 +00:00
this.menuModalLauncher = new MenuModalLauncher(AppRoot.Instance.ModalWindowManager);
2023-01-02 12:24:06 +00:00
this.highscoreLabel.string = `Highscore: ${Math.floor(AppRoot.Instance.LiveUserData.game.highscore)}`;
2023-01-12 14:30:44 +00:00
2023-01-12 14:59:35 +00:00
this.updateGoldIndicators();
2023-01-12 14:30:44 +00:00
}
2023-01-12 14:59:35 +00:00
private updateGoldIndicators(): void {
2023-01-12 14:30:44 +00:00
this.upgradeAvailableIndicator.active = this.isUpgradeAffordable();
2023-01-12 14:59:35 +00:00
const goldCoins = AppRoot.Instance.LiveUserData.game.goldCoins;
this.goldCounter.active = 0 < goldCoins;
this.goldLabel.string = goldCoins.toString();
2023-01-12 14:30:44 +00: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 09:47:46 +00:00
}
2022-12-15 10:14:35 +00:00
private startGame(): void {
2023-01-02 09:56:41 +00:00
AppRoot.Instance.ScreenFader.playOpen();
2022-12-15 10:14:35 +00:00
GameRunner.Instance.playGame();
}
2023-01-12 14:30:44 +00:00
private async openUpgradesWindow(): Promise<void> {
await this.menuModalLauncher.openUpgradesWindow();
2023-01-12 14:59:35 +00:00
this.updateGoldIndicators();
2022-12-16 10:49:52 +00:00
}
2022-12-21 13:08:49 +00:00
private openAudioSettingsWindow(): void {
this.menuModalLauncher.openAudioSettingsWindow();
}
2022-12-07 09:47:46 +00:00
}