MainUIControlller.ts 15 KB

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