import { _decorator, Component, Label, Node, ParticleSystem, Tween, tween, Vec3 } from 'cc'; import { EventManger } from '../Manager/EventManger'; import GameEvent from '../Events/GameEvent'; import ScoreType from '../Enum/ScoreType'; import GameState from '../Enum/GameState'; import { GameManager } from '../Manager/GameManager'; import BEConnector from '../API/BEConnector'; import Utilities from '../Utilities'; const { ccclass, property } = _decorator; @ccclass('UIController') export class UIController extends Component { @property({ type: Label, visible: true }) private _scoreLabel: Label; @property({ type: Label, visible: true }) private _ticketLabel: Label; @property({ type: Label, visible: true }) private _timeLabel: Label; @property({ type: Label, visible: true }) private _eventLabel: Label; @property({ type: ParticleSystem, visible: true }) private _buffFx: ParticleSystem; @property({ type: Node, visible: true }) private _tutorialPanel: Node; @property({ type: Node, visible: true }) private _startPanel: Node; @property({ type: Node, visible: true }) private _overPanel: Node; protected async onLoad() { EventManger.instance.on(GameEvent.Score, this.onScore, this); EventManger.instance.on(GameEvent.BallOut, this.onBallOut, this); EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this); EventManger.instance.on(GameEvent.MultiBall, this.onMultiBall, this); EventManger.instance.on(GameEvent.BoosterActive, this.onBoosterActive, this); EventManger.instance.on(GameEvent.BoosterDisable, this.onBoosterDisable, this); EventManger.instance.on(GameEvent.TimeUpdate, this.onTimeUpdate, this); this._eventLabel.string = ''; } private async onScore(score: number, points: number, type: ScoreType) { this._scoreLabel.string = score.toString(); if (type == ScoreType.Goal) { this.showEventLabel('GOAL!!!'); } } private onBallOut() { this.showEventLabel('BALL OUT!!!'); } private showEventLabel(string: string) { this._eventLabel.string = string; Tween.stopAllByTarget(this._eventLabel.node); this._eventLabel.node.setScale(Vec3.ZERO); tween(this._eventLabel.node) .to(0.2, { scale: Vec3.ONE }, { easing: 'backOut' }) .delay(1) .to( 0.1, { scale: Vec3.ZERO }, { onComplete: () => { this._eventLabel.string = ''; }, easing: 'backIn', }, ) .start(); } private async onGameStateChange(state: GameState) { switch (state) { case GameState.Init: this._startPanel.active = true; this._ticketLabel.string = BEConnector.instance.numberTicket.toString(); this._scoreLabel.string = '0'; this._timeLabel.string = this.secondsToTime(GameManager.instance.gameTime); break; case GameState.Playing: this._overPanel.active = false; break; case GameState.GameOver: this.showEventLabel('TIME UP!!!'); await Utilities.delay(1.2); this._buffFx.stop(); this._overPanel.active = true; break; case GameState.End: this.showEventLabel('TIME UP!!!'); await Utilities.delay(1.2); this._overPanel.active = true; break; case GameState.Relive: this._overPanel.active = false; break; } } private onMultiBall(active: boolean) { if (active) { this.showEventLabel('MULTI BALL!!!'); } } public onBoosterActive() { this._buffFx.play(); this.showEventLabel('BOOSTER NAME!!!'); } public onBoosterDisable() { this._buffFx.stop(); } public starGame() { this._tutorialPanel.active = true; this._startPanel.active = false; } private onTimeUpdate(time: number) { this._timeLabel.string = this.secondsToTime(time); } private secondsToTime(second: number) { const m = Math.floor((second - (second % 60)) / 60) .toString() .padStart(2, '0'); const s = Math.floor(second % 60) .toString() .padStart(2, '0'); return m + ':' + s; } }