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

138 lines
4.7 KiB
TypeScript

import { _decorator, Button, CCInteger, Component, EventTarget, Label, Node, EventTouch } from 'cc';
import BEConnector from '../API/BEConnector';
import { GameManager } from '../Manager/GameManager';
const { ccclass, property } = _decorator;
export enum PanelState {
NotShowing,
CompareScore,
Continue,
ShowResult,
}
@ccclass('EndGameUIController')
export class EndGameUIController extends Component {
@property(CCInteger)
private countdownTime: number = 15;
private currentCD: number = 15;
private isContinuable: boolean = true;
private isEndGameByTime: boolean = false;
private currentState: PanelState = PanelState.NotShowing;
// Texts
@property(Label)
private yourScoreTxt: Label = null;
@property(Label)
private topScoreTxt: Label = null;
@property(Label)
private continueTxt: Label = null;
@property(Label)
private countdownToReplayTxt: Label = null;
// Buttons
@property(Button)
private yesBtn: Label = null;
@property(Button)
private noBtn: Label = null;
// Panel
@property(Node)
private compareScorePnl: Node = null;
@property(Node)
private continuePnl: Node = null;
@property(Node)
private finalResultPnl: Node = null;
private eventRevive = new EventTarget();
//#region Cocos methods
protected onEnable(): void {
this.Setup();
this.currentCD = this.countdownTime;
}
protected update(dt: number): void {
if (this.isEndGameByTime == true) return;
this.currentCD -= dt;
this.countdownToReplayTxt.string = Math.round(this.currentCD).toString();
if (this.currentCD <= 0 && !this.isEndGameByTime) {
this.currentCD = 0;
this.ChangeState(PanelState.ShowResult);
this.isEndGameByTime = true;
}
}
//#endregion
private Setup(): void {
if (this.isContinuable) this.ChangeState(PanelState.CompareScore);
else this.ChangeState(PanelState.ShowResult);
}
private ChangeState(newState: PanelState): void {
if (this.currentState == newState) return;
this.currentState = newState;
switch (this.currentState) {
case PanelState.CompareScore:
this.TurnOffAllPanels(this.compareScorePnl);
var totalScore = GameManager.instance.score;
this.yourScoreTxt.string = 'Your score: ' + totalScore;
/// Todo: set top score here
break;
case PanelState.Continue:
this.TurnOffAllPanels(this.continuePnl);
var ticket = BEConnector.instance.numberTicket;
if (ticket >= BEConnector.instance.getTicketCanBeMinus()) {
this.continueTxt.string =
'To continue playing, you will be deducted ' + ticket + ' Extras. Do you want to proceed?';
} else {
this.continueTxt.string =
"You don't have enough Extras to continue playing. Would you like to buy more tickets?";
}
break;
case PanelState.ShowResult:
this.yesBtn.node.active = false;
this.noBtn.node.active = false;
this.countdownToReplayTxt.node.active = false;
this.TurnOffAllPanels(this.finalResultPnl);
BEConnector.instance.postScoreToServer(GameManager.instance.score);
break;
}
}
private TurnOffAllPanels(activePnl: Node): void {
this.compareScorePnl.active = false;
this.continuePnl.active = false;
this.finalResultPnl.active = false;
if (activePnl != null) activePnl.active = true;
}
public YesNoClick(e: EventTouch, yesNoString: string): void {
var isYesClick = true;
if (yesNoString == 'true') isYesClick = true;
else isYesClick = false;
switch (this.currentState) {
case PanelState.CompareScore:
if (isYesClick) this.ChangeState(PanelState.Continue);
else this.ChangeState(PanelState.ShowResult);
break;
case PanelState.Continue:
if (isYesClick) {
var ticket = BEConnector.instance.getTicketCanBeMinus();
if (BEConnector.instance.numberTicket >= ticket) {
GameManager.instance.onRevive();
BEConnector.instance.ticketMinus('revive');
this.isContinuable = false;
} else BEConnector.instance.postMessage();
} else {
this.ChangeState(PanelState.CompareScore);
}
break;
}
}
}