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

129 lines
4.5 KiB
TypeScript

import { _decorator, Button, Color, Component, Label, Node, ParticleSystem, Vec3 } from 'cc';
import BoosterType from '../Enum/BoosterType';
import GameState from '../Enum/GameState';
import ScoreType from '../Enum/ScoreType';
import GameEvent from '../Events/GameEvent';
import { EventManger } from '../Manager/EventManger';
import { GameManager } from '../Manager/GameManager';
import { StickerManager } from '../Manager/StickerManager';
import P4PSDK from '../P4PSDK';
import Utils 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: 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;
@property(Node)
private loadingScreen: Node;
@property(Button)
private playButton: Button;
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.TicketUpdate, this.onTicketUpdate, this);
this._buffFx.setNodeActive(false);
this._startPanel.active = true;
this.playButton.interactable = false;
this.loadingScreen.active = true;
}
private async onScore(score: number, points: number, type: ScoreType) {
this._scoreLabel.string = score.toString();
if (type == ScoreType.Goal) {
StickerManager.instance.Show('Goal', new Vec3(0, 700));
}
}
private onBallOut() {
StickerManager.instance.Show('BallOut');
}
private onTicketUpdate(ticket: number) {
this._ticketLabel.string = ticket.toString();
}
private async onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
this.playButton.interactable = true;
this.loadingScreen.active = false;
this._ticketLabel.string = P4PSDK.getUserTicket().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;
break;
case GameState.GameOver:
this._buffFx.setNodeActive(false);
await Utils.waitUntil(() => !GameManager.instance.isWaitingUpdateScore);
await Utils.delay(2);
this._overPanel.active = true;
break;
case GameState.End:
await Utils.delay(2);
this._overPanel.active = true;
break;
case GameState.Relive:
this._overPanel.active = false;
break;
}
}
private onMultiBall(active: boolean) {
if (active) {
StickerManager.instance.showLabel('MULTI BALL!!!');
}
}
public onBoosterActive(type: BoosterType, displayName: string) {
this._buffFx.setNodeActive(type == BoosterType.CumulativeBar);
StickerManager.instance.showLabel(displayName + '!!!', {
color: new Color('#ffb517'),
outLineColor: new Color('#ec830a'),
});
}
public onBoosterDisable(type: BoosterType) {
if (type == BoosterType.CumulativeBar) this._buffFx.setNodeActive(false);
}
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;
// }
}