// MainUIController.ts import { _decorator, Component, Node, Button, Label, find } from 'cc'; import { SaveDataManager } from '../../LevelSystem/SaveDataManager'; import { GameManager } from '../../LevelSystem/GameManager'; import { GameStartMove } from '../../Animations/GameStartMove'; const { ccclass, property } = _decorator; @ccclass('MainUIController') export class MainUIController extends Component { /* 顶部资源 & 关卡 */ @property(Node) moneyAddBtn: Node = null; // 绿色 + @property(Node) diamondAddBtn: Node = null; // 紫色 + @property(Node) moneyLabel: Node = null; // 金币数字 @property(Node) diamondLabel: Node = null; // 钻石数字 @property(Label) levelNumberLabel: Label = null; // 第 X 关文本 /* 奖励节点 */ @property(Node) rewardMoneyNode: Node = null; // 左金币奖励 @property(Node) rewardDiamondNode: Node = null; // 右钻石奖励 /* 升级 */ @property(Button) upgradeBtn: Button = null; // 升级按钮 @property(Node) upgradeCostLabel: Node = null; // 消耗金币数字 @property(Node) upgradeHpLabel: Node = null; // "105>>1000" /* 主功能按钮 */ @property(Node) battleBtn: Node = null; // 战斗 @property(Node) shopBtn: Node = null; // 商店 // 底栏按钮由 NavBarController 统一管理,这里不再需要引用 @property(Node) topArea: Node = null; // Canvas-001/TopArea private sdm: SaveDataManager = null; onLoad () { this.sdm = SaveDataManager.getInstance(); this.sdm.initialize(); this.bindButtons(); this.refreshAll(); // TopArea 默认隐藏,在点击战斗后再显示 if (this.topArea) this.topArea.active = false; } /* 绑定按钮事件 */ private bindButtons () { this.moneyAddBtn?.on(Button.EventType.CLICK, () => this.addCoins(100), this); this.diamondAddBtn?.on(Button.EventType.CLICK, () => this.addDiamonds(20), this); this.rewardMoneyNode?.on(Node.EventType.TOUCH_END, () => this.takeRewardCoins(), this); this.rewardDiamondNode?.on(Node.EventType.TOUCH_END, () => this.takeRewardDiamonds(), this); this.upgradeBtn?.node.on(Button.EventType.CLICK, this.upgradeWallHp, this); this.battleBtn?.on(Button.EventType.CLICK, this.onBattle, this); this.shopBtn?.on(Button.EventType.CLICK, this.onShop, this); // 底栏按钮已迁移至 NavBarController,无需在此脚本绑定 } /* ================= 业务逻辑 ================= */ private addCoins (v:number) { this.sdm.addCoins(v, 'ad'); this.refreshCurrency(); } private addDiamonds (v:number){ this.sdm.addDiamonds(v,'ad'); this.refreshCurrency(); } private takeRewardCoins () { const lbl = this.rewardMoneyNode?.getComponent(Label) || this.rewardMoneyNode?.getComponentInChildren(Label); if (!lbl) return; const amt = parseInt(lbl.string||'0'); if (amt>0) { this.addCoins(amt); this.rewardMoneyNode.active = false; } } private takeRewardDiamonds () { const lbl = this.rewardDiamondNode?.getComponent(Label) || this.rewardDiamondNode?.getComponentInChildren(Label); if (!lbl) return; const amt = parseInt(lbl.string||'0'); if (amt>0) { this.addDiamonds(amt); this.rewardDiamondNode.active = false; } } private upgradeWallHp () { const costLbl = this.upgradeCostLabel?.getComponent(Label); if (!costLbl) return; const cost = parseInt(costLbl.string || '0'); if (!this.sdm.spendCoins(cost)) return; // 金币不足 // 升级墙体 const gmNode = find('Canvas/GameLevelUI/GameManager'); const gm = gmNode?.getComponent(GameManager); if (!gm) return; const info = gm.upgradeWallLevel?.(); if (!info) return; // 更新显示 const hpLbl = this.upgradeHpLabel?.getComponent(Label); if (hpLbl) { hpLbl.string = `${info.currentHp}>>${info.nextHp}`; } this.refreshCurrency(); } private onBattle () { // 显示 TopArea(拖拽引用),避免使用 find() if (this.topArea) this.topArea.active = true; // 若上一关已完成则自动+1 const lvl = this.sdm.getCurrentLevel(); if (this.sdm.isLevelCompleted(lvl)) { const pd = this.sdm.getPlayerData(); pd.currentLevel = lvl + 1; this.sdm.savePlayerData(); } this.refreshLevelNumber(); // 切场景 UI const mainUI = find('Canvas/MainUI'); const gameUI = find('Canvas/GameLevelUI'); if (mainUI) mainUI.active = false; if (gameUI) gameUI.active = true; const gmNode = find('Canvas/GameLevelUI/GameManager'); const gm = gmNode?.getComponent(GameManager); gm?.restartGame(); gm?.loadCurrentLevelConfig(); // 隐藏底部导航栏 NavBar const navBarNode = find('Canvas/NavBar'); if (navBarNode) navBarNode.active = false; // Camera move down instantly for battle prep // 兼容 "Camera" 或 "Main Camera" 等命名 const camNode = find('Canvas/Camera'); const gsm = camNode?.getComponent(GameStartMove); gsm?.moveDownInstant(); } private onShop () { const mainUI = find('Canvas/MainUI'); const shopUI = find('Canvas/ShopUI'); if (mainUI) mainUI.active = false; if (shopUI) shopUI.active = true; } /* ================= 刷新 ================= */ private refreshCurrency () { const mLbl = this.moneyLabel?.getComponent(Label) || this.moneyLabel as unknown as Label; if (mLbl) mLbl.string = this.format(this.sdm.getCoins()); const dLbl = this.diamondLabel?.getComponent(Label) || this.diamondLabel as unknown as Label; if (dLbl) dLbl.string = this.format(this.sdm.getDiamonds()); } private refreshLevelNumber(){ if (this.levelNumberLabel) this.levelNumberLabel.string = `第 ${this.sdm.getCurrentLevel()} 关`; } private refreshAll(){ this.refreshCurrency(); this.refreshLevelNumber(); this.refreshUpgradeInfo(); } /** 刷新升级信息显示 */ private refreshUpgradeInfo () { const gmNode = find('Canvas/GameLevelUI/GameManager'); const gm = gmNode?.getComponent(GameManager); const hpLbl = this.upgradeHpLabel?.getComponent(Label); if (!gm || !hpLbl) return; const curHp = gm.getCurrentWallHealth?.() || 0; const nextHp = gm.getWallHealthByLevel?.(gm.getCurrentWallLevel?.() + 1) || curHp; hpLbl.string = `${curHp}>>${nextHp}`; } // 供外部(如 GameManager)调用的公共刷新接口 public updateUI (): void { this.refreshAll(); } /* =============== Util =============== */ private format(n:number){ return n>=1000000? (n/1e6).toFixed(1)+'M' : n>=1000? (n/1e3).toFixed(1)+'K' : n.toString(); } }