MainUIControlller.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. TopBarController.updateTopBarUI();
  68. }
  69. private onBattle () {
  70. // 显示 TopArea(拖拽引用),避免使用 find()
  71. if (this.topArea) this.topArea.active = true;
  72. // 若上一关已完成则自动+1
  73. const lvl = this.sdm.getCurrentLevel();
  74. if (this.sdm.isLevelCompleted(lvl)) {
  75. const pd = this.sdm.getPlayerData();
  76. pd.currentLevel = lvl + 1;
  77. this.sdm.savePlayerData();
  78. }
  79. this.refreshLevelNumber();
  80. // 切场景 UI - 使用装饰器引用
  81. if (this.mainUI) this.mainUI.active = false;
  82. if (this.gameUI) this.gameUI.active = true;
  83. const gm = this.gameManagerNode?.getComponent(GameManager);
  84. // 设置应用状态为游戏中(会自动隐藏TopBar和NavBar)
  85. gm?.setAppState(AppState.IN_GAME);
  86. // 统一使用StartGame启动游戏流程
  87. const eventBus = EventBus.getInstance();
  88. eventBus.emit(GameEvents.GAME_START);
  89. gm?.loadCurrentLevelConfig();
  90. // 镜头下移动画现在已集成到StartGame流程中的slideUpFromBottom方法里
  91. }
  92. /* ================= 刷新 ================= */
  93. private refreshLevelNumber(){
  94. if (this.levelNumberLabel) this.levelNumberLabel.string = `第 ${this.sdm.getCurrentLevel()} 关`;
  95. }
  96. private refreshAll(){
  97. this.refreshLevelNumber();
  98. this.refreshUpgradeInfo();
  99. this.refreshRewardDisplay();
  100. }
  101. /** 刷新奖励显示 - 从JSON配置读取 */
  102. private async refreshRewardDisplay() {
  103. const currentLevel = this.sdm.getCurrentLevel();
  104. try {
  105. // 从SaveDataManager获取关卡奖励配置
  106. const rewards = await this.sdm.getLevelRewardsFromConfig(currentLevel);
  107. if (rewards) {
  108. // 更新金币奖励显示
  109. if (this.rewardMoneyNode) {
  110. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  111. if (coinLabel) {
  112. coinLabel.string = rewards.coins.toString();
  113. }
  114. }
  115. // 更新钻石奖励显示
  116. if (this.rewardDiamondNode) {
  117. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  118. if (diamondLabel) {
  119. diamondLabel.string = rewards.diamonds.toString();
  120. }
  121. }
  122. } else {
  123. console.warn(`无法获取关卡${currentLevel}的奖励配置,使用默认值`);
  124. // 使用默认奖励值
  125. this.setDefaultRewards();
  126. }
  127. } catch (error) {
  128. console.error('刷新奖励显示时出错:', error);
  129. this.setDefaultRewards();
  130. }
  131. }
  132. /** 设置默认奖励值 */
  133. private setDefaultRewards() {
  134. // 金币奖励默认值
  135. if (this.rewardMoneyNode) {
  136. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  137. if (coinLabel) {
  138. coinLabel.string = '50';
  139. }
  140. }
  141. // 钻石奖励默认值
  142. if (this.rewardDiamondNode) {
  143. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  144. if (diamondLabel) {
  145. diamondLabel.string = '5';
  146. }
  147. }
  148. }
  149. /** 刷新升级信息显示 */
  150. private refreshUpgradeInfo () {
  151. const costLbl = this.upgradeCostLabel?.getComponent(Label);
  152. const hpLbl = this.upgradeHpLabel?.getComponent(Label);
  153. if (!costLbl || !hpLbl) return;
  154. // 显示升级费用
  155. const cost = this.sdm.getWallUpgradeCost();
  156. costLbl.string = cost.toString();
  157. // 显示当前和下一级血量
  158. const currentLevel = this.sdm.getWallLevel();
  159. const currentHp = this.sdm.getPlayerData()?.wallBaseHealth || 100;
  160. // 计算下一级血量
  161. const wallHpMap: Record<number, number> = {
  162. 1: 100,
  163. 2: 1000,
  164. 3: 1200,
  165. 4: 1500,
  166. 5: 2000
  167. };
  168. const nextHp = wallHpMap[currentLevel + 1] || (100 + currentLevel * 200);
  169. hpLbl.string = `${currentHp}>>${nextHp}`;
  170. // 根据是否可以升级来设置按钮状态
  171. if (this.upgradeBtn) {
  172. this.upgradeBtn.interactable = this.sdm.canUpgradeWall();
  173. }
  174. }
  175. // 供外部(如 GameManager)调用的公共刷新接口
  176. public updateUI (): void {
  177. this.refreshAll();
  178. // 同时更新TopBar的UI
  179. TopBarController.updateTopBarUI();
  180. }
  181. /**
  182. * 游戏失败或成功返回MainUI后的UI状态管理
  183. * 隐藏Canvas-001并显示Canvas/TopBar和Canvas/NavBar
  184. */
  185. public onReturnToMainUI(): void {
  186. console.log('MainUIController.onReturnToMainUI 开始执行');
  187. // 设置应用状态为主菜单
  188. const gm = this.gameManagerNode?.getComponent(GameManager);
  189. gm?.setAppState(AppState.MAIN_MENU);
  190. // 隐藏 TopArea (Canvas-001)
  191. if (this.topArea) {
  192. this.topArea.active = false;
  193. console.log('TopArea (Canvas-001) 已隐藏');
  194. }
  195. // 显示主UI
  196. if (this.mainUI) {
  197. this.mainUI.active = true;
  198. console.log('MainUI 已显示');
  199. }
  200. // 隐藏游戏UI
  201. if (this.gameUI) {
  202. this.gameUI.active = false;
  203. console.log('GameUI 已隐藏');
  204. }
  205. // 显示顶部钱币栏 TopBar
  206. if (this.topBarNode) {
  207. this.topBarNode.active = true;
  208. console.log('TopBar 已显示');
  209. }
  210. // 显示底部导航栏 NavBar
  211. if (this.navBarNode) {
  212. this.navBarNode.active = true;
  213. console.log('NavBar 已显示');
  214. }
  215. // 刷新UI显示
  216. this.refreshAll();
  217. TopBarController.updateTopBarUI();
  218. console.log('MainUIController.onReturnToMainUI 执行完成');
  219. }
  220. /**
  221. * 游戏成功返回MainUI并播放奖励动画
  222. */
  223. public onReturnToMainUIWithReward(): void {
  224. console.log('MainUIController.onReturnToMainUIWithReward 开始执行');
  225. // 先执行基本的UI状态管理
  226. this.onReturnToMainUI();
  227. // 延迟播放奖励动画,确保UI已经完全显示
  228. this.scheduleOnce(() => {
  229. this.playRewardAnimation();
  230. }, 0.5);
  231. }
  232. /**
  233. * 播放奖励动画
  234. */
  235. private async playRewardAnimation(): Promise<void> {
  236. console.log('MainUIController.playRewardAnimation 开始播放奖励动画');
  237. const currentLevel = this.sdm.getCurrentLevel();
  238. try {
  239. // 获取游戏管理器以判断游戏状态
  240. const gameManager = this.gameManagerNode?.getComponent(GameManager);
  241. const currentGameState = gameManager?.getCurrentGameState();
  242. const isGameSuccess = currentGameState === GameState.SUCCESS; // 使用游戏内状态枚举进行比较
  243. let rewards;
  244. if (isGameSuccess) {
  245. // 游戏成功,获取完整奖励
  246. console.log('游戏成功,获取完整奖励');
  247. rewards = await this.sdm.getLevelRewardsFromConfig(currentLevel);
  248. } else {
  249. // 游戏失败,计算部分奖励
  250. console.log('游戏失败,计算部分奖励');
  251. const baseRewards = await this.sdm.getLevelRewardsFromConfig(currentLevel);
  252. // 计算波数完成比例
  253. const currentWave = gameManager?.getCurrentWave() || 0;
  254. const inGameManager = gameManager?.getInGameManager();
  255. const totalWaves = inGameManager?.levelWaves?.length || 1;
  256. const completionRatio = Math.max(0, Math.min(1, Math.max(0, currentWave - 1) / totalWaves));
  257. console.log(`波数完成情况: ${Math.max(0, currentWave - 1)}/${totalWaves} = ${(completionRatio * 100).toFixed(1)}%`);
  258. // 失败奖励:基础奖励 × 完成比例 × 失败系数
  259. rewards = {
  260. coins: Math.floor((baseRewards?.coins || 100) * completionRatio * 0.5), // 50%系数
  261. diamonds: Math.floor((baseRewards?.diamonds || 10) * completionRatio * 0.3) // 30%系数
  262. };
  263. console.log('计算出的失败奖励:', rewards);
  264. }
  265. if (rewards && (rewards.coins > 0 || rewards.diamonds > 0)) {
  266. // 使用MoneyAni播放奖励动画
  267. if (this.moneyAniNode) {
  268. const moneyAni = this.moneyAniNode.getComponent(MoneyAni);
  269. if (moneyAni) {
  270. moneyAni.playRewardAnimation(rewards.coins, rewards.diamonds, () => {
  271. console.log('奖励动画播放完成');
  272. });
  273. } else {
  274. console.error('MoneyAni组件未找到');
  275. // 使用静态方法作为备选
  276. MoneyAni.playReward(rewards.coins, rewards.diamonds);
  277. }
  278. } else {
  279. console.warn('MoneyAni节点未设置,使用静态方法播放动画');
  280. MoneyAni.playReward(rewards.coins, rewards.diamonds);
  281. }
  282. } else {
  283. console.log('当前关卡没有奖励或奖励为0');
  284. }
  285. } catch (error) {
  286. console.error('播放奖励动画时出错:', error);
  287. // 使用默认奖励
  288. MoneyAni.playReward(25, 2);
  289. }
  290. }
  291. /* =============== Util =============== */
  292. private format(n:number){ return n>=1000000? (n/1e6).toFixed(1)+'M' : n>=1000? (n/1e3).toFixed(1)+'K' : n.toString(); }
  293. }