Slash-The-Hordes/assets/Scripts/Game/UI/GameUI.ts

57 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-12-13 17:21:04 +00:00
import { Component, Label, ProgressBar, _decorator } from "cc";
2022-12-21 13:08:49 +00:00
import { UIButton } from "../../Services/UI/Button/UIButton";
2023-01-12 14:50:21 +00:00
import { GameResult } from "../Game";
import { ItemManager } from "../Items/ItemManager";
import { ItemType } from "../Items/ItemType";
2022-12-21 13:08:49 +00:00
import { GameModalLauncher } from "../ModalWIndows/GameModalLauncher";
2022-11-28 10:49:16 +00:00
import { Player } from "../Unit/Player/Player";
2022-11-28 11:19:04 +00:00
import { UnitLevel } from "../Unit/UnitLevel";
2022-11-16 13:04:23 +00:00
const { ccclass, property } = _decorator;
@ccclass("GameUI")
export class GameUI extends Component {
@property(ProgressBar) private xpBar: ProgressBar;
2022-12-13 17:21:04 +00:00
@property(Label) private timeAliveText: Label;
2023-01-12 14:50:21 +00:00
@property(Label) private goldLabel: Label;
2022-12-21 13:08:49 +00:00
@property(UIButton) private pauseBtn: UIButton;
2022-11-16 13:04:23 +00:00
private playerLevel: UnitLevel;
2022-12-21 13:08:49 +00:00
private modalLauncher: GameModalLauncher;
2023-01-12 14:50:21 +00:00
private gameResult: GameResult;
2022-11-16 13:04:23 +00:00
2023-01-12 14:50:21 +00:00
public init(player: Player, modalLauncher: GameModalLauncher, itemManager: ItemManager, gameResult: GameResult): void {
2022-11-16 13:04:23 +00:00
this.playerLevel = player.Level;
2022-12-21 13:08:49 +00:00
this.modalLauncher = modalLauncher;
2023-01-12 14:50:21 +00:00
this.gameResult = gameResult;
2022-12-21 13:08:49 +00:00
2022-11-16 13:04:23 +00:00
this.playerLevel.XpAddedEvent.on(this.updateProgressBar, this);
this.playerLevel.LevelUpEvent.on(this.updateProgressBar, this);
2023-01-12 14:50:21 +00:00
itemManager.PickupEvent.on(this.tryUpdateGoldLabel, this);
2022-11-16 13:04:23 +00:00
this.xpBar.progress = 0;
2022-12-21 13:08:49 +00:00
this.pauseBtn.InteractedEvent.on(this.showPauseWindow, this);
2022-11-16 13:04:23 +00:00
}
private updateProgressBar(): void {
this.xpBar.progress = this.playerLevel.XP / this.playerLevel.RequiredXP;
}
2022-12-13 17:21:04 +00:00
2023-01-12 14:50:21 +00:00
private tryUpdateGoldLabel(itemType: ItemType): void {
if (itemType !== ItemType.Gold) return;
this.goldLabel.string = this.gameResult.goldCoins.toString();
}
2022-12-21 13:08:49 +00:00
private showPauseWindow(): void {
console.log("Show pause window");
this.modalLauncher.showPauseModal();
}
2022-12-13 17:21:04 +00:00
public updateTimeAlive(timeAlive: number): void {
this.timeAliveText.string = `${Math.floor(timeAlive)}`;
}
2022-11-16 13:04:23 +00:00
}