MainUIControlller.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // MainUIController.ts
  2. import { _decorator, Component, Node, Button, Label, JsonAsset, resources } 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. import { Audio } from '../../AudioManager/AudioManager';
  11. import { BundleLoader } from '../../Core/BundleLoader';
  12. import { ResourcePreloader } from '../../Core/ResourcePreloader';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('MainUIController')
  15. export class MainUIController extends Component {
  16. /* 奖励节点 */
  17. @property(Node) rewardMoneyNode: Node = null; // 左钞票奖励
  18. @property(Node) rewardDiamondNode: Node = null; // 右钻石奖励
  19. @property(Label) levelNumberLabel: Label = null; // 第 X 关文本
  20. /* 升级 */
  21. @property(Button) upgradeBtn: Button = null; // 升级按钮
  22. @property(Node) upgradeCostLabel: Node = null; // 消耗钞票数字
  23. @property(Node) upgradeHpLabel: Node = null; // 升级后血量数字
  24. @property(Label) upgradeCurrentHpLabel: Label = null; // 当前血量Label
  25. @property(Label) upgradeNextHpLabel: Label = null; // 升级后血量Label
  26. /* 墙体配置 */
  27. // @property(JsonAsset) wallConfigAsset: JsonAsset = null; // 墙体配置JSON资源 - 已改为动态加载
  28. /* 主功能按钮 */
  29. @property(Node) battleBtn: Node = null; // 战斗
  30. // 底栏按钮由 NavBarController 统一管理,这里不再需要引用
  31. @property(Node) topArea: Node = null; // Canvas-001/TopArea
  32. /* UI节点引用 - 替换find查找 */
  33. @property(Node) mainUI: Node = null; // Canvas/MainUI
  34. @property(Node) gameUI: Node = null; // Canvas/GameLevelUI
  35. @property(Node) gameManagerNode: Node = null; // Canvas/GameLevelUI/GameManager
  36. @property(Node) navBarNode: Node = null; // Canvas/NavBar
  37. @property(Node) topBarNode: Node = null; // Canvas/TopBar
  38. @property(Node) cameraNode: Node = null; // Canvas/Camera
  39. /* 奖励动画系统 */
  40. @property(Node) moneyAniNode: Node = null; // MoneyAni节点
  41. private sdm: SaveDataManager = null;
  42. private wallConfig: any = null;
  43. async onLoad () {
  44. console.log('[MainUIController] onLoad 开始执行');
  45. this.sdm = SaveDataManager.getInstance();
  46. await this.loadWallConfig();
  47. this.sdm.initialize();
  48. console.log('[MainUIController] SaveDataManager 初始化完成');
  49. this.bindButtons();
  50. this.refreshAll();
  51. // TopArea 默认隐藏,在点击战斗后再显示
  52. if (this.topArea) this.topArea.active = false;
  53. // 播放主界面背景音乐(等待bundle加载完成)
  54. await this.playMainUIBGM();
  55. // 预加载当前关卡和下一关的资源
  56. this.preloadLevelResources();
  57. console.log('[MainUIController] onLoad 执行完成');
  58. }
  59. /* 绑定按钮事件 */
  60. private bindButtons () {
  61. console.log('[MainUIController] bindButtons 开始执行');
  62. console.log('[MainUIController] upgradeBtn 状态:', this.upgradeBtn ? '已设置' : '未设置');
  63. console.log('[MainUIController] battleBtn 状态:', this.battleBtn ? '已设置' : '未设置');
  64. // 升级按钮绑定 - upgradeBtn是Button类型
  65. if (this.upgradeBtn) {
  66. console.log('[MainUIController] upgradeBtn.node:', this.upgradeBtn.node ? '存在' : '不存在');
  67. this.upgradeBtn.node.on(Button.EventType.CLICK, this.upgradeWallHp, this);
  68. console.log('[MainUIController] 升级按钮事件已绑定');
  69. } else {
  70. console.error('[MainUIController] upgradeBtn未设置,无法绑定升级事件');
  71. }
  72. // 战斗按钮绑定 - battleBtn是Node类型
  73. if (this.battleBtn) {
  74. this.battleBtn.on(Button.EventType.CLICK, this.onBattle, this);
  75. console.log('[MainUIController] 战斗按钮事件已绑定');
  76. } else {
  77. console.error('[MainUIController] battleBtn未设置,无法绑定战斗事件');
  78. }
  79. console.log('[MainUIController] bindButtons 执行完成');
  80. }
  81. /* ================= 配置加载 ================= */
  82. /**
  83. * 加载墙体配置
  84. */
  85. private async loadWallConfig(): Promise<void> {
  86. try {
  87. const bundleLoader = BundleLoader.getInstance();
  88. const wallData = await bundleLoader.loadDataJson('wall');
  89. if (!wallData) {
  90. console.error('[MainUIController] 墙体配置文件内容为空');
  91. return;
  92. }
  93. this.wallConfig = wallData.json;
  94. console.log('[MainUIController] 墙体配置加载成功:', this.wallConfig);
  95. // 将配置传递给SaveDataManager
  96. this.sdm.setWallConfig(this.wallConfig);
  97. } catch (error) {
  98. console.error('[MainUIController] 加载墙体配置失败:', error);
  99. }
  100. }
  101. /**
  102. * 获取墙体升级费用
  103. */
  104. private getWallUpgradeCost(level: number): number {
  105. if (this.wallConfig && this.wallConfig.wallConfig && this.wallConfig.wallConfig.upgradeCosts) {
  106. const costs = this.wallConfig.wallConfig.upgradeCosts;
  107. return costs[level.toString()] || 0;
  108. }
  109. return 0;
  110. }
  111. /**
  112. * 获取墙体血量
  113. */
  114. private getWallHealthByLevel(level: number): number {
  115. if (this.wallConfig && this.wallConfig.wallConfig && this.wallConfig.wallConfig.healthByLevel) {
  116. const healthByLevel = this.wallConfig.wallConfig.healthByLevel;
  117. return healthByLevel[level.toString()] || 100;
  118. }
  119. return 100;
  120. }
  121. /**
  122. * 获取墙体最大等级
  123. */
  124. private getWallMaxLevel(): number {
  125. if (this.wallConfig && this.wallConfig.wallConfig && this.wallConfig.wallConfig.maxLevel) {
  126. return this.wallConfig.wallConfig.maxLevel;
  127. }
  128. return 5;
  129. }
  130. /* ================= 业务逻辑 ================= */
  131. private upgradeWallHp () {
  132. // 播放UI点击音效
  133. Audio.playUISound('data/弹球音效/level up 2');
  134. console.log('[MainUIController] 升级墙体按钮被点击');
  135. // 检查SaveDataManager是否初始化
  136. if (!this.sdm) {
  137. console.error('[MainUIController] SaveDataManager未初始化');
  138. return;
  139. }
  140. // 打印当前状态用于调试
  141. const currentMoney = this.sdm.getMoney();
  142. const currentWallLevel = this.sdm.getWallLevel();
  143. const upgradeCost = this.getWallUpgradeCost(currentWallLevel);
  144. console.log(`[MainUIController] 当前状态: 钞票=${currentMoney}, 墙体等级=${currentWallLevel}, 升级费用=${upgradeCost}`);
  145. // 检查墙体等级是否已达到最大值
  146. const maxLevel = this.getWallMaxLevel();
  147. if (currentWallLevel >= maxLevel) {
  148. console.log('[MainUIController] 墙体已达到最大等级');
  149. EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
  150. message: '墙体已达到最大等级',
  151. duration: 2.0
  152. });
  153. return;
  154. }
  155. // 检查钞票是否足够
  156. if (currentMoney < upgradeCost) {
  157. EventBus.getInstance().emit(GameEvents.SHOW_RESOURCE_TOAST, {
  158. message: `钞票不足,需要${upgradeCost}钞票`,
  159. duration: 2.0
  160. });
  161. return;
  162. }
  163. // 执行升级
  164. if (!this.sdm.spendMoney(upgradeCost)) {
  165. console.log('[MainUIController] 扣除钞票失败');
  166. return;
  167. }
  168. if (!this.sdm.upgradeWallLevel()) {
  169. console.log('[MainUIController] 墙体升级失败');
  170. return;
  171. }
  172. console.log('[MainUIController] 墙体升级成功');
  173. // 刷新所有UI显示
  174. this.refreshAll();
  175. // 通过事件系统通知UI更新
  176. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  177. // 通知墙体组件更新血量显示
  178. const newLevel = this.sdm.getWallLevel();
  179. const newHealth = this.getWallHealthByLevel(newLevel);
  180. EventBus.getInstance().emit(GameEvents.WALL_HEALTH_CHANGED, {
  181. previousHealth: 0,
  182. currentHealth: newHealth,
  183. maxHealth: newHealth
  184. });
  185. }
  186. private onBattle () {
  187. // 停止主界面背景音乐,避免与游戏内音效冲突
  188. Audio.stopMusic();
  189. // 播放战斗背景音乐,音量较小
  190. setTimeout(() => {
  191. Audio.playMusic('data/弹球音效/fight bgm', true);
  192. Audio.setMusicVolume(0.4); // 设置较小的音量
  193. console.log('[MainUIController] 开始播放战斗背景音乐');
  194. }, 500); // 延迟500ms等start zombie音效播放
  195. // 显示 TopArea(拖拽引用),避免使用 find()
  196. if (this.topArea) this.topArea.active = true;
  197. // 若上一关已完成则自动+1
  198. const lvl = this.sdm.getCurrentLevel();
  199. if (this.sdm.isLevelCompleted(lvl)) {
  200. const pd = this.sdm.getPlayerData();
  201. pd.currentLevel = lvl + 1;
  202. this.sdm.savePlayerData();
  203. }
  204. this.refreshLevelNumber();
  205. // 切场景 UI - 使用装饰器引用
  206. if (this.mainUI) this.mainUI.active = false;
  207. if (this.gameUI) this.gameUI.active = true;
  208. const gm = this.gameManagerNode?.getComponent(GameManager);
  209. // 设置应用状态为游戏中(会自动隐藏TopBar和NavBar)
  210. gm?.setAppState(AppState.IN_GAME);
  211. // 统一使用StartGame启动游戏流程
  212. const eventBus = EventBus.getInstance();
  213. eventBus.emit(GameEvents.GAME_START);
  214. gm?.loadCurrentLevelConfig();
  215. // 镜头下移动画现在已集成到StartGame流程中的slideUpFromBottom方法里
  216. }
  217. /* ================= 刷新 ================= */
  218. private refreshLevelNumber(){
  219. if (this.levelNumberLabel) this.levelNumberLabel.string = `第 ${this.sdm.getCurrentLevel()} 关`;
  220. }
  221. private refreshAll(){
  222. this.refreshLevelNumber();
  223. this.refreshUpgradeInfo();
  224. this.refreshRewardDisplay();
  225. }
  226. /** 刷新奖励显示 - 从JSON配置读取 */
  227. private async refreshRewardDisplay() {
  228. const currentLevel = this.sdm.getCurrentLevel();
  229. try {
  230. // 从SaveDataManager获取关卡奖励配置
  231. const rewards = await this.sdm.getLevelRewardsFromConfig(currentLevel);
  232. if (rewards) {
  233. // 更新钞票奖励显示
  234. if (this.rewardMoneyNode) {
  235. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  236. if (coinLabel) {
  237. coinLabel.string = rewards.money.toString();
  238. }
  239. }
  240. // 更新钻石奖励显示
  241. if (this.rewardDiamondNode) {
  242. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  243. if (diamondLabel) {
  244. diamondLabel.string = rewards.diamonds.toString();
  245. }
  246. }
  247. } else {
  248. console.warn(`无法获取关卡${currentLevel}的奖励配置,使用默认值`);
  249. // 使用默认奖励值
  250. this.setDefaultRewards();
  251. }
  252. } catch (error) {
  253. console.error('刷新奖励显示时出错:', error);
  254. this.setDefaultRewards();
  255. }
  256. }
  257. /** 设置默认奖励值 */
  258. private setDefaultRewards() {
  259. // 钞票奖励默认值
  260. if (this.rewardMoneyNode) {
  261. const coinLabel = this.rewardMoneyNode.getComponent(Label) || this.rewardMoneyNode.getComponentInChildren(Label);
  262. if (coinLabel) {
  263. coinLabel.string = '50';
  264. }
  265. }
  266. // 钻石奖励默认值
  267. if (this.rewardDiamondNode) {
  268. const diamondLabel = this.rewardDiamondNode.getComponent(Label) || this.rewardDiamondNode.getComponentInChildren(Label);
  269. if (diamondLabel) {
  270. diamondLabel.string = '5';
  271. }
  272. }
  273. }
  274. /** 刷新升级信息显示 */
  275. private refreshUpgradeInfo () {
  276. console.log('[MainUIController] refreshUpgradeInfo 开始执行');
  277. const costLbl = this.upgradeCostLabel?.getComponent(Label);
  278. const hpLbl = this.upgradeHpLabel?.getComponent(Label);
  279. // 显示升级费用
  280. const currentLevel = this.sdm.getWallLevel();
  281. const cost = this.getWallUpgradeCost(currentLevel);
  282. if (costLbl) {
  283. costLbl.string = cost.toString();
  284. console.log(`[MainUIController] 升级费用: ${cost}`);
  285. }
  286. // 显示当前和下一级血量
  287. const currentHp = this.getWallHealthByLevel(currentLevel);
  288. // 计算下一级血量 - 使用配置数据
  289. const nextHp = this.getWallHealthByLevel(currentLevel + 1);
  290. // 如果有新的分离Label,使用分离显示
  291. if (this.upgradeCurrentHpLabel && this.upgradeNextHpLabel) {
  292. this.upgradeCurrentHpLabel.string = `${currentHp}>>`;
  293. this.upgradeNextHpLabel.string = `${nextHp}`;
  294. console.log(`[MainUIController] 分离血量显示: 当前=${currentHp}, 升级后=${nextHp}, 当前等级: ${currentLevel}`);
  295. }else {
  296. console.error('[MainUIController] 升级信息标签未找到');
  297. return;
  298. }
  299. // 升级按钮始终保持可点击状态,通过Toast显示各种提示
  300. console.log(`[MainUIController] 升级按钮保持可交互状态`);
  301. }
  302. // 供外部(如 GameManager)调用的公共刷新接口
  303. public updateUI (): void {
  304. this.refreshAll();
  305. // 通过事件系统通知UI更新
  306. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  307. }
  308. /**
  309. * 游戏失败或成功返回MainUI后的UI状态管理
  310. * 隐藏Canvas-001并显示Canvas/TopBar和Canvas/NavBar
  311. */
  312. public onReturnToMainUI(): void {
  313. console.log('MainUIController.onReturnToMainUI 开始执行');
  314. // 注意:应用状态已在GameManager中设置,这里不需要重复设置
  315. // 隐藏 TopArea (Canvas-001)
  316. if (this.topArea) {
  317. this.topArea.active = false;
  318. console.log('TopArea (Canvas-001) 已隐藏');
  319. }
  320. // 显示主UI
  321. if (this.mainUI) {
  322. this.mainUI.active = true;
  323. console.log('MainUI 已显示');
  324. }
  325. // 隐藏游戏UI
  326. if (this.gameUI) {
  327. this.gameUI.active = false;
  328. console.log('GameUI 已隐藏');
  329. }
  330. // 显示顶部钱币栏 TopBar
  331. if (this.topBarNode) {
  332. this.topBarNode.active = true;
  333. console.log('TopBar 已显示');
  334. }
  335. // 显示底部导航栏 NavBar
  336. if (this.navBarNode) {
  337. this.navBarNode.active = true;
  338. console.log('NavBar 已显示');
  339. }
  340. // 播放主界面背景音乐
  341. this.playMainUIBGM();
  342. // 刷新UI显示
  343. this.refreshAll();
  344. // 通过事件系统通知UI更新
  345. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  346. // 返回主界面时重新预加载资源,确保下次进入游戏时资源已准备就绪
  347. this.preloadLevelResources();
  348. console.log('MainUIController.onReturnToMainUI 执行完成');
  349. }
  350. /**
  351. * 游戏成功返回MainUI并播放奖励动画
  352. */
  353. public onReturnToMainUIWithReward(): void {
  354. console.log('MainUIController.onReturnToMainUIWithReward 开始执行');
  355. // 先执行基本的UI状态管理
  356. this.onReturnToMainUI();
  357. // 延迟播放奖励动画,确保UI已经完全显示
  358. this.scheduleOnce(() => {
  359. this.playRewardAnimation();
  360. }, 0.5);
  361. }
  362. /**
  363. * 播放奖励动画
  364. */
  365. private async playRewardAnimation(): Promise<void> {
  366. console.log('MainUIController.playRewardAnimation 开始播放奖励动画');
  367. const currentLevel = this.sdm.getCurrentLevel();
  368. try {
  369. // 直接获取SaveDataManager中已经计算好的奖励数据
  370. // 注意:不再依赖当前游戏状态判断,因为游戏数据清理可能已经重置了状态
  371. // SaveDataManager.getLastRewards()已经包含了正确的奖励信息(成功或失败奖励)
  372. const rewards = this.sdm.getLastRewards();
  373. console.log('获取已计算的奖励:', rewards);
  374. if (rewards && (rewards.money > 0 || rewards.diamonds > 0)) {
  375. // 使用MoneyAni播放奖励动画
  376. if (this.moneyAniNode) {
  377. const moneyAni = this.moneyAniNode.getComponent(MoneyAni);
  378. if (moneyAni) {
  379. moneyAni.playRewardAnimation(rewards.money, rewards.diamonds, () => {
  380. console.log('奖励动画播放完成');
  381. });
  382. } else {
  383. console.error('MoneyAni组件未找到');
  384. // 使用静态方法作为备选
  385. MoneyAni.playReward(rewards.money, rewards.diamonds);
  386. }
  387. } else {
  388. console.warn('MoneyAni节点未设置,使用静态方法播放动画');
  389. MoneyAni.playReward(rewards.money, rewards.diamonds);
  390. }
  391. } else {
  392. console.log('当前关卡没有奖励或奖励为0');
  393. }
  394. } catch (error) {
  395. console.error('播放奖励动画时出错:', error);
  396. // 使用默认奖励
  397. MoneyAni.playReward(25, 2);
  398. }
  399. }
  400. /**
  401. * 播放主界面背景音乐
  402. */
  403. private async playMainUIBGM(): Promise<void> {
  404. console.log('[MainUIController] 开始播放主界面背景音乐');
  405. try {
  406. // 获取BundleLoader实例
  407. const bundleLoader = BundleLoader.getInstance();
  408. // 确保data bundle已加载完成
  409. await bundleLoader.loadBundle('data');
  410. console.log('[MainUIController] data bundle加载完成,开始播放音乐');
  411. // 先停止当前音乐(包括战斗背景音乐)
  412. Audio.stopMusic();
  413. // 恢复正常音乐音量
  414. Audio.setMusicVolume(0.8);
  415. // 播放主界面BGM,循环播放
  416. Audio.playMusic('data/弹球音效/ui bgm', true);
  417. console.log('[MainUIController] 主界面背景音乐播放完成');
  418. } catch (error) {
  419. console.error('[MainUIController] 播放主界面背景音乐失败:', error);
  420. }
  421. }
  422. /**
  423. * 预加载当前关卡和下一关的资源
  424. * 在MainUI场景加载时提前预加载,避免进入游戏时的资源加载延迟
  425. */
  426. private async preloadLevelResources(): Promise<void> {
  427. console.log('[MainUIController] 开始预加载关卡资源');
  428. try {
  429. const resourcePreloader = ResourcePreloader.getInstance();
  430. const currentLevel = this.sdm.getCurrentLevel();
  431. // 预加载当前关卡资源
  432. console.log(`[MainUIController] 预加载当前关卡 ${currentLevel} 的资源`);
  433. try {
  434. await resourcePreloader.preloadLevelResources(currentLevel);
  435. console.log(`[MainUIController] 当前关卡 ${currentLevel} 资源预加载完成`);
  436. } catch (error) {
  437. console.warn(`[MainUIController] 当前关卡 ${currentLevel} 资源预加载失败:`, error);
  438. }
  439. // 预加载下一关资源(如果存在)
  440. const nextLevel = currentLevel + 1;
  441. console.log(`[MainUIController] 预加载下一关卡 ${nextLevel} 的资源`);
  442. try {
  443. await resourcePreloader.preloadLevelResources(nextLevel);
  444. console.log(`[MainUIController] 下一关卡 ${nextLevel} 资源预加载完成`);
  445. } catch (error) {
  446. console.warn(`[MainUIController] 下一关卡 ${nextLevel} 资源预加载失败(可能不存在该关卡):`, error);
  447. }
  448. console.log('[MainUIController] 关卡资源预加载流程完成');
  449. } catch (error) {
  450. console.error('[MainUIController] 预加载关卡资源时发生错误:', error);
  451. }
  452. }
  453. /* =============== Util =============== */
  454. private format(n:number){ return n>=1000000? (n/1e6).toFixed(1)+'M' : n>=1000? (n/1e3).toFixed(1)+'K' : n.toString(); }
  455. }