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

156 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-04-04 04:27:04 -07:00
import { _decorator, AudioClip, Component, Label, Node, Prefab, randomRange, 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-04-03 02:43:18 -07:00
import { SoundManager } from '../Manager/SoundManager';
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;
2024-04-03 02:43:18 -07:00
@property({ type: AudioClip, visible: true })
private _soundCollectCoinFx: AudioClip;
2024-03-29 04:24:58 -07:00
private _pool: ObjectPool;
private _active = false;
2024-04-01 19:26:53 -07:00
private _clicked = false;
2024-04-03 02:43:18 -07:00
private _end = false;
2024-03-29 04:24:58 -07:00
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) {
2024-04-03 02:43:18 -07:00
case GameState.Init:
break;
case GameState.Ready:
break;
case GameState.Playing:
break;
case GameState.GameOver:
break;
2024-03-29 04:24:58 -07:00
case GameState.End:
this._buyTicketBtn.active = false;
this._quitBtn.active = false;
2024-04-03 02:43:18 -07:00
this._end = true;
if (this._active) {
await Utilities.delay(1);
2024-04-05 01:09:19 -07:00
BEConnector.instance.postScoreToServer();
2024-04-03 02:43:18 -07:00
}
2024-03-29 04:24:58 -07:00
break;
2024-04-03 02:43:18 -07:00
case GameState.Relive:
2024-03-29 04:24:58 -07:00
break;
}
2024-03-26 00:28:59 -07:00
}
onClickYesButton() {
2024-04-01 19:26:53 -07:00
if (this._clicked) return;
this._clicked = true;
2024-03-29 04:24:58 -07:00
if (BEConnector.instance.canRelive()) {
BEConnector.instance
.checkGameScoreTicket()
.then(() => {
2024-04-01 19:26:53 -07:00
this._clicked = false;
2024-03-29 04:24:58 -07:00
GameManager.instance.gameRelive();
})
.catch(() => {
2024-04-01 19:26:53 -07:00
this._clicked = false;
2024-03-29 04:24:58 -07:00
GameManager.instance.gameOver();
});
} else {
2024-04-01 19:26:53 -07:00
this._clicked = false;
2024-03-29 04:24:58 -07:00
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) {
2024-04-03 02:43:18 -07:00
let x = 10;
let items = Math.ceil(gameScore / x);
if (items >= 50) {
items = 50;
2024-03-29 04:24:58 -07:00
x = Math.round(gameScore / items);
}
2024-04-03 02:43:18 -07:00
const time = 0.04;
2024-04-04 04:27:04 -07:00
const totalScore = gameScore + currentScore;
2024-03-29 04:24:58 -07:00
let score = currentScore;
2024-04-04 04:27:04 -07:00
const target = this.yourScore.node.getWorldPosition();
2024-03-29 04:24:58 -07:00
for (let i = 0; i < items; i++) {
score += x;
2024-04-04 04:27:04 -07:00
score = score > totalScore ? totalScore : score;
2024-03-29 04:24:58 -07:00
const obj = this._pool.get(this._scoreUI);
2024-04-04 21:43:09 -07:00
obj.setWorldPosition(this._scoreUI.worldPosition);
2024-03-29 04:24:58 -07:00
tween(obj)
2024-04-04 21:43:09 -07:00
.to(randomRange(0.2, 0.3), { worldPosition: target }, { easing: 'sineIn' })
.call(() => this._pool.release(obj))
2024-03-29 04:24:58 -07:00
.call(() => {
Tween.stopAllByTarget(this.yourScore);
tween(this.yourScore.node)
2024-04-04 21:43:09 -07:00
.to(0.1, { scale: new Vec3(1.3, 1.3, 1.3) })
2024-04-03 02:43:18 -07:00
.call(async () => {
if (i == items - 1) {
2024-04-04 04:27:04 -07:00
this.yourScore.string = totalScore.toString();
2024-04-03 02:43:18 -07:00
if (this._end) {
await Utilities.delay(1);
2024-04-05 01:09:19 -07:00
BEConnector.instance.postScoreToServer();
2024-04-03 02:43:18 -07:00
}
} else {
this.yourScore.string = score.toString();
}
2024-03-29 04:24:58 -07:00
})
.set({ scale: Vec3.ONE })
.start();
})
.start();
2024-04-03 02:43:18 -07:00
SoundManager.instance.playSfx(this._soundCollectCoinFx);
2024-03-29 04:24:58 -07:00
await Utilities.delay(time);
}
}
}
2024-03-26 00:28:59 -07:00
}