|
|
@@ -1,372 +1,168 @@
|
|
|
-import { _decorator, Component, Node, Label, Button, find } from 'cc';
|
|
|
+// MainUIController.ts
|
|
|
+import { _decorator, Component, Node, Button, Label, 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();
|
|
|
- }
|
|
|
+ /* 顶部资源 & 关卡 */
|
|
|
+ @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 关文本
|
|
|
|
|
|
- // 切换 UI:隐藏主界面,显示关卡界面
|
|
|
- const mainUI = find('Canvas/MainUI');
|
|
|
- const gameLevelUI = find('Canvas/GameLevelUI');
|
|
|
- if (mainUI) mainUI.active = false;
|
|
|
- if (gameLevelUI) gameLevelUI.active = true;
|
|
|
+ /* 奖励节点 */
|
|
|
+ @property(Node) rewardMoneyNode: Node = null; // 左金币奖励
|
|
|
+ @property(Node) rewardDiamondNode: Node = null; // 右钻石奖励
|
|
|
|
|
|
- // 重置并加载关卡
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
+ /* 升级 */
|
|
|
+ @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; // 商店
|
|
|
+ @property(Node) navUpgradeBtn: Node = null; // 底栏 UPGRADE
|
|
|
+ @property(Node) navBattleBtn: Node = null; // 底栏 BATTLE
|
|
|
+ @property(Node) navSkillBtn: Node = null; // 底栏 SKILL
|
|
|
+
|
|
|
+ private sdm: SaveDataManager = null;
|
|
|
+
|
|
|
+ onLoad () {
|
|
|
+ this.sdm = SaveDataManager.getInstance();
|
|
|
+ this.sdm.initialize();
|
|
|
+
|
|
|
+ this.bindButtons();
|
|
|
+ this.refreshAll();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /* 绑定按钮事件 */
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // 底栏按钮暂只打印
|
|
|
+ this.navUpgradeBtn?.on(Button.EventType.CLICK, () => console.log('底栏 UPGRADE'), this);
|
|
|
+ this.navBattleBtn?.on(Button.EventType.CLICK, () => console.log('底栏 BATTLE'), this);
|
|
|
+ this.navSkillBtn?.on(Button.EventType.CLICK, () => console.log('底栏 SKILL'), this);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ================= 业务逻辑 ================= */
|
|
|
+ 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 () {
|
|
|
+ // 若上一关已完成则自动+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();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(); }
|
|
|
+}
|