103 lines
3.5 KiB
TypeScript
103 lines
3.5 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';
|
|
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 _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);
|
|
EventManger.instance.on(GameEvent.TimeUpdate, this.onTimeUpdate, this);
|
|
this._goalLabel.string = '';
|
|
}
|
|
|
|
protected update(dt: number): void {
|
|
if (this._playing) this._timer += dt;
|
|
}
|
|
|
|
private async onScore(score: number, type: ScoreType) {
|
|
this._scoreLabel.string = `Score: ${score}`;
|
|
if (type == ScoreType.Goal) {
|
|
this._goalLabel.string = 'Goal!!';
|
|
await Utilities.delay(1);
|
|
this._goalLabel.string = '';
|
|
}
|
|
}
|
|
|
|
private async onBallOut() {
|
|
this._goalLabel.string = 'Ball Out!!';
|
|
await Utilities.delay(1);
|
|
this._goalLabel.string = '';
|
|
}
|
|
|
|
private onGameStateChange(state: GameState) {
|
|
switch (state) {
|
|
case GameState.Init:
|
|
this._startPanel.active = true;
|
|
break;
|
|
case GameState.Playing:
|
|
this._timer = 0;
|
|
this._playing = true;
|
|
this._scoreLabel.string = 'Score: 0';
|
|
this._startPanel.active = false;
|
|
this._overPanel.active = false;
|
|
break;
|
|
case GameState.GameOver:
|
|
this._buffFx.stop();
|
|
this._playing = false;
|
|
this._overPanel.active = true;
|
|
this._resultLabel.string = this._scoreLabel.string;
|
|
this._timeResult.string = this.secondsToTime(this._timer);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private onMultiBall(active: boolean) {
|
|
if (active) this._buffFx.play();
|
|
else this._buffFx.stop();
|
|
}
|
|
|
|
private onTimeUpdate(time: number) {
|
|
this._timeLabe.string = this.secondsToTime(time);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|