MenuController.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 { GamePause } from '../GamePause';
  7. import { SoundController } from './SoundController';
  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) closeButton: Button = null; // Canvas/MenuUI中的关闭按钮
  19. @property(Button) backButton: Button = null; // Canvas/MenuUI/Buttons/BackButton 退出游戏按钮
  20. @property(Button) continueButton: Button = null; // Canvas/MenuUI/Buttons/ContinueButton 继续游戏按钮
  21. // 动画控制器
  22. @property(PopUPAni) popupAni: PopUPAni = null; // Canvas/MenuUI上的PopUPAni组件
  23. // 音频控制器
  24. @property(SoundController) soundController: SoundController = null; // Canvas/MenuUI上的SoundController组件
  25. // 游戏管理器引用
  26. @property(GameManager) gameManager: GameManager = null; // GameManager组件引用
  27. // 菜单状态
  28. private isMenuOpen: boolean = false;
  29. // GameStartMove组件引用,用于重置镜头位置
  30. private gameStartMoveComponent: GameStartMove = null;
  31. onLoad() {
  32. this.bindEvents();
  33. // 初始化时隐藏菜单 - 使用PopUPAni的场景外位置隐藏方法
  34. if (this.popupAni) {
  35. this.popupAni.hidePanelImmediate();
  36. }
  37. }
  38. /**
  39. * 绑定事件
  40. */
  41. private bindEvents() {
  42. // 绑定菜单按钮点击事件
  43. if (this.menuButton) {
  44. this.menuButton.node.on(Button.EventType.CLICK, this.onMenuButtonClick, this);
  45. }
  46. // 绑定关闭按钮点击事件
  47. if (this.closeButton) {
  48. this.closeButton.node.on(Button.EventType.CLICK, this.onCloseButtonClick, this);
  49. }
  50. // 绑定退出游戏按钮点击事件
  51. if (this.backButton) {
  52. this.backButton.node.on(Button.EventType.CLICK, this.onBackButtonClick, this);
  53. }
  54. // 绑定继续游戏按钮点击事件
  55. if (this.continueButton) {
  56. this.continueButton.node.on(Button.EventType.CLICK, this.onContinueButtonClick, this);
  57. }
  58. }
  59. /**
  60. * 菜单按钮点击事件
  61. */
  62. private async onMenuButtonClick() {
  63. if (this.isMenuOpen) {
  64. await this.closeMenu();
  65. } else {
  66. await this.openMenu();
  67. }
  68. }
  69. /**
  70. * 关闭按钮点击事件
  71. */
  72. private async onCloseButtonClick() {
  73. await this.closeMenu();
  74. }
  75. /**
  76. * 继续游戏按钮点击事件
  77. */
  78. private async onContinueButtonClick() {
  79. console.log('[MenuController] 继续游戏按钮被点击');
  80. // 检查GameManager组件引用
  81. if (!this.gameManager) {
  82. console.error('[MenuController] GameManager组件未通过装饰器挂载,请在Inspector中拖拽GameManager组件');
  83. await this.closeMenu();
  84. return;
  85. }
  86. const currentAppState = this.gameManager.getCurrentAppState();
  87. console.log(`[MenuController] 当前应用状态: ${currentAppState}`);
  88. if (currentAppState === AppState.IN_GAME) {
  89. // 在游戏中点击继续,关闭菜单并恢复游戏
  90. console.log('[MenuController] 游戏中点击继续,关闭菜单并恢复游戏');
  91. await this.closeMenu();
  92. } else {
  93. // 游戏外点击继续,直接关闭菜单
  94. console.log('[MenuController] 游戏外点击继续,直接关闭菜单');
  95. await this.closeMenu();
  96. }
  97. }
  98. /**
  99. * 退出游戏按钮点击事件
  100. */
  101. private async onBackButtonClick() {
  102. console.log('[MenuController] 退出游戏按钮被点击');
  103. // 检查GameManager组件引用
  104. if (!this.gameManager) {
  105. console.error('[MenuController] GameManager组件未通过装饰器挂载,请在Inspector中拖拽GameManager组件');
  106. return;
  107. }
  108. const currentAppState = this.gameManager.getCurrentAppState();
  109. console.log(`[MenuController] 当前应用状态: ${currentAppState}`);
  110. if (currentAppState === AppState.IN_GAME) {
  111. // 在游戏中退出,先重置镜头位置,然后视为游戏失败
  112. console.log('[MenuController] 游戏中退出,先重置镜头位置');
  113. this.resetCameraPosition();
  114. console.log('[MenuController] 触发游戏失败事件');
  115. const eventBus = EventBus.getInstance();
  116. eventBus.emit(GameEvents.GAME_DEFEAT);
  117. // 等待游戏失败处理完成后关闭菜单
  118. setTimeout(async () => {
  119. await this.closeMenu();
  120. console.log('[MenuController] 游戏失败处理完成,菜单已关闭');
  121. }, 100);
  122. } else {
  123. // 游戏外退出,关闭菜单并返回主界面(不需要重置镜头)
  124. console.log('[MenuController] 游戏外退出,关闭菜单并返回主界面');
  125. await this.closeMenu();
  126. // 触发返回主界面事件
  127. const eventBus = EventBus.getInstance();
  128. eventBus.emit(GameEvents.RETURN_TO_MAIN_MENU);
  129. console.log('[MenuController] 已触发返回主界面事件');
  130. }
  131. }
  132. /**
  133. * 重置镜头位置到原始位置
  134. * 确保从游戏中退出时镜头位置正常
  135. */
  136. private resetCameraPosition() {
  137. // 如果还没有获取GameStartMove组件,尝试获取
  138. if (!this.gameStartMoveComponent) {
  139. const cameraNode = find('Canvas/Main Camera');
  140. if (cameraNode) {
  141. this.gameStartMoveComponent = cameraNode.getComponent(GameStartMove);
  142. }
  143. }
  144. // 如果成功获取到组件,重置镜头位置
  145. if (this.gameStartMoveComponent) {
  146. console.log('[MenuController] 重置镜头位置到原始位置');
  147. this.gameStartMoveComponent.resetCameraToOriginalPosition(0.3);
  148. } else {
  149. console.warn('[MenuController] 未找到GameStartMove组件,无法重置镜头位置');
  150. }
  151. }
  152. /**
  153. * 打开菜单
  154. */
  155. public async openMenu(): Promise<void> {
  156. if (this.isMenuOpen) return;
  157. this.isMenuOpen = true;
  158. // 检查是否在游戏中,如果是则暂停游戏
  159. if (this.gameManager && this.gameManager.getCurrentAppState() === AppState.IN_GAME) {
  160. console.log('[MenuController] 游戏中打开菜单,暂停游戏');
  161. const gamePause = GamePause.getInstance();
  162. gamePause.pauseGame();
  163. }
  164. // 使用动画显示菜单面板
  165. if (this.popupAni) {
  166. await this.popupAni.showPanel();
  167. }
  168. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  169. console.log('菜单已打开');
  170. }
  171. /**
  172. * 关闭菜单
  173. */
  174. public async closeMenu(): Promise<void> {
  175. if (!this.isMenuOpen) return;
  176. this.isMenuOpen = false;
  177. // 使用动画隐藏菜单面板
  178. if (this.popupAni) {
  179. await this.popupAni.hidePanel();
  180. }
  181. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  182. // 检查是否在游戏中,如果是则恢复游戏
  183. if (this.gameManager && this.gameManager.getCurrentAppState() === AppState.IN_GAME) {
  184. console.log('[MenuController] 游戏中关闭菜单,恢复游戏');
  185. const gamePause = GamePause.getInstance();
  186. gamePause.resumeGame();
  187. }
  188. console.log('菜单已关闭');
  189. }
  190. /**
  191. * 切换菜单状态
  192. */
  193. public async toggleMenu(): Promise<void> {
  194. if (this.isMenuOpen) {
  195. await this.closeMenu();
  196. } else {
  197. await this.openMenu();
  198. }
  199. }
  200. /**
  201. * 获取菜单状态
  202. */
  203. public getMenuState(): boolean {
  204. return this.isMenuOpen;
  205. }
  206. /**
  207. * 立即关闭菜单(无动画)
  208. */
  209. public closeMenuImmediate(): void {
  210. this.isMenuOpen = false;
  211. if (this.popupAni) {
  212. this.popupAni.hidePanelImmediate();
  213. }
  214. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  215. console.log('菜单已立即关闭');
  216. }
  217. /**
  218. * 立即打开菜单(无动画)
  219. */
  220. public openMenuImmediate(): void {
  221. this.isMenuOpen = true;
  222. if (this.popupAni) {
  223. this.popupAni.showPanelImmediate();
  224. }
  225. // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true
  226. console.log('菜单已立即打开');
  227. }
  228. onDestroy() {
  229. // 解绑事件
  230. if (this.menuButton) {
  231. this.menuButton.node.off(Button.EventType.CLICK, this.onMenuButtonClick, this);
  232. }
  233. if (this.closeButton) {
  234. this.closeButton.node.off(Button.EventType.CLICK, this.onCloseButtonClick, this);
  235. }
  236. if (this.backButton) {
  237. this.backButton.node.off(Button.EventType.CLICK, this.onBackButtonClick, this);
  238. }
  239. if (this.continueButton) {
  240. this.continueButton.node.off(Button.EventType.CLICK, this.onContinueButtonClick, this);
  241. }
  242. }
  243. }