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

177 lines
6.5 KiB
TypeScript
Raw Normal View History

2024-04-24 04:05:06 -07:00
import { _decorator, AudioClip, Component, geometry, 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-04-24 04:05:06 -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-24 04:05:06 -07:00
@property({ type: AudioClip, visible: true })
private _soundCollectCoinFx: AudioClip;
2024-05-07 03:20:37 -07:00
@property({ type: AudioClip, visible: true })
private _soundScore: AudioClip;
2024-04-03 02:43:18 -07:00
2024-04-17 04:11:13 -07:00
@property({ type: geometry.AnimationCurve, visible: true })
private _starSpeedCurve: geometry.AnimationCurve = new geometry.AnimationCurve();
@property({ type: geometry.AnimationCurve, visible: true })
private _starScaleCurve: geometry.AnimationCurve = new geometry.AnimationCurve();
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-04-26 00:48:20 -07:00
this._ticketMinus.string = '-' + BEConnector.getTicketCanBeMinus().toString();
this.topScore.string = BEConnector.maxScore.toString();
this.yourScore.string = BEConnector.currentScore.toString();
2024-03-29 04:24:58 -07:00
const gameScore = GameManager.instance.score;
2024-04-26 00:48:20 -07:00
const currentScore = BEConnector.currentScore;
2024-03-29 04:24:58 -07:00
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-26 00:48:20 -07:00
BEConnector.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-04-26 00:48:20 -07:00
if (BEConnector.canRelive()) {
BEConnector.checkGameScoreTicket()
2024-03-29 04:24:58 -07:00
.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-04-26 00:48:20 -07:00
BEConnector.postMessage();
2024-03-29 04:24:58 -07:00
}
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-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-04-17 04:11:13 -07:00
let duration = 0;
2024-03-29 04:24:58 -07:00
for (let i = 0; i < items; i++) {
score += x;
2024-04-17 04:11:13 -07:00
duration = this._starSpeedCurve.evaluate(i / items - 1);
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-17 04:11:13 -07:00
.to(
duration,
{ worldPosition: target },
{
easing: 'sineIn',
onUpdate: (target: Node, ratio: number) => {
const scale = this._starScaleCurve.evaluate(ratio) * 1.5;
target.setScale(new Vec3(scale, scale));
},
},
)
2024-04-04 21:43:09 -07:00
.call(() => this._pool.release(obj))
2024-04-17 01:47:13 -07:00
.call(async () => {
Tween.stopAllByTarget(this.yourScore.node);
this.yourScore.string = score.toString();
tween(this.yourScore.node)
.to(duration / 6, { scale: new Vec3(1.3, 1.3) })
.to(duration / 6, { scale: new Vec3(1, 1) })
.start();
2024-03-29 04:24:58 -07:00
})
.start();
2024-04-24 04:05:06 -07:00
SoundManager.instance.playSfx(this._soundCollectCoinFx);
2024-04-17 04:11:13 -07:00
await Utilities.delay(duration / 3);
2024-03-29 04:24:58 -07:00
}
await Utilities.waitUntil(() => this._pool.countActive == 0, 0.1);
this.yourScore.string = totalScore.toString();
2024-05-07 03:20:37 -07:00
SoundManager.instance.playSfx(this._soundScore);
Tween.stopAllByTarget(this.yourScore.node);
tween(this.yourScore.node)
.set({ scale: Vec3.ONE })
.to(0.3, { scale: new Vec3(1.8, 1.8) }, { easing: 'backIn' })
.to(0.3, { scale: new Vec3(1, 1) }, { easing: 'backOut' })
.start();
if (!this._end) return;
await Utilities.delay(1);
2024-04-26 00:48:20 -07:00
BEConnector.postScoreToServer();
2024-03-29 04:24:58 -07:00
}
}
2024-03-26 00:28:59 -07:00
}