pinball/assets/Scripts/UIController.ts

130 lines
4.5 KiB
TypeScript

import { _decorator, Button, Component, director, Label, Node } from 'cc';
import { GameplayController, GameState } from './GameplayController';
import { EventType } from './Enums';
import { SoundManager } from './SoundManager';
const { ccclass, property } = _decorator;
@ccclass('UIController')
export class UIController extends Component {
@property(Node)
private mainMenuPnl: Node;
@property(Button)
private startGameBtn: Button;
@property(Node)
private countdownPnl: Node;
@property(Label)
private countdownTxt: Label;
@property(Node)
private inGamePnl: Node;
@property(Label)
private goalTxt: Label;
@property(Label)
private currentStreakTxt: Label;
@property(Label)
private highestStreakTxt: Label;
@property(Label)
private ballTxt: Label;
@property(Label)
public currentLevelTxt: Label;
@property(Node)
public endGamePnl: Node;
@property(Label)
private yourScoreTxt: Label;
@property(Button)
private replayBtn: Button;
private isGameStarted: boolean;
private countdownTime: number;
private score: number;
private hStreak: number;
protected start(): void {
this.countdownTime = GameplayController.Instance().startGameCountDown;
this.startGameBtn.node.on(Button.EventType.CLICK, this.StartGameClicked, this);
this.replayBtn.node.on(Button.EventType.CLICK, this.RestartClicked, this);
GameplayController.Instance().eventMenuGame.on(EventType.MainMenu, this.ShowGameMenu, this);
GameplayController.Instance().eventStartGame.on(EventType.StartGame, this.StartCountdownEvent, this);
GameplayController.Instance().eventPlayGame.on(EventType.PlayGame, this.StartGameEvent, this);
GameplayController.Instance().eventEndGame.on(EventType.EndGame, this.EndGameEvent, this);
GameplayController.Instance().eventUpdateScore.on(EventType.UpdateScore, this.UpdateScore, this);
GameplayController.Instance().eventUpdateStreak.on(EventType.UpdateStreak, this.UpdateStreak, this);
GameplayController.Instance().eventUpdateHighestStreak.on(
EventType.UpdateHighestStreak,
this.UpdateHighestStreak,
this,
);
GameplayController.Instance().eventUpdateBall.on(EventType.UpdateBall, this.UpdateBall, this);
}
protected update(dt: number): void {
if (!this.isGameStarted) return;
if (this.countdownTime < 0) {
if (GameplayController.Instance().currentGameState != GameState.PlayGame)
GameplayController.Instance().ChangeGameState(GameState.PlayGame);
return;
}
this.countdownTime -= dt;
this.countdownTxt.string = Math.round(this.countdownTime).toString();
}
//#region Event listeners
private ShowGameMenu(): void {
this.mainMenuPnl.active = true;
}
private StartCountdownEvent(): void {
this.mainMenuPnl.active = false;
this.countdownPnl.active = true;
this.isGameStarted = true;
}
private StartGameEvent(): void {
this.countdownPnl.active = false;
this.inGamePnl.active = true;
}
private UpdateScore(score: number): void {
console.log('Score: ' + score);
this.score = score;
this.goalTxt.string = 'Goal: ' + score.toString();
}
private UpdateStreak(streak: number): void {
console.log('Streak: ' + streak);
this.currentStreakTxt.string = 'Streak: ' + streak.toString();
}
private UpdateHighestStreak(streak: number): void {
console.log('Highest Streak: ' + streak);
this.hStreak = streak;
this.highestStreakTxt.string = 'Highest Streak: ' + streak.toString();
}
private UpdateBall(ball: number): void {
console.log('Ball: ' + ball);
this.ballTxt.string = 'Ball: ' + ball.toString();
}
private EndGameEvent(): void {
this.inGamePnl.active = false;
this.endGamePnl.active = true;
if (this.score + this.hStreak * 2 > 0) this.yourScoreTxt.string = (this.score + this.hStreak * 2).toString();
else this.yourScoreTxt.string = '0';
}
public ShutEndPnl(): void {
this.endGamePnl.active = false;
}
//#endregion
private StartGameClicked(): void {
GameplayController.Instance().ChangeGameState(GameState.StartGame);
SoundManager.Instance().PlayOneShot(SoundManager.Instance().menuTap);
}
private RestartClicked(): void {
director.loadScene(director.getScene().name);
}
}