pinball/assets/_Game/Scripts/UI/UIController.ts

105 lines
3.6 KiB
TypeScript

import { _decorator, Component, Label, Node, ParticleSystem, tween } from 'cc';
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import ScoreType from '../Enum/ScoreType';
import Utilities from '../Utilities';
import GameState from '../Enum/GameState';
import { GameManager } from '../Manager/GameManager';
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 _ballLabel: Label;
@property({ type: Label, visible: true })
private _goalLabel: Label;
@property({ type: Label, visible: true })
private _resultLabel: Label;
@property({ type: ParticleSystem, visible: true })
private _buffFx: ParticleSystem;
@property({ type: Node, visible: true })
private _startPanel: Node;
@property({ type: Node, visible: true })
private _overPanel: Node;
@property({ type: Label, visible: true })
private _timeLabe: Label;
@property({ type: Label, visible: true })
private _timeResult: Label;
private _timer = 0;
private _playing = false;
protected onLoad(): void {
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);
this._goalLabel.string = '';
}
protected update(dt: number): void {
if (this._playing) {
this._timer += dt;
this._timeLabe.string = this.secondsToTime(this._timer);
}
}
private async onScore(score: number, type: ScoreType) {
this._scoreLabel.string = `Score: ${score}`;
if (type == ScoreType.Goal) {
this._goalLabel.string = 'Goal!!';
await Utilities.delay(1000);
this._goalLabel.string = '';
}
}
private async onBallOut(balls: number) {
this._ballLabel.string = `Ball ${balls}`;
this._goalLabel.string = 'Ball Out!!';
await Utilities.delay(1000);
this._goalLabel.string = '';
}
private onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
this._startPanel.active = true;
break;
case GameState.Playing:
this._playing = true;
this._scoreLabel.string = 'Score: 0';
this._ballLabel.string = `Ball: ${GameManager.instance.balls}`;
this._startPanel.active = false;
this._overPanel.active = false;
break;
case GameState.GameOver:
this._playing = false;
this._overPanel.active = true;
this._resultLabel.string = this._scoreLabel.string;
this._timeResult.string = this._timeLabe.string;
this._timer = 0;
break;
}
}
private onMultiBall(active: boolean) {
if (active) this._buffFx.play();
else this._buffFx.stop();
}
private secondsToTime(second: number) {
const h = Math.floor(second / 3600)
.toString()
.padStart(2, '0'),
m = Math.floor((second % 3600) / 60)
.toString()
.padStart(2, '0'),
s = Math.floor(second % 60)
.toString()
.padStart(2, '0');
return h + ':' + m + ':' + s;
}
}