GamePauseTest.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { _decorator, Component, Node } from 'cc';
  2. import EventBus, { GameEvents } from '../Core/EventBus';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * GamePause测试脚本
  6. * 用于测试游戏状态管理和事件流程
  7. */
  8. @ccclass('GamePauseTest')
  9. export class GamePauseTest extends Component {
  10. start() {
  11. // 延迟执行测试,确保所有组件都已初始化
  12. this.scheduleOnce(() => {
  13. this.runTests();
  14. }, 2);
  15. }
  16. private runTests() {
  17. console.log('=== GamePause测试开始 ===');
  18. // 测试1:检查GamePause实例
  19. this.testGamePauseInstance();
  20. // 测试2:测试游戏开始事件
  21. this.testGameStartEvent();
  22. // 测试3:测试游戏失败触发
  23. this.testGameDefeatTrigger();
  24. console.log('=== GamePause测试完成 ===');
  25. }
  26. private testGamePauseInstance() {
  27. console.log('--- 测试1:GamePause状态检查 ---');
  28. // 通过事件系统检查游戏状态
  29. const eventBus = EventBus.getInstance();
  30. // 检查游戏是否结束
  31. let isGameOver = false;
  32. eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
  33. isGameOver = result;
  34. });
  35. console.log('✓ 通过事件系统检查游戏状态');
  36. console.log('是否游戏结束:', isGameOver);
  37. }
  38. private testGameStartEvent() {
  39. console.log('--- 测试2:游戏开始事件测试 ---');
  40. // 发送游戏开始事件
  41. const eventBus = EventBus.getInstance();
  42. console.log('发送GAME_START事件...');
  43. eventBus.emit(GameEvents.GAME_START);
  44. // 检查状态
  45. this.scheduleOnce(() => {
  46. console.log('游戏开始事件发送后的状态检查:');
  47. // 通过事件系统检查游戏状态
  48. let isGameOver = false;
  49. eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
  50. isGameOver = result;
  51. });
  52. console.log('是否游戏结束:', isGameOver);
  53. }, 0.1);
  54. }
  55. private testGameDefeatTrigger() {
  56. console.log('--- 测试3:游戏失败触发测试 ---');
  57. // 延迟执行,确保游戏开始事件已处理
  58. this.scheduleOnce(() => {
  59. console.log('尝试触发游戏失败...');
  60. // 通过事件系统触发游戏失败
  61. const eventBus = EventBus.getInstance();
  62. eventBus.emit(GameEvents.GAME_DEFEAT);
  63. // 检查结果
  64. this.scheduleOnce(() => {
  65. console.log('游戏失败触发后的状态检查:');
  66. // 通过事件系统检查游戏状态
  67. let isGameOver = false;
  68. eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
  69. isGameOver = result;
  70. });
  71. console.log('是否游戏结束:', isGameOver);
  72. if (isGameOver) {
  73. console.log('✓ 游戏失败触发成功');
  74. } else {
  75. console.log('✗ 游戏失败触发失败');
  76. }
  77. }, 0.1);
  78. }, 0.5);
  79. }
  80. /**
  81. * 手动测试方法 - 可以在控制台调用
  82. */
  83. public manualTestGameStart() {
  84. console.log('手动测试:发送游戏开始事件');
  85. const eventBus = EventBus.getInstance();
  86. eventBus.emit(GameEvents.GAME_START);
  87. }
  88. public manualTestGameDefeat() {
  89. console.log('手动测试:触发游戏失败');
  90. const eventBus = EventBus.getInstance();
  91. eventBus.emit(GameEvents.GAME_DEFEAT);
  92. }
  93. public checkGamePauseStatus() {
  94. console.log('检查GamePause状态:');
  95. const eventBus = EventBus.getInstance();
  96. // 通过事件系统检查游戏状态
  97. let isGameOver = false;
  98. eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
  99. isGameOver = result;
  100. });
  101. let canFireBullet = true;
  102. eventBus.emit(GameEvents.BALL_FIRE_BULLET, { canFire: (result: boolean) => { canFireBullet = result; } });
  103. console.log('是否游戏结束:', isGameOver);
  104. console.log('是否允许发射子弹:', canFireBullet);
  105. }
  106. }