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