MainUIControlller.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. console.log('[MainUIController] onLoad 开始执行');
  37. this.sdm = SaveDataManager.getInstance();
  38. this.sdm.initialize();
  39. console.log('[MainUIController] SaveDataManager 初始化完成');
  40. this.bindButtons();
  41. this.refreshAll();
  42. // TopArea 默认隐藏,在点击战斗后再显示
  43. if (this.topArea) this.topArea.active = false;
  44. console.log('[MainUIController] onLoad 执行完成');
  45. }
  46. /* 绑定按钮事件 */
  47. private bindButtons () {
  48. console.log('[MainUIController] bindButtons 开始执行');
  49. console.log('[MainUIController] upgradeBtn 状态:', this.upgradeBtn ? '已设置' : '未设置');
  50. console.log('[MainUIController] battleBtn 状态:', this.battleBtn ? '已设置' : '未设置');
  51. // 升级按钮绑定 - upgradeBtn是Button类型
  52. if (this.upgradeBtn) {
  53. console.log('[MainUIController] upgradeBtn.node:', this.upgradeBtn.node ? '存在' : '不存在');
  54. this.upgradeBtn.node.on(Button.EventType.CLICK, this.upgradeWallHp, this);
  55. console.log('[MainUIController] 升级按钮事件已绑定');
  56. } else {
  57. console.error('[MainUIController] upgradeBtn未设置,无法绑定升级事件');
  58. }
  59. // 战斗按钮绑定 - battleBtn是Node类型
  60. if (this.battleBtn) {
  61. this.battleBtn.on(Button.EventType.CLICK, this.onBattle, this);
  62. console.log('[MainUIController] 战斗按钮事件已绑定');
  63. } else {
  64. console.error('[MainUIController] battleBtn未设置,无法绑定战斗事件');
  65. }
  66. console.log('[MainUIController] bindButtons 执行完成');
  67. }
  68. /* ================= 业务逻辑 ================= */
  69. private upgradeWallHp () {
  70. console.log('[MainUIController] 升级墙体按钮被点击');
  71. // 检查SaveDataManager是否初始化
  72. if (!this.sdm) {
  73. console.error('[MainUIController] SaveDataManager未初始化');
  74. return;
  75. }
  76. // 打印当前状态用于调试
  77. const currentMoney = this.sdm.getMoney();
  78. const currentWallLevel = this.sdm.getWallLevel();
  79. const upgradeCost = this.sdm.getWallUpgradeCost();
  80. console.log(`[MainUIController] 当前状态: 金币=${currentMoney}, 墙体等级=${currentWallLevel}, 升级费用=${upgradeCost}`);
  81. // 检查墙体等级是否已达到最大值
  82. if (currentWallLevel >= 5) {
  83. console.log('[MainUIController] 墙体已达到最大等级');
  84. EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
  85. message: '墙体已达到最大等级',
  86. duration: 2.0
  87. });
  88. return;
  89. }
  90. // 检查金币是否足够
  91. if (currentMoney < upgradeCost) {
  92. console.log(`[MainUIController] 金币不足,需要${upgradeCost},当前${currentMoney}`);
  93. EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
  94. message: `金币不足,需要${upgradeCost}金币`,
  95. duration: 2.0
  96. });
  97. return;
  98. }
  99. // 执行升级
  100. if (!this.sdm.spendMoney(upgradeCost)) {
  101. console.log('[MainUIController] 扣除金币失败');
  102. return;
  103. }
  104. if (!this.sdm.upgradeWallLevel()) {
  105. console.log('[MainUIController] 墙体升级失败');
  106. return;
  107. }
  108. console.log('[MainUIController] 墙体升级成功');
  109. // 刷新所有UI显示
  110. this.refreshAll();
  111. // 通过事件系统通知UI更新
  112. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  113. // 通知墙体组件更新血量显示
  114. EventBus.getInstance().emit(GameEvents.WALL_HEALTH_CHANGED, {
  115. previousHealth: 0,
  116. currentHealth: this.sdm.getPlayerData()?.wallBaseHealth || 100,
  117. maxHealth: this.sdm.getPlayerData()?.wallBaseHealth || 100
  118. });
  119. }
  120. private onBattle () {
  121. // 显示 TopArea(拖拽引用),避免使用 find()
  122. if (this.topArea) this.topArea.active = true;
  123. // 若上一关已完成则自动+1
  124. const lvl = this.sdm.getCurrentLevel();
  125. if (this.sdm.isLevelCompleted(lvl)) {
  126. const pd = this.sdm.getPlayerData();
  127. pd.currentLevel = lvl + 1;
  128. this.sdm.savePlayerData();
  129. }
  130. this.refreshLevelNumber();
  131. // 切场景 UI - 使用装饰器引用
  132. if (this.mainUI) this.mainUI.active = false;
  133. if (this.gameUI) this.gameUI.active = true;
  134. const gm = this.gameManagerNode?.getComponent(GameManager);
  135. // 设置应用状态为游戏中(会自动隐藏TopBar和NavBar)
  136. gm?.setAppState(AppState.IN_GAME);
  137. // 统一使用StartGame启动游戏流程
  138. const eventBus = EventBus.getInstance();
  139. eventBus.emit(GameEvents.GAME_START);
  140. gm?.loadCurrentLevelConfig();
  141. // 镜头下移动画现在已集成到StartGame流程中的slideUpFromBottom方法里
  142. }
  143. /* ================= 刷新 ================= */
  144. private refreshLevelNumber(){
  145. if (this.levelNumberLabel) this.levelNumberLabel.string = `第 ${this.sdm.getCurrentLevel()} 关`;
  146. }
  147. private refreshAll(){
  148. this.refreshLevelNumber();
  149. this.refreshUpgradeInfo();
  150. this.refreshRewardDisplay();
  151. }
  152. /** 刷新奖励显示 - 从JSON配置读取 */
  153. private async refreshRewardDisplay() {
  154. const currentLevel = this.sdm.getCurrentLevel();
  155. try {
  156. // 从SaveDataManager获取关卡奖励配置
  157. const rewards = await this.sdm.getLevelRewardsFromConfig(currentLevel);
  158. if (rewards) {
  159. // 更新金币奖励显示
  160. if (this.rewardMoneyNode) {
  161. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  162. if (coinLabel) {
  163. coinLabel.string = rewards.coins.toString();
  164. }
  165. }
  166. // 更新钻石奖励显示
  167. if (this.rewardDiamondNode) {
  168. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  169. if (diamondLabel) {
  170. diamondLabel.string = rewards.diamonds.toString();
  171. }
  172. }
  173. } else {
  174. console.warn(`无法获取关卡${currentLevel}的奖励配置,使用默认值`);
  175. // 使用默认奖励值
  176. this.setDefaultRewards();
  177. }
  178. } catch (error) {
  179. console.error('刷新奖励显示时出错:', error);
  180. this.setDefaultRewards();
  181. }
  182. }
  183. /** 设置默认奖励值 */
  184. private setDefaultRewards() {
  185. // 金币奖励默认值
  186. if (this.rewardMoneyNode) {
  187. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  188. if (coinLabel) {
  189. coinLabel.string = '50';
  190. }
  191. }
  192. // 钻石奖励默认值
  193. if (this.rewardDiamondNode) {
  194. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  195. if (diamondLabel) {
  196. diamondLabel.string = '5';
  197. }
  198. }
  199. }
  200. /** 刷新升级信息显示 */
  201. private refreshUpgradeInfo () {
  202. console.log('[MainUIController] refreshUpgradeInfo 开始执行');
  203. const costLbl = this.upgradeCostLabel?.getComponent(Label);
  204. const hpLbl = this.upgradeHpLabel?.getComponent(Label);
  205. if (!costLbl || !hpLbl) {
  206. console.error('[MainUIController] 升级信息标签未找到:', { costLbl: !!costLbl, hpLbl: !!hpLbl });
  207. return;
  208. }
  209. // 显示升级费用
  210. const cost = this.sdm.getWallUpgradeCost();
  211. costLbl.string = cost.toString();
  212. console.log(`[MainUIController] 升级费用: ${cost}`);
  213. // 显示当前和下一级血量
  214. const currentLevel = this.sdm.getWallLevel();
  215. const currentHp = this.sdm.getPlayerData()?.wallBaseHealth || 100;
  216. // 计算下一级血量
  217. const wallHpMap: Record<number, number> = {
  218. 1: 100,
  219. 2: 1000,
  220. 3: 1200,
  221. 4: 1500,
  222. 5: 2000
  223. };
  224. const nextHp = wallHpMap[currentLevel + 1] || (100 + currentLevel * 200);
  225. hpLbl.string = `${currentHp}>>${nextHp}`;
  226. console.log(`[MainUIController] 血量显示: ${currentHp}>>${nextHp}, 当前等级: ${currentLevel}`);
  227. // 升级按钮始终保持可点击状态,通过Toast显示各种提示
  228. console.log(`[MainUIController] 升级按钮保持可交互状态`);
  229. }
  230. // 供外部(如 GameManager)调用的公共刷新接口
  231. public updateUI (): void {
  232. this.refreshAll();
  233. // 通过事件系统通知UI更新
  234. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  235. }
  236. /**
  237. * 游戏失败或成功返回MainUI后的UI状态管理
  238. * 隐藏Canvas-001并显示Canvas/TopBar和Canvas/NavBar
  239. */
  240. public onReturnToMainUI(): void {
  241. console.log('MainUIController.onReturnToMainUI 开始执行');
  242. // 设置应用状态为主菜单
  243. const gm = this.gameManagerNode?.getComponent(GameManager);
  244. gm?.setAppState(AppState.MAIN_MENU);
  245. // 隐藏 TopArea (Canvas-001)
  246. if (this.topArea) {
  247. this.topArea.active = false;
  248. console.log('TopArea (Canvas-001) 已隐藏');
  249. }
  250. // 显示主UI
  251. if (this.mainUI) {
  252. this.mainUI.active = true;
  253. console.log('MainUI 已显示');
  254. }
  255. // 隐藏游戏UI
  256. if (this.gameUI) {
  257. this.gameUI.active = false;
  258. console.log('GameUI 已隐藏');
  259. }
  260. // 显示顶部钱币栏 TopBar
  261. if (this.topBarNode) {
  262. this.topBarNode.active = true;
  263. console.log('TopBar 已显示');
  264. }
  265. // 显示底部导航栏 NavBar
  266. if (this.navBarNode) {
  267. this.navBarNode.active = true;
  268. console.log('NavBar 已显示');
  269. }
  270. // 刷新UI显示
  271. this.refreshAll();
  272. // 通过事件系统通知UI更新
  273. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  274. console.log('MainUIController.onReturnToMainUI 执行完成');
  275. }
  276. /**
  277. * 游戏成功返回MainUI并播放奖励动画
  278. */
  279. public onReturnToMainUIWithReward(): void {
  280. console.log('MainUIController.onReturnToMainUIWithReward 开始执行');
  281. // 先执行基本的UI状态管理
  282. this.onReturnToMainUI();
  283. // 延迟播放奖励动画,确保UI已经完全显示
  284. this.scheduleOnce(() => {
  285. this.playRewardAnimation();
  286. }, 0.5);
  287. }
  288. /**
  289. * 播放奖励动画
  290. */
  291. private async playRewardAnimation(): Promise<void> {
  292. console.log('MainUIController.playRewardAnimation 开始播放奖励动画');
  293. const currentLevel = this.sdm.getCurrentLevel();
  294. try {
  295. // 获取游戏管理器以判断游戏状态
  296. const gameManager = this.gameManagerNode?.getComponent(GameManager);
  297. const currentGameState = gameManager?.getCurrentGameState();
  298. const isGameSuccess = currentGameState === GameState.SUCCESS; // 使用游戏内状态枚举进行比较
  299. let rewards;
  300. if (isGameSuccess) {
  301. // 游戏成功,获取完整奖励
  302. console.log('游戏成功,获取完整奖励');
  303. rewards = this.sdm.getLastRewards();
  304. } else {
  305. // 游戏失败,直接获取已经计算好的失败奖励
  306. console.log('游戏失败,获取已计算的部分奖励');
  307. rewards = this.sdm.getLastRewards();
  308. // 记录波数完成情况用于调试
  309. const currentWave = gameManager?.getCurrentWave() || 0;
  310. const inGameManager = gameManager?.getInGameManager();
  311. const totalWaves = inGameManager?.levelWaves?.length || 1;
  312. const completedWaves = Math.max(0, currentWave - 1);
  313. console.log(`波数完成情况: ${completedWaves}/${totalWaves} = ${(completedWaves / totalWaves * 100).toFixed(1)}%`);
  314. console.log('计算出的失败奖励:', rewards);
  315. }
  316. if (rewards && (rewards.coins > 0 || rewards.diamonds > 0)) {
  317. // 使用MoneyAni播放奖励动画
  318. if (this.moneyAniNode) {
  319. const moneyAni = this.moneyAniNode.getComponent(MoneyAni);
  320. if (moneyAni) {
  321. moneyAni.playRewardAnimation(rewards.coins, rewards.diamonds, () => {
  322. console.log('奖励动画播放完成');
  323. });
  324. } else {
  325. console.error('MoneyAni组件未找到');
  326. // 使用静态方法作为备选
  327. MoneyAni.playReward(rewards.coins, rewards.diamonds);
  328. }
  329. } else {
  330. console.warn('MoneyAni节点未设置,使用静态方法播放动画');
  331. MoneyAni.playReward(rewards.coins, rewards.diamonds);
  332. }
  333. } else {
  334. console.log('当前关卡没有奖励或奖励为0');
  335. }
  336. } catch (error) {
  337. console.error('播放奖励动画时出错:', error);
  338. // 使用默认奖励
  339. MoneyAni.playReward(25, 2);
  340. }
  341. }
  342. /* =============== Util =============== */
  343. private format(n:number){ return n>=1000000? (n/1e6).toFixed(1)+'M' : n>=1000? (n/1e3).toFixed(1)+'K' : n.toString(); }
  344. }