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

129 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-06-12 04:48:19 -07:00
import { _decorator, Button, Color, Component, Label, Node, ParticleSystem, Vec3 } from 'cc';
2024-06-09 05:12:08 -07:00
import BoosterType from '../Enum/BoosterType';
2024-03-10 20:46:50 -07:00
import GameState from '../Enum/GameState';
2024-06-09 05:12:08 -07:00
import ScoreType from '../Enum/ScoreType';
import GameEvent from '../Events/GameEvent';
import { EventManger } from '../Manager/EventManger';
2024-03-26 00:28:59 -07:00
import { GameManager } from '../Manager/GameManager';
import { StickerManager } from '../Manager/StickerManager';
2024-06-12 04:48:19 -07:00
import P4PSDK from '../P4PSDK';
2024-06-09 05:12:08 -07:00
import Utils from '../Utilities';
2024-03-07 03:15:08 -08:00
const { ccclass, property } = _decorator;
@ccclass('UIController')
export class UIController extends Component {
2024-03-10 20:46:50 -07:00
@property({ type: Label, visible: true })
private _scoreLabel: Label;
@property({ type: Label, visible: true })
2024-03-29 04:24:58 -07:00
private _ticketLabel: Label;
2024-03-27 04:00:23 -07:00
2024-03-11 04:12:22 -07:00
@property({ type: ParticleSystem, visible: true })
private _buffFx: ParticleSystem;
2024-03-27 04:00:23 -07:00
@property({ type: Node, visible: true })
2024-04-04 04:27:04 -07:00
private _controlPanel: Node;
2024-03-10 20:46:50 -07:00
@property({ type: Node, visible: true })
private _startPanel: Node;
@property({ type: Node, visible: true })
private _overPanel: Node;
2024-06-12 04:48:19 -07:00
@property(Node)
private loadingScreen: Node;
@property(Button)
private playButton: Button;
2024-03-07 03:15:08 -08:00
2024-03-22 03:27:52 -07:00
protected async onLoad() {
2024-03-10 20:46:50 -07:00
EventManger.instance.on(GameEvent.Score, this.onScore, this);
EventManger.instance.on(GameEvent.BallOut, this.onBallOut, this);
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
2024-03-11 04:12:22 -07:00
EventManger.instance.on(GameEvent.MultiBall, this.onMultiBall, this);
2024-03-28 20:35:44 -07:00
EventManger.instance.on(GameEvent.BoosterActive, this.onBoosterActive, this);
EventManger.instance.on(GameEvent.BoosterDisable, this.onBoosterDisable, this);
2024-04-26 00:48:20 -07:00
EventManger.instance.on(GameEvent.TicketUpdate, this.onTicketUpdate, this);
2024-05-07 03:20:37 -07:00
this._buffFx.setNodeActive(false);
2024-06-12 04:48:19 -07:00
this._startPanel.active = true;
this.playButton.interactable = false;
this.loadingScreen.active = true;
2024-03-07 03:15:08 -08:00
}
2024-03-28 20:35:44 -07:00
private async onScore(score: number, points: number, type: ScoreType) {
2024-03-29 04:24:58 -07:00
this._scoreLabel.string = score.toString();
2024-03-10 20:46:50 -07:00
if (type == ScoreType.Goal) {
2024-04-23 03:36:31 -07:00
StickerManager.instance.Show('Goal', new Vec3(0, 700));
2024-03-10 20:46:50 -07:00
}
}
2024-03-22 03:27:52 -07:00
private onBallOut() {
StickerManager.instance.Show('BallOut');
2024-03-22 03:27:52 -07:00
}
2024-04-26 00:48:20 -07:00
private onTicketUpdate(ticket: number) {
this._ticketLabel.string = ticket.toString();
}
2024-03-29 04:24:58 -07:00
private async onGameStateChange(state: GameState) {
2024-03-10 20:46:50 -07:00
switch (state) {
case GameState.Init:
2024-06-12 04:48:19 -07:00
this.playButton.interactable = true;
this.loadingScreen.active = false;
this._ticketLabel.string = P4PSDK.getUserTicket().toString();
2024-03-29 04:24:58 -07:00
this._scoreLabel.string = '0';
2024-03-10 20:46:50 -07:00
break;
2024-04-03 02:43:18 -07:00
case GameState.Ready:
2024-04-04 04:27:04 -07:00
this._controlPanel.active = true;
2024-04-03 02:43:18 -07:00
this._startPanel.active = false;
break;
2024-03-10 20:46:50 -07:00
case GameState.Playing:
this._overPanel.active = false;
2024-04-03 02:43:18 -07:00
2024-03-10 20:46:50 -07:00
break;
case GameState.GameOver:
2024-05-07 03:20:37 -07:00
this._buffFx.setNodeActive(false);
2024-06-09 05:12:08 -07:00
await Utils.waitUntil(() => !GameManager.instance.isWaitingUpdateScore);
await Utils.delay(2);
2024-03-10 20:46:50 -07:00
this._overPanel.active = true;
2024-03-26 00:28:59 -07:00
break;
case GameState.End:
2024-06-09 05:12:08 -07:00
await Utils.delay(2);
2024-03-29 04:24:58 -07:00
this._overPanel.active = true;
2024-03-26 00:28:59 -07:00
break;
case GameState.Relive:
this._overPanel.active = false;
2024-03-10 20:46:50 -07:00
break;
}
}
2024-03-11 04:12:22 -07:00
private onMultiBall(active: boolean) {
2024-03-22 03:27:52 -07:00
if (active) {
StickerManager.instance.showLabel('MULTI BALL!!!');
2024-03-22 03:27:52 -07:00
}
2024-03-11 04:12:22 -07:00
}
public onBoosterActive(type: BoosterType, displayName: string) {
2024-06-12 04:48:19 -07:00
this._buffFx.setNodeActive(type == BoosterType.CumulativeBar);
StickerManager.instance.showLabel(displayName + '!!!', {
color: new Color('#ffb517'),
outLineColor: new Color('#ec830a'),
});
2024-03-28 20:35:44 -07:00
}
2024-06-12 04:48:19 -07:00
public onBoosterDisable(type: BoosterType) {
if (type == BoosterType.CumulativeBar) this._buffFx.setNodeActive(false);
2024-03-28 20:35:44 -07:00
}
2024-03-26 20:04:28 -07:00
public starGame() {
2024-04-03 02:43:18 -07:00
GameManager.instance.Ready();
2024-03-26 20:04:28 -07:00
}
2024-04-21 19:04:58 -07:00
// 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');
2024-03-11 04:12:22 -07:00
2024-04-21 19:04:58 -07:00
// return m + ':' + s;
// }
2024-03-10 20:46:50 -07:00
}