| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import { _decorator, Component, find } from 'cc';
- import { LevelSessionManager } from '../Core/LevelSessionManager';
- import EventBus, { GameEvents } from '../Core/EventBus';
- const { ccclass } = _decorator;
- /**
- * 游戏重置管理器
- * 负责协调各个组件的重置操作,使用事件系统解耦
- */
- @ccclass('ReStartGame')
- export class ReStartGame extends Component {
-
- start() {
- // 监听游戏重启事件
- const eventBus = EventBus.getInstance();
- eventBus.on(GameEvents.GAME_RESTART, this.onGameRestartEvent, this);
- }
-
- onDestroy() {
- // 移除事件监听
- const eventBus = EventBus.getInstance();
- eventBus.off(GameEvents.GAME_RESTART, this.onGameRestartEvent, this);
- }
-
- /**
- * 处理游戏重启事件
- */
- private onGameRestartEvent() {
- console.log('[ReStartGame] 接收到游戏重启事件,开始重置游戏状态');
- ReStartGame.resetAllGameStates();
- }
-
- /**
- * 完整重置游戏状态
- * 在关闭成功/失败界面时调用
- */
- public static resetAllGameStates() {
- console.log('[ReStartGame] 开始完整重置游戏状态');
-
- const eventBus = EventBus.getInstance();
-
- // 发送游戏重置请求事件
- eventBus.emit(GameEvents.GAME_RESET_REQUEST);
-
- // 按顺序发送各个组件的重置事件
- setTimeout(() => {
- // 1. 清理所有游戏对象
- eventBus.emit(GameEvents.CLEAR_ALL_GAME_OBJECTS);
-
- // 2. 重置各个组件状态
- eventBus.emit(GameEvents.RESET_GAME_MANAGER);
- eventBus.emit(GameEvents.RESET_ENEMY_CONTROLLER);
- eventBus.emit(GameEvents.RESET_BALL_CONTROLLER);
- eventBus.emit(GameEvents.RESET_BLOCK_MANAGER);
- eventBus.emit(GameEvents.RESET_BLOCK_SELECTION);
- eventBus.emit(GameEvents.RESET_WALL_HEALTH);
- eventBus.emit(GameEvents.RESET_GAME_PAUSE);
- eventBus.emit(GameEvents.RESET_ENERGY_SYSTEM);
-
- // 3. 重置UI状态
- eventBus.emit(GameEvents.RESET_UI_STATES);
-
- // 4. 重新初始化游戏
- setTimeout(() => {
- ReStartGame.reinitializeGame();
- eventBus.emit(GameEvents.GAME_RESET_COMPLETE);
- console.log('[ReStartGame] 游戏状态重置完成');
- }, 100);
- }, 50);
- }
-
-
- /**
- * 重新初始化游戏
- */
- private static reinitializeGame() {
- console.log('[ReStartGame] 开始重新初始化游戏');
-
- // 清理并重新初始化关卡会话数据
- LevelSessionManager.inst.clear();
-
- // 通过事件系统显示方块选择界面
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.SHOW_BLOCK_SELECTION);
-
- console.log('[ReStartGame] 游戏重新初始化完成');
- }
-
- /**
- * 快速重启游戏(用于重新开始按钮)
- */
- public static quickRestart() {
- console.log('[ReStartGame] 快速重启游戏');
-
- // 执行完整重置
- ReStartGame.resetAllGameStates();
- }
-
- /**
- * 通过事件系统重置能量值
- */
- public static resetEnergy() {
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.RESET_ENERGY_SYSTEM);
- console.log('[ReStartGame] 发送能量重置事件');
- }
- }
|