super-hero/assets/cc-game/scripts/game_ui/GameUI.ts

170 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-05-08 04:03:33 -07:00
import { _decorator, Component, Node } from 'cc';
import { HPBar } from './HPBar';
import { GameGlobalData } from '../global/GameGlobalData';
import { EGAME_STATE, GameDefine } from '../config/GameDefine';
import { Button } from 'cc';
import { UmClientEvent } from '../../../cc-common/cc-util/UmOneToMultiListener';
import { director } from 'cc';
import { Label } from 'cc';
import { GameWinLayout } from './GameWinLayout';
import { LevelUpLayout } from './LevelUpLayout';
import { UmLog } from '../../../cc-common/cc-util/UmLog';
2024-05-12 20:54:17 -07:00
import { JoyStick } from '../joy_stick/JoyStick';
import { BUILD_MODE, UmConfig } from '../../../cc-common/cc-util/UmConfig';
import { UmUtil } from '../../../cc-common/cc-util/UmUtil';
2024-05-08 04:03:33 -07:00
const { ccclass, property } = _decorator;
@ccclass('GameUI')
export class GameUI extends Component {
@property(Node) gameWinPopup: Node = null!;
@property(Node) gameLosePopup: Node = null!;
@property(Node) levelUpLayout: Node = null!;
@property(Node) topLayout: Node = null!;
@property(Node) bottomLayout: Node = null!;
@property(HPBar) bossHPBar: HPBar = null!;
@property(HPBar) heroHPBar: HPBar = null!;
@property(HPBar) expBar: HPBar = null!;
@property(Button) btnConfig: Button = null!;
@property(Button) btnStart: Button = null!;
@property(Node) layoutConfig: Node = null!;
@property(Label) expLevelTitle: Label = null!;
2024-05-12 20:54:17 -07:00
@property(Label) levelTxt: Label = null!;
2024-05-08 04:03:33 -07:00
@property(Label) txtSkillCollectDebug: Label = null!;
2024-05-12 20:54:17 -07:00
@property(Label) txtCoin: Label = null!;
2024-05-08 04:03:33 -07:00
protected onLoad(): void {
this.btnConfig?.node.on(Button.EventType.CLICK, this.showLayoutConfig, this);
this.btnStart?.node.on(Button.EventType.CLICK, this.onBtnStartClicked, this);
}
protected start(): void {
GameGlobalData.Instance.changeState(EGAME_STATE.INIT);
this.btnStart?.node.setNodeActive(true);
this.gameWinPopup.setNodeActive(false);
this.gameLosePopup.setNodeActive(false);
this.levelUpLayout.setNodeActive(false);
this.setTopBottomLayoutActive(false);
this.getBossHPBar().node.setNodeActive(false);
this.debugSkillCollectedDebug();
JoyStick.Instance.hideJoySitck();
}
public getBossHPBar(): HPBar
{
return this.bossHPBar;
}
public getHeroHPBar(): HPBar {
return this.heroHPBar;
}
public getExpbar(): HPBar {
return this.expBar;
}
public setExpLevelTitle(content: string)
{
if (!this.expLevelTitle)
return;
this.expLevelTitle.string = content;
}
2024-05-12 20:54:17 -07:00
setLevelTxt(level: number)
{
this.levelTxt.string = `Level ${level}`;
}
setCoinValue(value: number)
{
this.txtCoin.string = value.formatMoney();
}
2024-05-08 04:03:33 -07:00
private onBtnStartClicked(btn: Button)
{
this.btnStart?.node.setNodeActive(false);
this.setTopBottomLayoutActive(true);
this.displayStatsDataOnStartGame();
UmClientEvent.dispatchEvent(GameDefine.EVENT_START_GAME);
JoyStick.Instance.showJoystick();
}
public setTopBottomLayoutActive(isActive: boolean)
{
this.bottomLayout?.setNodeActive(isActive);
this.topLayout?.setNodeActive(isActive);
}
public showLayoutConfig()
{
this.layoutConfig?.setNodeActive(true);
}
public displayStatsDataOnStartGame()
{
var expLevel = GameGlobalData.Instance.getExpInlevelNeed();
var level = GameGlobalData.Instance.level;
this.setExpLevelTitle(`Exp Level ${level}`);
2024-05-12 20:54:17 -07:00
this.setLevelTxt(level);
2024-05-08 04:03:33 -07:00
this.getExpbar().setHPValue(GameGlobalData.Instance.userDataSaver.expInLevel, expLevel);
this.debugSkillCollectedDebug();
2024-05-12 20:54:17 -07:00
this.setCoinValue(GameGlobalData.Instance.getGold());
2024-05-08 04:03:33 -07:00
}
public updateExpProgressBar(expCollectRunTime: number)
{
var currentExp = expCollectRunTime + GameGlobalData.Instance.userDataSaver.expInLevel;
// UmLog.log("updateExpProgressBar => ", currentExp, GameGlobalData.Instance.getExpInlevelNeed());
this.getExpbar().setHPValue(currentExp, GameGlobalData.Instance.getExpInlevelNeed());
}
2024-05-12 20:54:17 -07:00
public async showGameLose()
2024-05-08 04:03:33 -07:00
{
GameGlobalData.Instance.changeState(EGAME_STATE.FINISH);
2024-05-12 20:54:17 -07:00
JoyStick.Instance.hideJoySitck();
await UmUtil.asyncDelay(5);
2024-05-08 04:03:33 -07:00
this.gameLosePopup.setNodeActive(true);
}
public showGameWin(rewardData)
{
GameGlobalData.Instance.changeState(EGAME_STATE.FINISH);
this.gameWinPopup.setNodeActive(true);
this.gameWinPopup.getComponent(GameWinLayout)?.showWin(rewardData, () => {
GameGlobalData.Instance.newGame();
});
}
checkLevelUp()
{
UmLog.log("checkLevelUp");
if (!GameGlobalData.Instance.checkLevelUpWithSessionExpCollected(GameGlobalData.Instance.expCollected))
{
UmLog.log("checkLevelUp => NO");
return;
}
this.setExpLevelTitle(`Exp Level ${GameGlobalData.Instance.level}`);
this.updateExpProgressBar(GameGlobalData.Instance.expCollected);
UmLog.log("SHOW Level Up");
var levelAfter = GameGlobalData.Instance.level;
this.levelUpLayout.setNodeActive(true);
this.levelUpLayout.getComponent(LevelUpLayout).showLevelUp(levelAfter - 1, levelAfter, () => {
this.debugSkillCollectedDebug();
});
}
public debugSkillCollectedDebug()
{
// var debug: string = "";
this.txtSkillCollectDebug.string = `AS => ${JSON.stringify(GameGlobalData.Instance.userDataSaver.activeSkillCollected)} | PS => ${JSON.stringify(GameGlobalData.Instance.passiveSkillCollected) }`;
}
}