| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import { _decorator, Component, Node } from 'cc';
- import EventBus, { GameEvents } from '../Core/EventBus';
- const { ccclass, property } = _decorator;
- /**
- * GamePause测试脚本
- * 用于测试游戏状态管理和事件流程
- */
- @ccclass('GamePauseTest')
- export class GamePauseTest extends Component {
- start() {
- // 延迟执行测试,确保所有组件都已初始化
- this.scheduleOnce(() => {
- this.runTests();
- }, 2);
- }
- private runTests() {
- console.log('=== GamePause测试开始 ===');
-
- // 测试1:检查GamePause实例
- this.testGamePauseInstance();
-
- // 测试2:测试游戏开始事件
- this.testGameStartEvent();
-
- // 测试3:测试游戏失败触发
- this.testGameDefeatTrigger();
-
- console.log('=== GamePause测试完成 ===');
- }
- private testGamePauseInstance() {
- console.log('--- 测试1:GamePause状态检查 ---');
-
- // 通过事件系统检查游戏状态
- const eventBus = EventBus.getInstance();
-
- // 检查游戏是否结束
- let isGameOver = false;
- eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
- isGameOver = result;
- });
-
- console.log('✓ 通过事件系统检查游戏状态');
- console.log('是否游戏结束:', isGameOver);
- }
- private testGameStartEvent() {
- console.log('--- 测试2:游戏开始事件测试 ---');
-
- // 发送游戏开始事件
- const eventBus = EventBus.getInstance();
- console.log('发送GAME_START事件...');
- eventBus.emit(GameEvents.GAME_START);
-
- // 检查状态
- this.scheduleOnce(() => {
- console.log('游戏开始事件发送后的状态检查:');
- // 通过事件系统检查游戏状态
- let isGameOver = false;
- eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
- isGameOver = result;
- });
- console.log('是否游戏结束:', isGameOver);
- }, 0.1);
- }
- private testGameDefeatTrigger() {
- console.log('--- 测试3:游戏失败触发测试 ---');
-
- // 延迟执行,确保游戏开始事件已处理
- this.scheduleOnce(() => {
- console.log('尝试触发游戏失败...');
- // 通过事件系统触发游戏失败
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_DEFEAT);
-
- // 检查结果
- this.scheduleOnce(() => {
- console.log('游戏失败触发后的状态检查:');
- // 通过事件系统检查游戏状态
- let isGameOver = false;
- eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
- isGameOver = result;
- });
- console.log('是否游戏结束:', isGameOver);
-
- if (isGameOver) {
- console.log('✓ 游戏失败触发成功');
- } else {
- console.log('✗ 游戏失败触发失败');
- }
- }, 0.1);
- }, 0.5);
- }
- /**
- * 手动测试方法 - 可以在控制台调用
- */
- public manualTestGameStart() {
- console.log('手动测试:发送游戏开始事件');
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_START);
- }
- public manualTestGameDefeat() {
- console.log('手动测试:触发游戏失败');
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_DEFEAT);
- }
- public checkGamePauseStatus() {
- console.log('检查GamePause状态:');
- const eventBus = EventBus.getInstance();
-
- // 通过事件系统检查游戏状态
- let isGameOver = false;
- eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
- isGameOver = result;
- });
-
- let canFireBullet = true;
- eventBus.emit(GameEvents.BALL_FIRE_BULLET, { canFire: (result: boolean) => { canFireBullet = result; } });
-
- console.log('是否游戏结束:', isGameOver);
- console.log('是否允许发射子弹:', canFireBullet);
- }
- }
|