MenuController.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import { _decorator, Component, Node, Button, find } from 'cc';
  2. import { PopUPAni } from '../../Animations/PopUPAni';
  3. import EventBus, { GameEvents } from '../../Core/EventBus';
  4. import { GameManager, AppState } from '../../LevelSystem/GameManager';
  5. import { SaveDataManager } from '../../LevelSystem/SaveDataManager';
  6. import { GameStartMove } from '../../Animations/GameStartMove';
  7. import { SoundController } from './SoundController';
  8. import { Audio } from '../../AudioManager/AudioManager';
  9. const { ccclass, property } = _decorator;
  10. /**
  11. * 菜单系统控制器
  12. * 负责管理菜单UI的显示和隐藏
  13. */
  14. @ccclass('MenuController')
  15. export class MenuController extends Component {
  16. // UI节点引用
  17. @property(Node) menuUI: Node = null; // Canvas/MenuUI
  18. @property(Button) menuButton: Button = null; // Canvas/MenuButton (主界面使用)
  19. @property(Button) closeButton: Button = null; // Canvas/MenuUI中的关闭按钮
  20. @property(Button) backButton: Button = null; // Canvas/MenuUI/Buttons/BackButton 退出游戏按钮
  21. @property(Button) continueButton: Button = null; // Canvas/MenuUI/Buttons/ContinueButton 继续游戏按钮
  22. // 需要检测的UI节点引用
  23. @property(Node) skillUpUI: Node = null; // Canvas/SkillUpUI
  24. @property(Node) upgradeUI: Node = null; // Canvas/UpgradeUI
  25. @property(Node) shopUI: Node = null; // Canvas/ShopUI
  26. // 动画控制器
  27. @property(PopUPAni) popupAni: PopUPAni = null; // Canvas/MenuUI上的PopUPAni组件
  28. // 音频控制器
  29. @property(SoundController) soundController: SoundController = null; // Canvas/MenuUI上的SoundController组件
  30. // 游戏管理器引用
  31. @property(GameManager) gameManager: GameManager = null; // GameManager组件引用
  32. // 菜单状态
  33. private isMenuOpen: boolean = false;
  34. // GameStartMove组件引用,用于重置镜头位置
  35. private gameStartMoveComponent: GameStartMove = null;
  36. onLoad() {
  37. this.bindEvents();
  38. // 初始化时隐藏菜单 - 使用PopUPAni的场景外位置隐藏方法
  39. if (this.popupAni) {
  40. this.popupAni.hidePanelImmediate();
  41. }
  42. // 设置界面状态监听
  43. this.setupUIStateListeners();
  44. // 初始化菜单按钮显示状态
  45. this.updateMenuButtonVisibility();
  46. }
  47. start() {
  48. // 初始化菜单按钮状态
  49. this.updateMenuButtonVisibility();
  50. }
  51. /**
  52. * 绑定事件
  53. */
  54. private bindEvents() {
  55. // 绑定菜单按钮点击事件 (主界面使用)
  56. if (this.menuButton) {
  57. this.menuButton.node.on(Button.EventType.CLICK, this.onMenuButtonClick, this);
  58. }
  59. // 绑定关闭按钮点击事件
  60. if (this.closeButton) {
  61. this.closeButton.node.on(Button.EventType.CLICK, this.onCloseButtonClick, this);
  62. }
  63. // 绑定退出游戏按钮点击事件
  64. if (this.backButton) {
  65. this.backButton.node.on(Button.EventType.CLICK, this.onBackButtonClick, this);
  66. }
  67. // 绑定继续游戏按钮点击事件
  68. if (this.continueButton) {
  69. this.continueButton.node.on(Button.EventType.CLICK, this.onContinueButtonClick, this);
  70. }
  71. }
  72. /**
  73. * 设置UI状态监听器
  74. */
  75. private setupUIStateListeners() {
  76. const eventBus = EventBus.getInstance();
  77. // 监听界面切换事件(如果有的话)
  78. // 这里可以根据实际的界面切换事件来更新菜单按钮显示状态
  79. eventBus.on(GameEvents.RETURN_TO_MAIN_MENU, this.updateMenuButtonVisibility, this);
  80. eventBus.on(GameEvents.UI_PANEL_SWITCHED, this.updateMenuButtonVisibility, this);
  81. // 监听应用状态切换事件
  82. eventBus.on(GameEvents.APP_STATE_CHANGED, this.onAppStateChanged, this);
  83. }
  84. /**
  85. * 检测当前是否在需要隐藏菜单按钮的界面
  86. */
  87. private isInHiddenMenuUI(): boolean {
  88. // 检查SkillUpUI、UpgradeUI、ShopUI是否激活
  89. const isSkillUpActive = this.skillUpUI && this.skillUpUI.active;
  90. const isUpgradeActive = this.upgradeUI && this.upgradeUI.active;
  91. const isShopActive = this.shopUI && this.shopUI.active;
  92. return isSkillUpActive || isUpgradeActive || isShopActive;
  93. }
  94. /**
  95. * 更新菜单按钮的显示状态
  96. */
  97. private updateMenuButtonVisibility() {
  98. // 检查当前应用状态
  99. const currentAppState = this.gameManager ? this.gameManager.getCurrentAppState() : null;
  100. // 检查是否为第一关
  101. const isFirstLevel = (() => {
  102. try {
  103. const save = SaveDataManager.getInstance();
  104. return save && typeof save.getCurrentLevel === 'function' && save.getCurrentLevel() === 1;
  105. } catch {
  106. return false;
  107. }
  108. })();
  109. // 在游戏外界面中,检查是否在需要隐藏菜单按钮的界面
  110. if (currentAppState !== AppState.IN_GAME && this.isInHiddenMenuUI()) {
  111. // 在SkillUpUI、UpgradeUI、ShopUI界面中隐藏菜单按钮
  112. if (this.menuButton) this.menuButton.node.active = false;
  113. console.log('[MenuController] 在游戏外界面中隐藏菜单按钮');
  114. } else if (currentAppState === AppState.IN_GAME) {
  115. // 第一关隐藏游戏中的菜单按钮;其他关卡显示
  116. if (isFirstLevel) {
  117. if (this.menuButton) this.menuButton.node.active = false;
  118. console.log('[MenuController] 第一关隐藏菜单按钮');
  119. } else {
  120. if (this.menuButton) this.menuButton.node.active = true;
  121. console.log('[MenuController] 游戏中显示菜单按钮');
  122. }
  123. } else {
  124. // 主界面显示菜单按钮
  125. if (this.menuButton) this.menuButton.node.active = true;
  126. console.log('[MenuController] 主界面显示菜单按钮');
  127. }
  128. }
  129. /**
  130. * 菜单按钮点击事件
  131. */
  132. private async onMenuButtonClick() {
  133. // 播放UI点击音效
  134. Audio.playUISound('data/弹球音效/ui play');
  135. if (this.isMenuOpen) {
  136. await this.closeMenu();
  137. } else {
  138. await this.openMenu();
  139. }
  140. }
  141. /**
  142. * 关闭按钮点击事件
  143. */
  144. private async onCloseButtonClick() {
  145. // 播放UI点击音效
  146. Audio.playUISound('data/弹球音效/ui play');
  147. await this.closeMenu();
  148. }
  149. /**
  150. * 继续游戏按钮点击事件
  151. */
  152. private async onContinueButtonClick() {
  153. // 播放UI点击音效
  154. Audio.playUISound('data/弹球音效/ui play');
  155. console.log('[MenuController] 继续游戏按钮被点击');
  156. // 检查GameManager组件引用
  157. if (!this.gameManager) {
  158. console.error('[MenuController] GameManager组件未通过装饰器挂载,请在Inspector中拖拽GameManager组件');
  159. await this.closeMenu();
  160. return;
  161. }
  162. const currentAppState = this.gameManager.getCurrentAppState();
  163. console.log(`[MenuController] 当前应用状态: ${currentAppState}`);
  164. if (currentAppState === AppState.IN_GAME) {
  165. // 在游戏中点击继续,关闭菜单并恢复游戏
  166. console.log('[MenuController] 游戏中点击继续,关闭菜单并恢复游戏');
  167. await this.closeMenu();
  168. } else {
  169. // 游戏外点击继续,直接关闭菜单
  170. console.log('[MenuController] 游戏外点击继续,直接关闭菜单');
  171. await this.closeMenu();
  172. }
  173. }
  174. /**
  175. * 退出游戏按钮点击事件
  176. * 只在游戏中有效,游戏外界面不显示菜单按钮
  177. */
  178. private async onBackButtonClick() {
  179. // 播放UI点击音效
  180. Audio.playUISound('data/弹球音效/ui play');
  181. console.log('[MenuController] 退出游戏按钮被点击');
  182. // 检查GameManager组件引用
  183. if (!this.gameManager) {
  184. console.error('[MenuController] GameManager组件未通过装饰器挂载,请在Inspector中拖拽GameManager组件');
  185. return;
  186. }
  187. const currentAppState = this.gameManager.getCurrentAppState();
  188. console.log(`[MenuController] 当前应用状态: ${currentAppState}`);
  189. // 只处理游戏中的退出逻辑,游戏外界面不显示菜单按钮
  190. if (currentAppState === AppState.IN_GAME) {
  191. // 在游戏中退出,应该触发游戏失败事件显示GameEnd面板
  192. console.log('[MenuController] 游戏中退出,触发GAME_DEFEAT事件显示游戏失败UI');
  193. // 先触发游戏失败事件,让GameEnd面板显示
  194. const eventBus = EventBus.getInstance();
  195. eventBus.emit(GameEvents.GAME_DEFEAT);
  196. // 关闭菜单(不触发GAME_RESUME事件,避免事件冲突)
  197. await this.closeMenuWithoutResume();
  198. console.log('[MenuController] 菜单退出处理完成,已触发GAME_DEFEAT事件');
  199. } else {
  200. // 游戏外不应该显示菜单按钮,如果意外触发则只关闭菜单
  201. console.log('[MenuController] 游戏外意外触发退出按钮,仅关闭菜单');
  202. await this.closeMenu();
  203. }
  204. }
  205. /**
  206. * 重置镜头位置到原始位置
  207. * 确保从游戏中退出时镜头位置正常
  208. */
  209. private resetCameraPosition() {
  210. // 如果还没有获取GameStartMove组件,尝试获取
  211. if (!this.gameStartMoveComponent) {
  212. const cameraNode = find('Canvas/Main Camera');
  213. if (cameraNode) {
  214. this.gameStartMoveComponent = cameraNode.getComponent(GameStartMove);
  215. }
  216. }
  217. // 如果成功获取到组件,重置镜头位置
  218. if (this.gameStartMoveComponent) {
  219. console.log('[MenuController] 重置镜头位置到原始位置');
  220. this.gameStartMoveComponent.resetCameraToOriginalPosition(0.3);
  221. } else {
  222. console.warn('[MenuController] 未找到GameStartMove组件,无法重置镜头位置');
  223. }
  224. }
  225. /**
  226. * 打开菜单
  227. */
  228. public async openMenu(): Promise<void> {
  229. if (this.isMenuOpen) return;
  230. this.isMenuOpen = true;
  231. // 检查是否在游戏中,如果是则通过事件系统触发游戏暂停
  232. if (this.gameManager && this.gameManager.getCurrentAppState() === AppState.IN_GAME) {
  233. console.log('[MenuController] 游戏中打开菜单,通过事件系统触发游戏暂停');
  234. const eventBus = EventBus.getInstance();
  235. eventBus.emit(GameEvents.GAME_PAUSE);
  236. }
  237. // 使用动画显示菜单面板
  238. if (this.popupAni) {
  239. await this.popupAni.showPanel();
  240. }
  241. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  242. console.log('菜单已打开');
  243. }
  244. /**
  245. * 关闭菜单
  246. */
  247. public async closeMenu(): Promise<void> {
  248. if (!this.isMenuOpen) return;
  249. this.isMenuOpen = false;
  250. // 使用动画隐藏菜单面板
  251. if (this.popupAni) {
  252. await this.popupAni.hidePanel();
  253. }
  254. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  255. // 检查是否在游戏中,如果是则通过事件系统触发游戏恢复
  256. if (this.gameManager && this.gameManager.getCurrentAppState() === AppState.IN_GAME) {
  257. console.log('[MenuController] 游戏中关闭菜单,通过事件系统触发游戏恢复');
  258. const eventBus = EventBus.getInstance();
  259. eventBus.emit(GameEvents.GAME_RESUME);
  260. }
  261. console.log('菜单已关闭');
  262. }
  263. /**
  264. * 关闭菜单但不触发游戏恢复事件(用于游戏失败退出)
  265. */
  266. private async closeMenuWithoutResume(): Promise<void> {
  267. if (!this.isMenuOpen) return;
  268. this.isMenuOpen = false;
  269. // 使用动画隐藏菜单面板
  270. if (this.popupAni) {
  271. await this.popupAni.hidePanel();
  272. }
  273. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  274. // 不触发GAME_RESUME事件,避免与GAME_DEFEAT事件冲突
  275. console.log('菜单已关闭(未触发游戏恢复)');
  276. }
  277. /**
  278. * 切换菜单状态
  279. */
  280. public async toggleMenu(): Promise<void> {
  281. if (this.isMenuOpen) {
  282. await this.closeMenu();
  283. } else {
  284. await this.openMenu();
  285. }
  286. }
  287. /**
  288. * 获取菜单状态
  289. */
  290. public getMenuState(): boolean {
  291. return this.isMenuOpen;
  292. }
  293. /**
  294. * 立即关闭菜单(无动画)
  295. */
  296. public closeMenuImmediate(): void {
  297. this.isMenuOpen = false;
  298. if (this.popupAni) {
  299. this.popupAni.hidePanelImmediate();
  300. }
  301. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  302. console.log('菜单已立即关闭');
  303. }
  304. /**
  305. * 立即打开菜单(无动画)
  306. */
  307. public openMenuImmediate(): void {
  308. this.isMenuOpen = true;
  309. if (this.popupAni) {
  310. this.popupAni.showPanelImmediate();
  311. }
  312. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  313. console.log('菜单已立即打开');
  314. }
  315. /**
  316. * 应用状态切换事件处理
  317. */
  318. private onAppStateChanged(): void {
  319. console.log('[MenuController] 应用状态切换,更新菜单按钮显示状态');
  320. this.updateMenuButtonVisibility();
  321. }
  322. /**
  323. * 公共方法:更新菜单按钮显示状态
  324. * 供外部组件调用,例如在界面切换时
  325. */
  326. public updateMenuButtonState(): void {
  327. this.updateMenuButtonVisibility();
  328. }
  329. onDestroy() {
  330. // 解绑按钮事件
  331. if (this.menuButton) {
  332. this.menuButton.node.off(Button.EventType.CLICK, this.onMenuButtonClick, this);
  333. }
  334. if (this.closeButton) {
  335. this.closeButton.node.off(Button.EventType.CLICK, this.onCloseButtonClick, this);
  336. }
  337. if (this.backButton) {
  338. this.backButton.node.off(Button.EventType.CLICK, this.onBackButtonClick, this);
  339. }
  340. if (this.continueButton) {
  341. this.continueButton.node.off(Button.EventType.CLICK, this.onContinueButtonClick, this);
  342. }
  343. // 解绑事件监听器
  344. const eventBus = EventBus.getInstance();
  345. eventBus.off(GameEvents.RETURN_TO_MAIN_MENU, this.updateMenuButtonVisibility, this);
  346. eventBus.off(GameEvents.UI_PANEL_SWITCHED, this.updateMenuButtonVisibility, this);
  347. eventBus.off(GameEvents.APP_STATE_CHANGED, this.onAppStateChanged, this);
  348. }
  349. }