MenuController.ts 15 KB

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