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

126 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-03-29 04:24:58 -07:00
import { _decorator, Component, Label, Node, Prefab, Tween, tween, Vec3 } from 'cc';
2024-03-26 00:28:59 -07:00
import BEConnector from '../API/BEConnector';
import { GameManager } from '../Manager/GameManager';
2024-03-29 04:24:58 -07:00
import ObjectPool from '../Pool/ObjectPool';
import Utilities from '../Utilities';
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import GameState from '../Enum/GameState';
2024-03-26 00:28:59 -07:00
const { ccclass, property } = _decorator;
@ccclass('GameOverPanel')
export class GameOverPanel extends Component {
@property(Label) private topScore: Label = null;
@property(Label) private yourScore: Label = null;
2024-03-29 04:24:58 -07:00
@property({ type: Label, visible: true })
private _ticketMinus: Label;
@property({ type: Node, visible: true })
private _buyTicketBtn: Node;
@property({ type: Node, visible: true })
private _quitBtn: Node;
@property({ type: Node, visible: true })
private _scoreUI: Node;
@property({ type: Prefab, visible: true })
private _scorePrefab: Prefab;
private _pool: ObjectPool;
private _active = false;
protected onLoad(): void {
this._pool = new ObjectPool(this._scorePrefab, 100, true);
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
}
2024-03-26 00:28:59 -07:00
protected onEnable(): void {
2024-03-29 04:24:58 -07:00
this._ticketMinus.string = '-' + BEConnector.instance.getTicketCanBeMinus().toString();
2024-03-26 00:28:59 -07:00
this.topScore.string = BEConnector.instance.maxScore.toString();
2024-03-29 04:24:58 -07:00
this.yourScore.string = BEConnector.instance.currentScore.toString();
const gameScore = GameManager.instance.score;
const currentScore = BEConnector.instance.currentScore;
this.playCollectEffect(gameScore, currentScore);
2024-03-26 00:28:59 -07:00
this.scheduleOnce(this.endGame, 60);
2024-03-29 04:24:58 -07:00
this._active = true;
}
private async onGameStateChange(state: GameState) {
switch (state) {
case GameState.End:
this._buyTicketBtn.active = false;
this._quitBtn.active = false;
break;
case GameState.GameOver:
break;
default:
break;
}
2024-03-26 00:28:59 -07:00
}
onClickYesButton() {
2024-03-29 04:24:58 -07:00
if (BEConnector.instance.canRelive()) {
BEConnector.instance
.checkGameScoreTicket()
.then(() => {
GameManager.instance.gameRelive();
})
.catch(() => {
GameManager.instance.gameOver();
});
} else {
BEConnector.instance.postMessage();
}
2024-03-26 00:28:59 -07:00
}
onClickNoButton() {
GameManager.instance.gameOver();
}
protected onDisable(): void {
2024-03-29 04:24:58 -07:00
this._active = false;
2024-03-26 00:28:59 -07:00
this.unschedule(this.endGame);
}
private endGame() {
GameManager.instance.gameOver();
}
2024-03-29 04:24:58 -07:00
private async playCollectEffect(gameScore: number, currentScore: number) {
if (!this._active) {
let items = Math.round(gameScore / 5);
let x = 5;
2024-03-31 19:27:35 -07:00
if (items >= 300) {
items = 300;
2024-03-29 04:24:58 -07:00
x = Math.round(gameScore / items);
}
const time = items < 10 ? 0.1 : 2 / items;
let score = currentScore;
for (let i = 0; i < items; i++) {
score += x;
const obj = this._pool.get(this._scoreUI);
obj.setWorldPosition(this._scoreUI.worldPosition);
tween(obj)
.to(0.3, { worldPosition: this.yourScore.node.worldPosition })
.call(() => {
Tween.stopAllByTarget(this.yourScore);
tween(this.yourScore.node)
.to(0.1, { scale: new Vec3(1.3, 1.3, 1.3) })
.call(() => {
this.yourScore.string =
items == 0
? (gameScore + BEConnector.instance.currentScore).toString()
: score.toString();
})
.set({ scale: Vec3.ONE })
.start();
})
.call(() => ObjectPool.release(obj))
.start();
items--;
await Utilities.delay(time);
}
}
}
2024-03-26 00:28:59 -07:00
}