import { _decorator, Component, Node, Label, Button, find } from 'cc'; import { SaveDataManager } from './SaveDataManager'; import { GameManager } from './GameManager'; const { ccclass, property } = _decorator; /** * 主界面控制器 * 负责显示玩家信息和处理主界面交互 */ @ccclass('MainUIController') export class MainUIController extends Component { @property({ type: Node, tooltip: '玩家等级显示节点' }) public playerLevelLabel: Node = null; @property({ type: Node, tooltip: '当前关卡显示节点' }) public currentLevelLabel: Node = null; @property({ type: Node, tooltip: '金币显示节点' }) public coinsLabel: Node = null; @property({ type: Node, tooltip: '钻石显示节点' }) public diamondsLabel: Node = null; @property({ type: Node, tooltip: '宝石显示节点' }) public gemsLabel: Node = null; @property({ type: Node, tooltip: '战斗按钮节点' }) public battleButton: Node = null; @property({ type: Node, tooltip: '商店按钮节点' }) public shopButton: Node = null; @property({ type: Node, tooltip: '设置按钮节点' }) public settingsButton: Node = null; private saveDataManager: SaveDataManager = null; start() { // 初始化存档管理器 this.saveDataManager = SaveDataManager.getInstance(); this.saveDataManager.initialize(); // 自动查找UI节点(如果没有手动拖拽设置) this.findUINodes(); // 更新UI显示 this.updateUI(); // 设置按钮事件 this.setupButtons(); } /** * 自动查找UI节点 */ private findUINodes() { if (!this.playerLevelLabel) { this.playerLevelLabel = find('Canvas/MainUI/PlayerInfo/LevelLabel'); } if (!this.currentLevelLabel) { this.currentLevelLabel = find('Canvas/MainUI/LevelInfo/CurrentLevelLabel'); } if (!this.coinsLabel) { this.coinsLabel = find('Canvas/MainUI/Currency/CoinsLabel'); } if (!this.diamondsLabel) { this.diamondsLabel = find('Canvas/MainUI/Currency/DiamondsLabel'); } if (!this.gemsLabel) { this.gemsLabel = find('Canvas/MainUI/Currency/GemsLabel'); } if (!this.battleButton) { this.battleButton = find('Canvas/MainUI/BattleButtonNode'); } if (!this.shopButton) { this.shopButton = find('Canvas/MainUI/ShopButton'); } if (!this.settingsButton) { this.settingsButton = find('Canvas/MainUI/SettingsButton'); } } /** * 更新UI显示 */ public updateUI() { if (!this.saveDataManager) return; // 更新玩家等级显示 if (this.playerLevelLabel) { const label = this.playerLevelLabel.getComponent(Label); if (label) { const playerLevel = this.saveDataManager.getPlayerLevel(); label.string = `Lv.${playerLevel}`; } } // 更新当前关卡显示 if (this.currentLevelLabel) { const label = this.currentLevelLabel.getComponent(Label); if (label) { const currentLevel = this.saveDataManager.getCurrentLevel(); const maxUnlocked = this.saveDataManager.getMaxUnlockedLevel(); label.string = `关卡 ${currentLevel} (最高 ${maxUnlocked})`; } } // 更新货币显示 this.updateCurrencyDisplay(); // 更新按钮状态 this.updateButtonStates(); } /** * 更新货币显示 */ private updateCurrencyDisplay() { if (!this.saveDataManager) return; // 金币 if (this.coinsLabel) { const label = this.coinsLabel.getComponent(Label); if (label) { const coins = this.saveDataManager.getCoins(); label.string = this.formatNumber(coins); } } // 钻石 if (this.diamondsLabel) { const label = this.diamondsLabel.getComponent(Label); if (label) { const diamonds = this.saveDataManager.getDiamonds(); label.string = this.formatNumber(diamonds); } } // 宝石 if (this.gemsLabel) { const label = this.gemsLabel.getComponent(Label); if (label) { const gems = this.saveDataManager.getGems(); label.string = this.formatNumber(gems); } } } /** * 更新按钮状态 */ private updateButtonStates() { // 这里可以根据玩家状态启用/禁用某些按钮 // 例如:如果没有解锁某些功能,可以禁用相应按钮 } /** * 设置按钮事件 */ private setupButtons() { // 战斗按钮 if (this.battleButton) { const button = this.battleButton.getComponent(Button); if (button) { button.node.on(Button.EventType.CLICK, this.onBattleButtonClick, this); } } // 商店按钮 if (this.shopButton) { const button = this.shopButton.getComponent(Button); if (button) { button.node.on(Button.EventType.CLICK, this.onShopButtonClick, this); } } // 设置按钮 if (this.settingsButton) { const button = this.settingsButton.getComponent(Button); if (button) { button.node.on(Button.EventType.CLICK, this.onSettingsButtonClick, this); } } } /** * 战斗按钮点击事件 */ private onBattleButtonClick() { // 保存数据 if (this.saveDataManager) { this.saveDataManager.savePlayerData(); } // 切换 UI:隐藏主界面,显示关卡界面 const mainUI = find('Canvas/MainUI'); const gameLevelUI = find('Canvas/GameLevelUI'); if (mainUI) mainUI.active = false; if (gameLevelUI) gameLevelUI.active = true; // 重置并加载关卡 const gmNode = find('Canvas/GameLevelUI/GameManager'); if (gmNode) { const gm = gmNode.getComponent(GameManager); if (gm) { gm.restartGame(); // 清理上一局数据 gm.loadCurrentLevelConfig(); // 加载当前(或下一)关配置 } } } /** * 商店按钮点击事件 */ private onShopButtonClick() { // UI 切换:隐藏主界面,显示商城界面(如果存在) const mainUI = find('Canvas/MainUI'); const shopUI = find('Canvas/ShopUI'); if (mainUI) mainUI.active = false; if (shopUI) { shopUI.active = true; } else { console.warn('未找到商城界面节点 Canvas/ShopUI'); } } /** * 设置按钮点击事件 */ private onSettingsButtonClick() { // 这里可以打开设置界面 // 暂时只显示调试信息 this.showCurrentLevelInfo(); } /** * 显示当前关卡信息(调试用) */ private showCurrentLevelInfo() { if (!this.saveDataManager) return; const currentLevel = this.saveDataManager.getCurrentLevel(); const isCompleted = this.saveDataManager.isLevelCompleted(currentLevel); if (isCompleted) { const progress = this.saveDataManager.getLevelProgress(currentLevel); if (progress) { // 显示关卡完成信息 } } } /** * 格式化数字显示 */ private formatNumber(num: number): string { if (num >= 1000000) { return (num / 1000000).toFixed(1) + 'M'; } else if (num >= 1000) { return (num / 1000).toFixed(1) + 'K'; } return num.toString(); } /** * 添加货币(用于测试) */ public addTestCoins(amount: number = 1000) { if (this.saveDataManager) { this.saveDataManager.addCoins(amount, 'test'); this.updateUI(); } } /** * 添加钻石(用于测试) */ public addTestDiamonds(amount: number = 100) { if (this.saveDataManager) { this.saveDataManager.addDiamonds(amount, 'test'); this.updateUI(); } } /** * 解锁关卡(用于测试) */ public unlockLevel(level: number) { if (this.saveDataManager) { // 直接设置玩家数据中的最大解锁关卡 const playerData = this.saveDataManager.getPlayerData(); if (level > playerData.maxUnlockedLevel) { playerData.maxUnlockedLevel = level; this.saveDataManager.savePlayerData(); this.updateUI(); } } } /** * 重置存档(用于测试) */ public resetSaveData() { if (this.saveDataManager) { this.saveDataManager.resetAllData(); this.updateUI(); } } /** * 获取存档管理器(供其他脚本使用) */ public getSaveDataManager(): SaveDataManager { return this.saveDataManager; } onDestroy() { // 清理按钮事件 if (this.battleButton) { const button = this.battleButton.getComponent(Button); if (button) { button.node.off(Button.EventType.CLICK, this.onBattleButtonClick, this); } } if (this.shopButton) { const button = this.shopButton.getComponent(Button); if (button) { button.node.off(Button.EventType.CLICK, this.onShopButtonClick, this); } } if (this.settingsButton) { const button = this.settingsButton.getComponent(Button); if (button) { button.node.off(Button.EventType.CLICK, this.onSettingsButtonClick, this); } } } }