import { _decorator, Component, Node, Button, find } from 'cc'; import { PopUPAni } from '../../Animations/PopUPAni'; import EventBus, { GameEvents } from '../../Core/EventBus'; import { GameManager, AppState } from '../../LevelSystem/GameManager'; import { GameStartMove } from '../../Animations/GameStartMove'; import { GamePause } from '../GamePause'; import { SoundController } from './SoundController'; const { ccclass, property } = _decorator; /** * 菜单系统控制器 * 负责管理菜单UI的显示和隐藏 */ @ccclass('MenuController') export class MenuController extends Component { // UI节点引用 @property(Node) menuUI: Node = null; // Canvas/MenuUI @property(Button) menuButton: Button = null; // Canvas/MenuButton @property(Button) closeButton: Button = null; // Canvas/MenuUI中的关闭按钮 @property(Button) backButton: Button = null; // Canvas/MenuUI/Buttons/BackButton 退出游戏按钮 @property(Button) continueButton: Button = null; // Canvas/MenuUI/Buttons/ContinueButton 继续游戏按钮 // 动画控制器 @property(PopUPAni) popupAni: PopUPAni = null; // Canvas/MenuUI上的PopUPAni组件 // 音频控制器 @property(SoundController) soundController: SoundController = null; // Canvas/MenuUI上的SoundController组件 // 游戏管理器引用 @property(GameManager) gameManager: GameManager = null; // GameManager组件引用 // 菜单状态 private isMenuOpen: boolean = false; // GameStartMove组件引用,用于重置镜头位置 private gameStartMoveComponent: GameStartMove = null; onLoad() { this.bindEvents(); // 初始化时隐藏菜单 - 使用PopUPAni的场景外位置隐藏方法 if (this.popupAni) { this.popupAni.hidePanelImmediate(); } } /** * 绑定事件 */ private bindEvents() { // 绑定菜单按钮点击事件 if (this.menuButton) { this.menuButton.node.on(Button.EventType.CLICK, this.onMenuButtonClick, this); } // 绑定关闭按钮点击事件 if (this.closeButton) { this.closeButton.node.on(Button.EventType.CLICK, this.onCloseButtonClick, this); } // 绑定退出游戏按钮点击事件 if (this.backButton) { this.backButton.node.on(Button.EventType.CLICK, this.onBackButtonClick, this); } // 绑定继续游戏按钮点击事件 if (this.continueButton) { this.continueButton.node.on(Button.EventType.CLICK, this.onContinueButtonClick, this); } } /** * 菜单按钮点击事件 */ private async onMenuButtonClick() { if (this.isMenuOpen) { await this.closeMenu(); } else { await this.openMenu(); } } /** * 关闭按钮点击事件 */ private async onCloseButtonClick() { await this.closeMenu(); } /** * 继续游戏按钮点击事件 */ private async onContinueButtonClick() { console.log('[MenuController] 继续游戏按钮被点击'); // 检查GameManager组件引用 if (!this.gameManager) { console.error('[MenuController] GameManager组件未通过装饰器挂载,请在Inspector中拖拽GameManager组件'); await this.closeMenu(); return; } const currentAppState = this.gameManager.getCurrentAppState(); console.log(`[MenuController] 当前应用状态: ${currentAppState}`); if (currentAppState === AppState.IN_GAME) { // 在游戏中点击继续,关闭菜单并恢复游戏 console.log('[MenuController] 游戏中点击继续,关闭菜单并恢复游戏'); await this.closeMenu(); } else { // 游戏外点击继续,直接关闭菜单 console.log('[MenuController] 游戏外点击继续,直接关闭菜单'); await this.closeMenu(); } } /** * 退出游戏按钮点击事件 */ private async onBackButtonClick() { console.log('[MenuController] 退出游戏按钮被点击'); // 检查GameManager组件引用 if (!this.gameManager) { console.error('[MenuController] GameManager组件未通过装饰器挂载,请在Inspector中拖拽GameManager组件'); return; } const currentAppState = this.gameManager.getCurrentAppState(); console.log(`[MenuController] 当前应用状态: ${currentAppState}`); if (currentAppState === AppState.IN_GAME) { // 在游戏中退出,先重置镜头位置,然后视为游戏失败 console.log('[MenuController] 游戏中退出,先重置镜头位置'); this.resetCameraPosition(); console.log('[MenuController] 触发游戏失败事件'); const eventBus = EventBus.getInstance(); eventBus.emit(GameEvents.GAME_DEFEAT); // 等待游戏失败处理完成后关闭菜单 setTimeout(async () => { await this.closeMenu(); console.log('[MenuController] 游戏失败处理完成,菜单已关闭'); }, 100); } else { // 游戏外退出,关闭菜单并返回主界面(不需要重置镜头) console.log('[MenuController] 游戏外退出,关闭菜单并返回主界面'); await this.closeMenu(); // 触发返回主界面事件 const eventBus = EventBus.getInstance(); eventBus.emit(GameEvents.RETURN_TO_MAIN_MENU); console.log('[MenuController] 已触发返回主界面事件'); } } /** * 重置镜头位置到原始位置 * 确保从游戏中退出时镜头位置正常 */ private resetCameraPosition() { // 如果还没有获取GameStartMove组件,尝试获取 if (!this.gameStartMoveComponent) { const cameraNode = find('Canvas/Main Camera'); if (cameraNode) { this.gameStartMoveComponent = cameraNode.getComponent(GameStartMove); } } // 如果成功获取到组件,重置镜头位置 if (this.gameStartMoveComponent) { console.log('[MenuController] 重置镜头位置到原始位置'); this.gameStartMoveComponent.resetCameraToOriginalPosition(0.3); } else { console.warn('[MenuController] 未找到GameStartMove组件,无法重置镜头位置'); } } /** * 打开菜单 */ public async openMenu(): Promise { if (this.isMenuOpen) return; this.isMenuOpen = true; // 检查是否在游戏中,如果是则暂停游戏 if (this.gameManager && this.gameManager.getCurrentAppState() === AppState.IN_GAME) { console.log('[MenuController] 游戏中打开菜单,暂停游戏'); const gamePause = GamePause.getInstance(); gamePause.pauseGame(); } // 使用动画显示菜单面板 if (this.popupAni) { await this.popupAni.showPanel(); } // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true console.log('菜单已打开'); } /** * 关闭菜单 */ public async closeMenu(): Promise { if (!this.isMenuOpen) return; this.isMenuOpen = false; // 使用动画隐藏菜单面板 if (this.popupAni) { await this.popupAni.hidePanel(); } // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true // 检查是否在游戏中,如果是则恢复游戏 if (this.gameManager && this.gameManager.getCurrentAppState() === AppState.IN_GAME) { console.log('[MenuController] 游戏中关闭菜单,恢复游戏'); const gamePause = GamePause.getInstance(); gamePause.resumeGame(); } console.log('菜单已关闭'); } /** * 切换菜单状态 */ public async toggleMenu(): Promise { if (this.isMenuOpen) { await this.closeMenu(); } else { await this.openMenu(); } } /** * 获取菜单状态 */ public getMenuState(): boolean { return this.isMenuOpen; } /** * 立即关闭菜单(无动画) */ public closeMenuImmediate(): void { this.isMenuOpen = false; if (this.popupAni) { this.popupAni.hidePanelImmediate(); } // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true console.log('菜单已立即关闭'); } /** * 立即打开菜单(无动画) */ public openMenuImmediate(): void { this.isMenuOpen = true; if (this.popupAni) { this.popupAni.showPanelImmediate(); } // 注意:不再有fallback逻辑设置active,菜单面板始终保持active=true console.log('菜单已立即打开'); } onDestroy() { // 解绑事件 if (this.menuButton) { this.menuButton.node.off(Button.EventType.CLICK, this.onMenuButtonClick, this); } if (this.closeButton) { this.closeButton.node.off(Button.EventType.CLICK, this.onCloseButtonClick, this); } if (this.backButton) { this.backButton.node.off(Button.EventType.CLICK, this.onBackButtonClick, this); } if (this.continueButton) { this.continueButton.node.off(Button.EventType.CLICK, this.onContinueButtonClick, this); } } }