MainUIController.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // MainUIController.ts
  2. import { _decorator, Component, Node, Button, Label, find } from 'cc';
  3. import { SaveDataManager } from './SaveDataManager';
  4. import { GameManager } from './GameManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('MainUIController')
  7. export class MainUIController extends Component {
  8. /* 顶部资源 & 关卡 */
  9. @property(Node) moneyAddBtn: Node = null; // 绿色 +
  10. @property(Node) diamondAddBtn: Node = null; // 紫色 +
  11. @property(Node) moneyLabel: Node = null; // 金币数字
  12. @property(Node) diamondLabel: Node = null; // 钻石数字
  13. @property(Label) levelNumberLabel: Label = null; // 第 X 关文本
  14. /* 奖励节点 */
  15. @property(Node) rewardMoneyNode: Node = null; // 左金币奖励
  16. @property(Node) rewardDiamondNode: Node = null; // 右钻石奖励
  17. /* 升级 */
  18. @property(Button) upgradeBtn: Button = null; // 升级按钮
  19. @property(Node) upgradeCostLabel: Node = null; // 消耗金币数字
  20. @property(Node) upgradeHpLabel: Node = null; // "105>>1000"
  21. /* 主功能按钮 */
  22. @property(Node) battleBtn: Node = null; // 战斗
  23. @property(Node) shopBtn: Node = null; // 商店
  24. @property(Node) navUpgradeBtn: Node = null; // 底栏 UPGRADE
  25. @property(Node) navBattleBtn: Node = null; // 底栏 BATTLE
  26. @property(Node) navSkillBtn: Node = null; // 底栏 SKILL
  27. private sdm: SaveDataManager = null;
  28. onLoad () {
  29. this.sdm = SaveDataManager.getInstance();
  30. this.sdm.initialize();
  31. this.bindButtons();
  32. this.refreshAll();
  33. }
  34. /* 绑定按钮事件 */
  35. private bindButtons () {
  36. this.moneyAddBtn?.on(Button.EventType.CLICK, () => this.addCoins(100), this);
  37. this.diamondAddBtn?.on(Button.EventType.CLICK, () => this.addDiamonds(20), this);
  38. this.rewardMoneyNode?.on(Node.EventType.TOUCH_END, () => this.takeRewardCoins(), this);
  39. this.rewardDiamondNode?.on(Node.EventType.TOUCH_END, () => this.takeRewardDiamonds(), this);
  40. this.upgradeBtn?.node.on(Button.EventType.CLICK, this.upgradeWallHp, this);
  41. this.battleBtn?.on(Button.EventType.CLICK, this.onBattle, this);
  42. this.shopBtn?.on(Button.EventType.CLICK, this.onShop, this);
  43. // 底栏按钮暂只打印
  44. this.navUpgradeBtn?.on(Button.EventType.CLICK, () => console.log('底栏 UPGRADE'), this);
  45. this.navBattleBtn?.on(Button.EventType.CLICK, () => console.log('底栏 BATTLE'), this);
  46. this.navSkillBtn?.on(Button.EventType.CLICK, () => console.log('底栏 SKILL'), this);
  47. }
  48. /* ================= 业务逻辑 ================= */
  49. private addCoins (v:number) { this.sdm.addCoins(v, 'ad'); this.refreshCurrency(); }
  50. private addDiamonds (v:number){ this.sdm.addDiamonds(v,'ad'); this.refreshCurrency(); }
  51. private takeRewardCoins () {
  52. const lbl = this.rewardMoneyNode?.getComponent(Label) || this.rewardMoneyNode?.getComponentInChildren(Label);
  53. if (!lbl) return;
  54. const amt = parseInt(lbl.string||'0');
  55. if (amt>0) { this.addCoins(amt); this.rewardMoneyNode.active = false; }
  56. }
  57. private takeRewardDiamonds () {
  58. const lbl = this.rewardDiamondNode?.getComponent(Label) || this.rewardDiamondNode?.getComponentInChildren(Label);
  59. if (!lbl) return;
  60. const amt = parseInt(lbl.string||'0');
  61. if (amt>0) { this.addDiamonds(amt); this.rewardDiamondNode.active = false; }
  62. }
  63. private upgradeWallHp () {
  64. const costLbl = this.upgradeCostLabel?.getComponent(Label);
  65. if (!costLbl) return;
  66. const cost = parseInt(costLbl.string || '0');
  67. if (!this.sdm.spendCoins(cost)) return; // 金币不足
  68. // 升级墙体
  69. const gmNode = find('Canvas/GameLevelUI/GameManager');
  70. const gm = gmNode?.getComponent(GameManager);
  71. if (!gm) return;
  72. const info = gm.upgradeWallLevel?.();
  73. if (!info) return;
  74. // 更新显示
  75. const hpLbl = this.upgradeHpLabel?.getComponent(Label);
  76. if (hpLbl) {
  77. hpLbl.string = `${info.currentHp}>>${info.nextHp}`;
  78. }
  79. this.refreshCurrency();
  80. }
  81. private onBattle () {
  82. // 若上一关已完成则自动+1
  83. const lvl = this.sdm.getCurrentLevel();
  84. if (this.sdm.isLevelCompleted(lvl)) {
  85. const pd = this.sdm.getPlayerData();
  86. pd.currentLevel = lvl + 1;
  87. this.sdm.savePlayerData();
  88. }
  89. this.refreshLevelNumber();
  90. // 切场景 UI
  91. const mainUI = find('Canvas/MainUI');
  92. const gameUI = find('Canvas/GameLevelUI');
  93. if (mainUI) mainUI.active = false;
  94. if (gameUI) gameUI.active = true;
  95. const gmNode = find('Canvas/GameLevelUI/GameManager');
  96. const gm = gmNode?.getComponent(GameManager);
  97. gm?.restartGame();
  98. gm?.loadCurrentLevelConfig();
  99. }
  100. private onShop () {
  101. const mainUI = find('Canvas/MainUI');
  102. const shopUI = find('Canvas/ShopUI');
  103. if (mainUI) mainUI.active = false;
  104. if (shopUI) shopUI.active = true;
  105. }
  106. /* ================= 刷新 ================= */
  107. private refreshCurrency () {
  108. const mLbl = this.moneyLabel?.getComponent(Label) || this.moneyLabel as unknown as Label;
  109. if (mLbl) mLbl.string = this.format(this.sdm.getCoins());
  110. const dLbl = this.diamondLabel?.getComponent(Label) || this.diamondLabel as unknown as Label;
  111. if (dLbl) dLbl.string = this.format(this.sdm.getDiamonds());
  112. }
  113. private refreshLevelNumber(){
  114. if (this.levelNumberLabel) this.levelNumberLabel.string = `第 ${this.sdm.getCurrentLevel()} 关`;
  115. }
  116. private refreshAll(){
  117. this.refreshCurrency();
  118. this.refreshLevelNumber();
  119. this.refreshUpgradeInfo();
  120. }
  121. /** 刷新升级信息显示 */
  122. private refreshUpgradeInfo () {
  123. const gmNode = find('Canvas/GameLevelUI/GameManager');
  124. const gm = gmNode?.getComponent(GameManager);
  125. const hpLbl = this.upgradeHpLabel?.getComponent(Label);
  126. if (!gm || !hpLbl) return;
  127. const curHp = gm.getCurrentWallHealth?.() || 0;
  128. const nextHp = gm.getWallHealthByLevel?.(gm.getCurrentWallLevel?.() + 1) || curHp;
  129. hpLbl.string = `${curHp}>>${nextHp}`;
  130. }
  131. // 供外部(如 GameManager)调用的公共刷新接口
  132. public updateUI (): void {
  133. this.refreshAll();
  134. }
  135. /* =============== Util =============== */
  136. private format(n:number){ return n>=1000000? (n/1e6).toFixed(1)+'M' : n>=1000? (n/1e3).toFixed(1)+'K' : n.toString(); }
  137. }