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

168 lines
6.2 KiB
TypeScript

import { _decorator, AudioClip, Component, geometry, Label, Node, Tween, tween, Vec3 } from 'cc';
import GameState from '../Enum/GameState';
import GameEvent from '../Events/GameEvent';
import SpriteFloatingFactory from '../Factory/SpriteFloatingFactory';
import AudioManager from '../Manager/AudioManager';
import { EventManger } from '../Manager/EventManger';
import { GameManager } from '../Manager/GameManager';
import P4PSDK from '../P4PSDK';
import Utils from '../Utilities';
const { ccclass, property } = _decorator;
@ccclass('GameOverPanel')
export class GameOverPanel extends Component {
@property(Label)
private topScore: Label = null;
@property(Label)
private yourScore: Label = null;
@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: SpriteFloatingFactory, visible: true })
private _floatingStarFactory: SpriteFloatingFactory;
@property({ type: AudioClip, visible: true })
private _soundCollectCoinFx: AudioClip;
@property({ type: AudioClip, visible: true })
private _soundScore: AudioClip;
@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();
private _active = false;
private _clicked = false;
private _end = false;
protected onEnable(): void {
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
}
public show(end: boolean): void {
this._ticketMinus.string = P4PSDK.getTicketNeedToContinue().toString();
const currentScore = P4PSDK.getLatestScore();
const gameScore = P4PSDK.getGameScore();
this.topScore.string = P4PSDK.getTopScore().toString();
this.yourScore.string = currentScore.toString();
this.playCollectEffect(gameScore, currentScore);
this.scheduleOnce(this.endGame, 60);
this._end = end;
this._active = true;
if (this._end) {
this._buyTicketBtn.active = false;
this._quitBtn.active = false;
}
}
private async onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
break;
case GameState.Ready:
break;
case GameState.Playing:
break;
case GameState.GameOver:
break;
case GameState.End:
this._buyTicketBtn.active = false;
this._quitBtn.active = false;
if (this._active) {
await Utils.delay(1);
P4PSDK.postScoreToServer();
}
break;
case GameState.Relive:
break;
}
}
onClickYesButton() {
GameManager.instance.replay();
}
onClickNoButton() {
GameManager.instance.gameOver();
}
protected onDisable(): void {
this._active = false;
this.unschedule(this.endGame);
EventManger.instance.off(GameEvent.GameStateChange, this.onGameStateChange, this);
}
private endGame() {
GameManager.instance.gameOver();
}
private async playCollectEffect(gameScore: number, currentScore: number) {
if (!this._active) {
let x = 10;
let items = Math.ceil(gameScore / x);
if (items >= 50) {
items = 50;
x = Math.round(gameScore / items);
}
const totalScore = gameScore + currentScore;
let score = currentScore;
const target = this.yourScore.node.getWorldPosition();
let duration = 0;
for (let i = 0; i < items; i++) {
if (this._clicked) return;
duration = this._starSpeedCurve.evaluate(i / items - 1);
const obj = this._floatingStarFactory.create(this._scoreUI);
obj.node.setWorldPosition(this._scoreUI.worldPosition);
tween(obj.node)
.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));
},
},
)
.call(() => obj.node.releaseToPool())
.call(async () => {
score += x;
score = score > totalScore ? totalScore : score;
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();
})
.start();
AudioManager.playSfx(this._soundCollectCoinFx);
await Utils.delay(duration / 3);
}
await Utils.waitUntil(() => this._floatingStarFactory.countActive == 0, 0.1);
this.yourScore.string = totalScore.toString();
AudioManager.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 Utils.delay(1);
P4PSDK.postScoreToServer();
}
}
}