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

138 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-02-28 03:25:11 -08:00
import { _decorator, Button, CCInteger, Component, EventTarget, Label, Node, EventTouch } from 'cc';
2024-03-06 01:28:01 -08:00
import BEConnector from '../API/BEConnector';
import { GameManager } from '../Manager/GameManager';
2024-02-27 18:19:33 -08:00
const { ccclass, property } = _decorator;
export enum PanelState {
NotShowing,
CompareScore,
Continue,
2024-02-28 03:25:11 -08:00
ShowResult,
2024-02-27 18:19:33 -08:00
}
@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 {
2024-02-28 03:25:11 -08:00
if (this.isContinuable) this.ChangeState(PanelState.CompareScore);
else this.ChangeState(PanelState.ShowResult);
2024-02-27 18:19:33 -08:00
}
private ChangeState(newState: PanelState): void {
if (this.currentState == newState) return;
this.currentState = newState;
switch (this.currentState) {
case PanelState.CompareScore:
this.TurnOffAllPanels(this.compareScorePnl);
2024-03-06 01:28:01 -08:00
var totalScore = GameManager.instance.score + GameManager.instance.highestStreak * 2;
2024-02-28 03:25:11 -08:00
this.yourScoreTxt.string = 'Your score: ' + totalScore;
2024-02-27 18:19:33 -08:00
/// Todo: set top score here
break;
case PanelState.Continue:
this.TurnOffAllPanels(this.continuePnl);
var ticket = BEConnector.instance.numberTicket;
if (ticket >= BEConnector.instance.getTicketCanBeMinus()) {
2024-02-28 03:25:11 -08:00
this.continueTxt.string =
'To continue playing, you will be deducted ' + ticket + ' Extras. Do you want to proceed?';
} else {
2024-02-27 18:19:33 -08:00
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);
2024-03-06 01:28:01 -08:00
BEConnector.instance.postScoreToServer(GameManager.instance.score);
2024-02-27 18:19:33 -08:00
break;
}
}
private TurnOffAllPanels(activePnl: Node): void {
this.compareScorePnl.active = false;
this.continuePnl.active = false;
this.finalResultPnl.active = false;
2024-02-28 03:25:11 -08:00
if (activePnl != null) activePnl.active = true;
2024-02-27 18:19:33 -08:00
}
public YesNoClick(e: EventTouch, yesNoString: string): void {
var isYesClick = true;
2024-02-28 03:25:11 -08:00
if (yesNoString == 'true') isYesClick = true;
2024-02-27 18:19:33 -08:00
else isYesClick = false;
switch (this.currentState) {
case PanelState.CompareScore:
2024-02-28 03:25:11 -08:00
if (isYesClick) this.ChangeState(PanelState.Continue);
else this.ChangeState(PanelState.ShowResult);
2024-02-27 18:19:33 -08:00
break;
case PanelState.Continue:
if (isYesClick) {
var ticket = BEConnector.instance.getTicketCanBeMinus();
if (BEConnector.instance.numberTicket >= ticket) {
2024-03-06 01:28:01 -08:00
GameManager.instance.onRevive();
2024-02-28 03:25:11 -08:00
BEConnector.instance.ticketMinus('revive');
2024-02-27 18:19:33 -08:00
this.isContinuable = false;
2024-02-28 03:25:11 -08:00
} else BEConnector.instance.postMessage();
} else {
2024-02-27 18:19:33 -08:00
this.ChangeState(PanelState.CompareScore);
}
break;
}
}
2024-02-28 03:25:11 -08:00
}