ReStartGame.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { _decorator, Component, find } from 'cc';
  2. import { LevelSessionManager } from '../Core/LevelSessionManager';
  3. import EventBus, { GameEvents } from '../Core/EventBus';
  4. const { ccclass } = _decorator;
  5. /**
  6. * 游戏重置管理器
  7. * 负责协调各个组件的重置操作,使用事件系统解耦
  8. */
  9. @ccclass('ReStartGame')
  10. export class ReStartGame extends Component {
  11. start() {
  12. // 监听游戏重启事件
  13. const eventBus = EventBus.getInstance();
  14. eventBus.on(GameEvents.GAME_RESTART, this.onGameRestartEvent, this);
  15. }
  16. onDestroy() {
  17. // 移除事件监听
  18. const eventBus = EventBus.getInstance();
  19. eventBus.off(GameEvents.GAME_RESTART, this.onGameRestartEvent, this);
  20. }
  21. /**
  22. * 处理游戏重启事件
  23. */
  24. private onGameRestartEvent() {
  25. console.log('[ReStartGame] 接收到游戏重启事件,开始重置游戏状态');
  26. ReStartGame.resetAllGameStates();
  27. }
  28. /**
  29. * 完整重置游戏状态
  30. * 在关闭成功/失败界面时调用
  31. */
  32. public static resetAllGameStates() {
  33. console.log('[ReStartGame] 开始完整重置游戏状态');
  34. const eventBus = EventBus.getInstance();
  35. // 发送游戏重置请求事件
  36. eventBus.emit(GameEvents.GAME_RESET_REQUEST);
  37. // 按顺序发送各个组件的重置事件
  38. setTimeout(() => {
  39. // 1. 清理所有游戏对象
  40. eventBus.emit(GameEvents.CLEAR_ALL_GAME_OBJECTS);
  41. // 2. 重置各个组件状态
  42. eventBus.emit(GameEvents.RESET_GAME_MANAGER);
  43. eventBus.emit(GameEvents.RESET_ENEMY_CONTROLLER);
  44. eventBus.emit(GameEvents.RESET_BALL_CONTROLLER);
  45. eventBus.emit(GameEvents.RESET_BLOCK_MANAGER);
  46. eventBus.emit(GameEvents.RESET_BLOCK_SELECTION);
  47. eventBus.emit(GameEvents.RESET_WALL_HEALTH);
  48. eventBus.emit(GameEvents.RESET_GAME_PAUSE);
  49. eventBus.emit(GameEvents.RESET_ENERGY_SYSTEM);
  50. // 3. 重置UI状态
  51. eventBus.emit(GameEvents.RESET_UI_STATES);
  52. // 4. 重新初始化游戏
  53. setTimeout(() => {
  54. ReStartGame.reinitializeGame();
  55. eventBus.emit(GameEvents.GAME_RESET_COMPLETE);
  56. console.log('[ReStartGame] 游戏状态重置完成');
  57. }, 100);
  58. }, 50);
  59. }
  60. /**
  61. * 重新初始化游戏
  62. */
  63. private static reinitializeGame() {
  64. console.log('[ReStartGame] 开始重新初始化游戏');
  65. // 清理并重新初始化关卡会话数据
  66. LevelSessionManager.inst.clear();
  67. // 通过事件系统显示方块选择界面
  68. const eventBus = EventBus.getInstance();
  69. eventBus.emit(GameEvents.SHOW_BLOCK_SELECTION);
  70. console.log('[ReStartGame] 游戏重新初始化完成');
  71. }
  72. /**
  73. * 快速重启游戏(用于重新开始按钮)
  74. */
  75. public static quickRestart() {
  76. console.log('[ReStartGame] 快速重启游戏');
  77. // 执行完整重置
  78. ReStartGame.resetAllGameStates();
  79. }
  80. /**
  81. * 通过事件系统重置能量值
  82. */
  83. public static resetEnergy() {
  84. const eventBus = EventBus.getInstance();
  85. eventBus.emit(GameEvents.RESET_ENERGY_SYSTEM);
  86. console.log('[ReStartGame] 发送能量重置事件');
  87. }
  88. }