import { _decorator, Component, Label, Node, Prefab, Tween, tween, Vec3 } from 'cc'; import BEConnector from '../API/BEConnector'; import { GameManager } from '../Manager/GameManager'; import ObjectPool from '../Pool/ObjectPool'; import Utilities from '../Utilities'; import { EventManger } from '../Manager/EventManger'; import GameEvent from '../Events/GameEvent'; import GameState from '../Enum/GameState'; 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: 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); } protected onEnable(): void { this._ticketMinus.string = '-' + BEConnector.instance.getTicketCanBeMinus().toString(); this.topScore.string = BEConnector.instance.maxScore.toString(); this.yourScore.string = BEConnector.instance.currentScore.toString(); const gameScore = GameManager.instance.score; const currentScore = BEConnector.instance.currentScore; this.playCollectEffect(gameScore, currentScore); this.scheduleOnce(this.endGame, 60); 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; } } onClickYesButton() { if (BEConnector.instance.canRelive()) { BEConnector.instance .checkGameScoreTicket() .then(() => { GameManager.instance.gameRelive(); }) .catch(() => { GameManager.instance.gameOver(); }); } else { BEConnector.instance.postMessage(); } } onClickNoButton() { GameManager.instance.gameOver(); } protected onDisable(): void { this._active = false; this.unschedule(this.endGame); } private endGame() { GameManager.instance.gameOver(); } private async playCollectEffect(gameScore: number, currentScore: number) { if (!this._active) { let items = Math.round(gameScore / 5); let x = 5; if (items >= 300) { items = 300; 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); } } } }