2024-06-09 05:12:08 -07:00
|
|
|
import { _decorator, AudioClip, Component, geometry, Label, Node, Tween, tween, Vec3 } from 'cc';
|
2024-05-27 02:19:31 -07:00
|
|
|
import GameState from '../Enum/GameState';
|
|
|
|
import GameEvent from '../Events/GameEvent';
|
2024-06-09 05:12:08 -07:00
|
|
|
import SpriteFloatingFactory from '../Factory/SpriteFloatingFactory';
|
2024-05-27 02:19:31 -07:00
|
|
|
import AudioManager from '../Manager/AudioManager';
|
|
|
|
import { EventManger } from '../Manager/EventManger';
|
2024-03-26 00:28:59 -07:00
|
|
|
import { GameManager } from '../Manager/GameManager';
|
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-26 00:28:59 -07:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass('GameOverPanel')
|
|
|
|
export class GameOverPanel extends Component {
|
2024-06-09 05:12:08 -07:00
|
|
|
@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;
|
2024-06-09 05:12:08 -07:00
|
|
|
@property({ type: SpriteFloatingFactory, visible: true })
|
|
|
|
private _floatingStarFactory: SpriteFloatingFactory;
|
2024-03-29 04:24:58 -07:00
|
|
|
|
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 _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
|
|
|
|
2024-06-18 02:59:16 -07:00
|
|
|
protected onEnable(): void {
|
2024-03-29 04:24:58 -07:00
|
|
|
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
|
|
|
|
}
|
2024-03-26 00:28:59 -07:00
|
|
|
|
2024-06-18 02:59:16 -07:00
|
|
|
public show(end: boolean): void {
|
2024-06-18 00:42:31 -07:00
|
|
|
this._ticketMinus.string = P4PSDK.getTicketNeedToContinue().toString();
|
2024-06-12 04:48:19 -07:00
|
|
|
const currentScore = P4PSDK.getLatestScore();
|
2024-06-13 04:15:18 -07:00
|
|
|
const gameScore = P4PSDK.getGameScore();
|
2024-06-12 04:48:19 -07:00
|
|
|
this.topScore.string = P4PSDK.getTopScore().toString();
|
|
|
|
this.yourScore.string = currentScore.toString();
|
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-06-18 02:59:16 -07:00
|
|
|
this._end = end;
|
2024-03-29 04:24:58 -07:00
|
|
|
this._active = true;
|
2024-06-18 02:59:16 -07:00
|
|
|
if (this._end) {
|
|
|
|
this._buyTicketBtn.active = false;
|
|
|
|
this._quitBtn.active = false;
|
|
|
|
}
|
2024-03-29 04:24:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
if (this._active) {
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.delay(1);
|
2024-06-12 04:48:19 -07:00
|
|
|
P4PSDK.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-06-12 04:48:19 -07:00
|
|
|
GameManager.instance.replay();
|
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);
|
2024-06-18 02:59:16 -07:00
|
|
|
EventManger.instance.off(GameEvent.GameStateChange, this.onGameStateChange, this);
|
2024-03-26 00:28:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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++) {
|
2024-06-18 01:53:46 -07:00
|
|
|
if (this._clicked) return;
|
2024-03-29 04:24:58 -07:00
|
|
|
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-06-10 21:35:13 -07:00
|
|
|
const obj = this._floatingStarFactory.create(this._scoreUI);
|
2024-06-09 05:12:08 -07:00
|
|
|
obj.node.setWorldPosition(this._scoreUI.worldPosition);
|
|
|
|
tween(obj.node)
|
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-06-09 05:12:08 -07:00
|
|
|
.call(() => obj.node.releaseToPool())
|
2024-04-17 01:47:13 -07:00
|
|
|
.call(async () => {
|
2024-04-24 10:27:59 -07:00
|
|
|
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-05-27 02:19:31 -07:00
|
|
|
AudioManager.playSfx(this._soundCollectCoinFx);
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.delay(duration / 3);
|
2024-03-29 04:24:58 -07:00
|
|
|
}
|
2024-04-24 10:27:59 -07:00
|
|
|
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.waitUntil(() => this._floatingStarFactory.countActive == 0, 0.1);
|
2024-04-24 10:27:59 -07:00
|
|
|
this.yourScore.string = totalScore.toString();
|
2024-05-27 02:19:31 -07:00
|
|
|
AudioManager.playSfx(this._soundScore);
|
2024-04-24 10:27:59 -07:00
|
|
|
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;
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.delay(1);
|
2024-06-12 04:48:19 -07:00
|
|
|
P4PSDK.postScoreToServer();
|
2024-03-29 04:24:58 -07:00
|
|
|
}
|
|
|
|
}
|
2024-03-26 00:28:59 -07:00
|
|
|
}
|