MainUIControlller.ts 22 KB

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