2024-02-28 03:25:11 -08:00
|
|
|
import { _decorator, Button, Component, director, Label, Node } from 'cc';
|
2024-02-27 18:19:33 -08:00
|
|
|
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);
|
2024-02-28 03:25:11 -08:00
|
|
|
GameplayController.Instance().eventUpdateHighestStreak.on(
|
|
|
|
EventType.UpdateHighestStreak,
|
|
|
|
this.UpdateHighestStreak,
|
|
|
|
this,
|
|
|
|
);
|
2024-02-27 18:19:33 -08:00
|
|
|
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 {
|
2024-02-28 03:25:11 -08:00
|
|
|
console.log('Score: ' + score);
|
2024-02-27 18:19:33 -08:00
|
|
|
this.score = score;
|
2024-02-28 03:25:11 -08:00
|
|
|
this.goalTxt.string = 'Goal: ' + score.toString();
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
private UpdateStreak(streak: number): void {
|
2024-02-28 03:25:11 -08:00
|
|
|
console.log('Streak: ' + streak);
|
|
|
|
this.currentStreakTxt.string = 'Streak: ' + streak.toString();
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
private UpdateHighestStreak(streak: number): void {
|
2024-02-28 03:25:11 -08:00
|
|
|
console.log('Highest Streak: ' + streak);
|
2024-02-27 18:19:33 -08:00
|
|
|
this.hStreak = streak;
|
2024-02-28 03:25:11 -08:00
|
|
|
this.highestStreakTxt.string = 'Highest Streak: ' + streak.toString();
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
private UpdateBall(ball: number): void {
|
2024-02-28 03:25:11 -08:00
|
|
|
console.log('Ball: ' + ball);
|
|
|
|
this.ballTxt.string = 'Ball: ' + ball.toString();
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
private EndGameEvent(): void {
|
|
|
|
this.inGamePnl.active = false;
|
|
|
|
this.endGamePnl.active = true;
|
2024-02-28 03:25:11 -08:00
|
|
|
if (this.score + this.hStreak * 2 > 0) this.yourScoreTxt.string = (this.score + this.hStreak * 2).toString();
|
|
|
|
else this.yourScoreTxt.string = '0';
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
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 {
|
2024-02-28 03:25:11 -08:00
|
|
|
director.loadScene(director.getScene().name);
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
2024-02-28 03:25:11 -08:00
|
|
|
}
|