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

141 lines
4.9 KiB
TypeScript

import { _decorator, Color, Component, Label, LabelOutline, 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';
import { StickerManager } from '../Manager/StickerManager';
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 _eventLabel: Label;
@property({ type: ParticleSystem, visible: true })
private _buffFx: ParticleSystem;
@property({ type: Node, visible: true })
private _controlPanel: 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);
this._eventLabel.string = '';
}
private async onScore(score: number, points: number, type: ScoreType) {
this._scoreLabel.string = score.toString();
if (type == ScoreType.Goal) {
StickerManager.instance.Show('Goal');
}
}
private onBallOut() {
StickerManager.instance.Show('BallOut');
}
private showEventLabel(string: string, color?: Color, outLineColor?: Color) {
this._eventLabel.string = string;
this._eventLabel.color = color || new Color('#FFFF00');
this._eventLabel.getComponent(LabelOutline).color = outLineColor || new Color('#FF6600');
Tween.stopAllByTarget(this._eventLabel.node);
tween(this._eventLabel.node)
.set({ scale: Vec3.ZERO })
.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';
break;
case GameState.Ready:
this._controlPanel.active = true;
this._startPanel.active = false;
break;
case GameState.Playing:
this._overPanel.active = false;
this._ticketLabel.string = BEConnector.instance.numberTicket.toString();
break;
case GameState.GameOver:
this.showEventLabel('TIME UP!!!', new Color('#ed3a18'), Color.WHITE);
await Utilities.delay(1.2);
this._buffFx.stop();
this._overPanel.active = true;
break;
case GameState.End:
this.showEventLabel('TIME UP!!!', new Color('#ed3a18'), Color.WHITE);
await Utilities.delay(1.2);
this._overPanel.active = true;
break;
case GameState.Relive:
this._overPanel.active = false;
this._ticketLabel.string = BEConnector.instance.numberTicket.toString();
break;
}
}
private onMultiBall(active: boolean) {
if (active) {
this.showEventLabel('MULTI BALL!!!');
}
}
public onBoosterActive() {
this._buffFx.play();
this.showEventLabel('CHEESE!!!', new Color('#ffb517'), new Color('#ec830a'));
}
public onBoosterDisable() {
this._buffFx.stop();
}
public starGame() {
GameManager.instance.Ready();
}
// 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;
// }
}