MainUIControlller.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // MainUIController.ts
  2. import { _decorator, Component, Node, Button, Label } from 'cc';
  3. import { SaveDataManager } from '../../LevelSystem/SaveDataManager';
  4. import { GameManager, AppState } from '../../LevelSystem/GameManager';
  5. import { GameState } from '../../LevelSystem/IN_game';
  6. import { GameStartMove } from '../../Animations/GameStartMove';
  7. import { TopBarController } from '../TopBarController';
  8. import { MoneyAni } from '../../Animations/MoneyAni';
  9. import EventBus, { GameEvents } from '../../Core/EventBus';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('MainUIController')
  12. export class MainUIController extends Component {
  13. /* 奖励节点 */
  14. @property(Node) rewardMoneyNode: Node = null; // 左金币奖励
  15. @property(Node) rewardDiamondNode: Node = null; // 右钻石奖励
  16. @property(Label) levelNumberLabel: Label = null; // 第 X 关文本
  17. /* 升级 */
  18. @property(Button) upgradeBtn: Button = null; // 升级按钮
  19. @property(Node) upgradeCostLabel: Node = null; // 消耗金币数字
  20. @property(Node) upgradeHpLabel: Node = null; // "100>>1000"
  21. /* 主功能按钮 */
  22. @property(Node) battleBtn: Node = null; // 战斗
  23. // 底栏按钮由 NavBarController 统一管理,这里不再需要引用
  24. @property(Node) topArea: Node = null; // Canvas-001/TopArea
  25. /* UI节点引用 - 替换find查找 */
  26. @property(Node) mainUI: Node = null; // Canvas/MainUI
  27. @property(Node) gameUI: Node = null; // Canvas/GameLevelUI
  28. @property(Node) gameManagerNode: Node = null; // Canvas/GameLevelUI/GameManager
  29. @property(Node) navBarNode: Node = null; // Canvas/NavBar
  30. @property(Node) topBarNode: Node = null; // Canvas/TopBar
  31. @property(Node) cameraNode: Node = null; // Canvas/Camera
  32. /* 奖励动画系统 */
  33. @property(Node) moneyAniNode: Node = null; // MoneyAni节点
  34. private sdm: SaveDataManager = null;
  35. onLoad () {
  36. this.sdm = SaveDataManager.getInstance();
  37. this.sdm.initialize();
  38. this.bindButtons();
  39. this.refreshAll();
  40. // TopArea 默认隐藏,在点击战斗后再显示
  41. if (this.topArea) this.topArea.active = false;
  42. }
  43. /* 绑定按钮事件 */
  44. private bindButtons () {
  45. this.upgradeBtn?.node.on(Button.EventType.CLICK, this.upgradeWallHp, this);
  46. this.battleBtn?.on(Button.EventType.CLICK, this.onBattle, this);
  47. }
  48. /* ================= 业务逻辑 ================= */
  49. private upgradeWallHp () {
  50. // 直接使用SaveDataManager进行墙体升级
  51. if (!this.sdm.canUpgradeWall()) {
  52. console.log('无法升级墙体:条件不满足');
  53. return;
  54. }
  55. const cost = this.sdm.getWallUpgradeCost();
  56. if (!this.sdm.spendCoins(cost)) {
  57. console.log('无法升级墙体:金币不足');
  58. return;
  59. }
  60. if (!this.sdm.upgradeWallLevel()) {
  61. console.log('墙体升级失败');
  62. return;
  63. }
  64. console.log('墙体升级成功');
  65. // 刷新所有UI显示
  66. this.refreshAll();
  67. // 通过事件系统通知UI更新
  68. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  69. }
  70. private onBattle () {
  71. // 显示 TopArea(拖拽引用),避免使用 find()
  72. if (this.topArea) this.topArea.active = true;
  73. // 若上一关已完成则自动+1
  74. const lvl = this.sdm.getCurrentLevel();
  75. if (this.sdm.isLevelCompleted(lvl)) {
  76. const pd = this.sdm.getPlayerData();
  77. pd.currentLevel = lvl + 1;
  78. this.sdm.savePlayerData();
  79. }
  80. this.refreshLevelNumber();
  81. // 切场景 UI - 使用装饰器引用
  82. if (this.mainUI) this.mainUI.active = false;
  83. if (this.gameUI) this.gameUI.active = true;
  84. const gm = this.gameManagerNode?.getComponent(GameManager);
  85. // 设置应用状态为游戏中(会自动隐藏TopBar和NavBar)
  86. gm?.setAppState(AppState.IN_GAME);
  87. // 统一使用StartGame启动游戏流程
  88. const eventBus = EventBus.getInstance();
  89. eventBus.emit(GameEvents.GAME_START);
  90. gm?.loadCurrentLevelConfig();
  91. // 镜头下移动画现在已集成到StartGame流程中的slideUpFromBottom方法里
  92. }
  93. /* ================= 刷新 ================= */
  94. private refreshLevelNumber(){
  95. if (this.levelNumberLabel) this.levelNumberLabel.string = `第 ${this.sdm.getCurrentLevel()} 关`;
  96. }
  97. private refreshAll(){
  98. this.refreshLevelNumber();
  99. this.refreshUpgradeInfo();
  100. this.refreshRewardDisplay();
  101. }
  102. /** 刷新奖励显示 - 从JSON配置读取 */
  103. private async refreshRewardDisplay() {
  104. const currentLevel = this.sdm.getCurrentLevel();
  105. try {
  106. // 从SaveDataManager获取关卡奖励配置
  107. const rewards = await this.sdm.getLevelRewardsFromConfig(currentLevel);
  108. if (rewards) {
  109. // 更新金币奖励显示
  110. if (this.rewardMoneyNode) {
  111. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  112. if (coinLabel) {
  113. coinLabel.string = rewards.coins.toString();
  114. }
  115. }
  116. // 更新钻石奖励显示
  117. if (this.rewardDiamondNode) {
  118. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  119. if (diamondLabel) {
  120. diamondLabel.string = rewards.diamonds.toString();
  121. }
  122. }
  123. } else {
  124. console.warn(`无法获取关卡${currentLevel}的奖励配置,使用默认值`);
  125. // 使用默认奖励值
  126. this.setDefaultRewards();
  127. }
  128. } catch (error) {
  129. console.error('刷新奖励显示时出错:', error);
  130. this.setDefaultRewards();
  131. }
  132. }
  133. /** 设置默认奖励值 */
  134. private setDefaultRewards() {
  135. // 金币奖励默认值
  136. if (this.rewardMoneyNode) {
  137. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  138. if (coinLabel) {
  139. coinLabel.string = '50';
  140. }
  141. }
  142. // 钻石奖励默认值
  143. if (this.rewardDiamondNode) {
  144. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  145. if (diamondLabel) {
  146. diamondLabel.string = '5';
  147. }
  148. }
  149. }
  150. /** 刷新升级信息显示 */
  151. private refreshUpgradeInfo () {
  152. const costLbl = this.upgradeCostLabel?.getComponent(Label);
  153. const hpLbl = this.upgradeHpLabel?.getComponent(Label);
  154. if (!costLbl || !hpLbl) return;
  155. // 显示升级费用
  156. const cost = this.sdm.getWallUpgradeCost();
  157. costLbl.string = cost.toString();
  158. // 显示当前和下一级血量
  159. const currentLevel = this.sdm.getWallLevel();
  160. const currentHp = this.sdm.getPlayerData()?.wallBaseHealth || 100;
  161. // 计算下一级血量
  162. const wallHpMap: Record<number, number> = {
  163. 1: 100,
  164. 2: 1000,
  165. 3: 1200,
  166. 4: 1500,
  167. 5: 2000
  168. };
  169. const nextHp = wallHpMap[currentLevel + 1] || (100 + currentLevel * 200);
  170. hpLbl.string = `${currentHp}>>${nextHp}`;
  171. // 根据是否可以升级来设置按钮状态
  172. if (this.upgradeBtn) {
  173. this.upgradeBtn.interactable = this.sdm.canUpgradeWall();
  174. }
  175. }
  176. // 供外部(如 GameManager)调用的公共刷新接口
  177. public updateUI (): void {
  178. this.refreshAll();
  179. // 通过事件系统通知UI更新
  180. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  181. }
  182. /**
  183. * 游戏失败或成功返回MainUI后的UI状态管理
  184. * 隐藏Canvas-001并显示Canvas/TopBar和Canvas/NavBar
  185. */
  186. public onReturnToMainUI(): void {
  187. console.log('MainUIController.onReturnToMainUI 开始执行');
  188. // 设置应用状态为主菜单
  189. const gm = this.gameManagerNode?.getComponent(GameManager);
  190. gm?.setAppState(AppState.MAIN_MENU);
  191. // 隐藏 TopArea (Canvas-001)
  192. if (this.topArea) {
  193. this.topArea.active = false;
  194. console.log('TopArea (Canvas-001) 已隐藏');
  195. }
  196. // 显示主UI
  197. if (this.mainUI) {
  198. this.mainUI.active = true;
  199. console.log('MainUI 已显示');
  200. }
  201. // 隐藏游戏UI
  202. if (this.gameUI) {
  203. this.gameUI.active = false;
  204. console.log('GameUI 已隐藏');
  205. }
  206. // 显示顶部钱币栏 TopBar
  207. if (this.topBarNode) {
  208. this.topBarNode.active = true;
  209. console.log('TopBar 已显示');
  210. }
  211. // 显示底部导航栏 NavBar
  212. if (this.navBarNode) {
  213. this.navBarNode.active = true;
  214. console.log('NavBar 已显示');
  215. }
  216. // 刷新UI显示
  217. this.refreshAll();
  218. // 通过事件系统通知UI更新
  219. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  220. console.log('MainUIController.onReturnToMainUI 执行完成');
  221. }
  222. /**
  223. * 游戏成功返回MainUI并播放奖励动画
  224. */
  225. public onReturnToMainUIWithReward(): void {
  226. console.log('MainUIController.onReturnToMainUIWithReward 开始执行');
  227. // 先执行基本的UI状态管理
  228. this.onReturnToMainUI();
  229. // 延迟播放奖励动画,确保UI已经完全显示
  230. this.scheduleOnce(() => {
  231. this.playRewardAnimation();
  232. }, 0.5);
  233. }
  234. /**
  235. * 播放奖励动画
  236. */
  237. private async playRewardAnimation(): Promise<void> {
  238. console.log('MainUIController.playRewardAnimation 开始播放奖励动画');
  239. const currentLevel = this.sdm.getCurrentLevel();
  240. try {
  241. // 获取游戏管理器以判断游戏状态
  242. const gameManager = this.gameManagerNode?.getComponent(GameManager);
  243. const currentGameState = gameManager?.getCurrentGameState();
  244. const isGameSuccess = currentGameState === GameState.SUCCESS; // 使用游戏内状态枚举进行比较
  245. let rewards;
  246. if (isGameSuccess) {
  247. // 游戏成功,获取完整奖励
  248. console.log('游戏成功,获取完整奖励');
  249. rewards = this.sdm.getLastRewards();
  250. } else {
  251. // 游戏失败,直接获取已经计算好的失败奖励
  252. console.log('游戏失败,获取已计算的部分奖励');
  253. rewards = this.sdm.getLastRewards();
  254. // 记录波数完成情况用于调试
  255. const currentWave = gameManager?.getCurrentWave() || 0;
  256. const inGameManager = gameManager?.getInGameManager();
  257. const totalWaves = inGameManager?.levelWaves?.length || 1;
  258. const completedWaves = Math.max(0, currentWave - 1);
  259. console.log(`波数完成情况: ${completedWaves}/${totalWaves} = ${(completedWaves / totalWaves * 100).toFixed(1)}%`);
  260. console.log('计算出的失败奖励:', rewards);
  261. }
  262. if (rewards && (rewards.coins > 0 || rewards.diamonds > 0)) {
  263. // 使用MoneyAni播放奖励动画
  264. if (this.moneyAniNode) {
  265. const moneyAni = this.moneyAniNode.getComponent(MoneyAni);
  266. if (moneyAni) {
  267. moneyAni.playRewardAnimation(rewards.coins, rewards.diamonds, () => {
  268. console.log('奖励动画播放完成');
  269. });
  270. } else {
  271. console.error('MoneyAni组件未找到');
  272. // 使用静态方法作为备选
  273. MoneyAni.playReward(rewards.coins, rewards.diamonds);
  274. }
  275. } else {
  276. console.warn('MoneyAni节点未设置,使用静态方法播放动画');
  277. MoneyAni.playReward(rewards.coins, rewards.diamonds);
  278. }
  279. } else {
  280. console.log('当前关卡没有奖励或奖励为0');
  281. }
  282. } catch (error) {
  283. console.error('播放奖励动画时出错:', error);
  284. // 使用默认奖励
  285. MoneyAni.playReward(25, 2);
  286. }
  287. }
  288. /* =============== Util =============== */
  289. private format(n:number){ return n>=1000000? (n/1e6).toFixed(1)+'M' : n>=1000? (n/1e3).toFixed(1)+'K' : n.toString(); }
  290. }