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

143 lines
4.8 KiB
TypeScript

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';
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 _eventLabel: Label;
@property({ type: Label, visible: true })
private _resultLabel: Label;
@property({ type: Label, visible: true })
private _resultTotalLabel: 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;
@property({ type: Label, visible: true })
private _timeLabe: Label;
@property({ type: Node, visible: true })
private _resultPanel: Node = null;
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: ${score}`;
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 onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
this._startPanel.active = true;
break;
case GameState.Playing:
this._scoreLabel.string = 'Score: 0';
this._overPanel.active = false;
break;
case GameState.GameOver:
this._buffFx.stop();
this._overPanel.active = true;
this._resultLabel.string = this._scoreLabel.string;
break;
case GameState.End:
this._overPanel.active = false;
this._resultPanel.active = true;
this._resultLabel.string = GameManager.instance.score.toString();
this._resultTotalLabel.string = (
GameManager.instance.score + BEConnector.instance.currentScore
).toString();
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._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;
}
}