EnemySpawnerTestScene.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { _decorator, Component, Node, Prefab, Canvas, UITransform, Vec3, input, Input, EventKeyboard, KeyCode, director } from 'cc';
  2. import { EnemySpawnerExample } from './EnemySpawnerExample';
  3. import { ConfigManager } from '../Core/ConfigManager';
  4. const { ccclass, property } = _decorator;
  5. /**
  6. * 敌人生成器测试场景
  7. * 用于测试敌人配置系统的完整示例
  8. */
  9. @ccclass('EnemySpawnerTestScene')
  10. export class EnemySpawnerTestScene extends Component {
  11. @property({
  12. type: EnemySpawnerExample,
  13. tooltip: '敌人生成器组件'
  14. })
  15. public enemySpawner: EnemySpawnerExample = null;
  16. start() {
  17. console.log('=== 敌人生成器测试场景启动 ===');
  18. this.setupTestScene();
  19. this.setupKeyboardControls();
  20. }
  21. private async setupTestScene() {
  22. // 等待配置管理器加载完成
  23. const configManager = ConfigManager.getInstance();
  24. // 等待配置加载
  25. await new Promise<void>((resolve) => {
  26. const checkConfig = () => {
  27. if (configManager.isConfigLoaded()) {
  28. resolve();
  29. } else {
  30. this.scheduleOnce(checkConfig, 0.1);
  31. }
  32. };
  33. checkConfig();
  34. });
  35. console.log('✅ 配置管理器已加载');
  36. // 显示可用敌人信息
  37. this.displayEnemyInfo();
  38. // 创建测试说明
  39. this.createTestInstructions();
  40. }
  41. private displayEnemyInfo() {
  42. const configManager = ConfigManager.getInstance();
  43. const allEnemies = configManager.getAllEnemies();
  44. console.log('👹 可用敌人列表:');
  45. allEnemies.forEach(enemy => {
  46. console.log(` - ${enemy.name} (${enemy.id}) - ${enemy.rarity} - HP:${enemy.stats.health}`);
  47. });
  48. // 按稀有度统计
  49. const rarityCount = {
  50. common: configManager.getEnemiesByRarity('common').length,
  51. uncommon: configManager.getEnemiesByRarity('uncommon').length,
  52. rare: configManager.getEnemiesByRarity('rare').length,
  53. boss: configManager.getEnemiesByRarity('boss').length
  54. };
  55. console.log('📊 敌人稀有度统计:');
  56. console.log(` 普通: ${rarityCount.common}个`);
  57. console.log(` 稀有: ${rarityCount.uncommon}个`);
  58. console.log(` 史诗: ${rarityCount.rare}个`);
  59. console.log(` BOSS: ${rarityCount.boss}个`);
  60. }
  61. private createTestInstructions() {
  62. console.log('🎯 敌人生成器测试说明:');
  63. console.log('键盘控制:');
  64. console.log(' 1 - 生成普通敌人');
  65. console.log(' 2 - 生成稀有敌人');
  66. console.log(' 3 - 生成史诗敌人');
  67. console.log(' B - 生成BOSS');
  68. console.log(' R - 生成随机敌人');
  69. console.log(' C - 清除所有敌人');
  70. console.log(' P - 暂停/恢复生成');
  71. console.log(' W - 下一波次');
  72. console.log(' S - 显示状态信息');
  73. }
  74. private setupKeyboardControls() {
  75. input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  76. }
  77. private onKeyDown(event: EventKeyboard) {
  78. if (!this.enemySpawner) {
  79. console.warn('敌人生成器未设置');
  80. return;
  81. }
  82. switch (event.keyCode) {
  83. case KeyCode.DIGIT_1:
  84. this.enemySpawner.spawnEnemyByRarity('common');
  85. console.log('🟢 生成普通敌人');
  86. break;
  87. case KeyCode.DIGIT_2:
  88. this.enemySpawner.spawnEnemyByRarity('uncommon');
  89. console.log('🟡 生成稀有敌人');
  90. break;
  91. case KeyCode.DIGIT_3:
  92. this.enemySpawner.spawnEnemyByRarity('rare');
  93. console.log('🔵 生成史诗敌人');
  94. break;
  95. case KeyCode.KEY_B:
  96. this.enemySpawner.spawnEnemyByRarity('boss');
  97. console.log('🔴 生成BOSS');
  98. break;
  99. case KeyCode.KEY_R:
  100. this.spawnRandomEnemy();
  101. console.log('🎲 生成随机敌人');
  102. break;
  103. case KeyCode.KEY_C:
  104. this.enemySpawner.clearAllEnemies();
  105. console.log('🧹 清除所有敌人');
  106. break;
  107. case KeyCode.KEY_P:
  108. this.toggleSpawning();
  109. break;
  110. case KeyCode.KEY_W:
  111. this.nextWave();
  112. break;
  113. case KeyCode.KEY_S:
  114. this.showStatus();
  115. break;
  116. }
  117. }
  118. private spawnRandomEnemy() {
  119. // 生成随机敌人类型
  120. const enemyTypes = [
  121. 'normal_zombie', 'roadblock_zombie', 'wandering_zombie',
  122. 'mage_zombie', 'archer_zombie', 'stealth_zombie',
  123. 'bucket_zombie', 'barrel_zombie'
  124. ];
  125. const randomType = enemyTypes[Math.floor(Math.random() * enemyTypes.length)];
  126. this.enemySpawner.spawnEnemyByType(randomType);
  127. }
  128. private isPaused = false;
  129. private toggleSpawning() {
  130. if (this.isPaused) {
  131. this.enemySpawner.resumeSpawning(2.0);
  132. console.log('▶️ 恢复敌人生成');
  133. } else {
  134. this.enemySpawner.pauseSpawning();
  135. console.log('⏸️ 暂停敌人生成');
  136. }
  137. this.isPaused = !this.isPaused;
  138. }
  139. private nextWave() {
  140. const currentWave = this.enemySpawner.getCurrentWave();
  141. const nextWave = currentWave + 1;
  142. this.enemySpawner.setCurrentWave(nextWave);
  143. console.log(`🌊 切换到第 ${nextWave} 波`);
  144. }
  145. private showStatus() {
  146. const currentWave = this.enemySpawner.getCurrentWave();
  147. const enemyCount = this.enemySpawner.getCurrentEnemyCount();
  148. console.log('📋 当前状态:');
  149. console.log(` 当前波次: ${currentWave}`);
  150. console.log(` 敌人数量: ${enemyCount}`);
  151. console.log(` 生成状态: ${this.isPaused ? '暂停' : '进行中'}`);
  152. }
  153. onDestroy() {
  154. input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  155. }
  156. }
  157. /**
  158. * 敌人生成器设置指南
  159. */
  160. export class EnemySpawnerSetupGuide {
  161. /**
  162. * 在Cocos Creator编辑器中的设置步骤:
  163. *
  164. * 1. 创建场景节点结构:
  165. * Canvas
  166. * ├── ConfigManager (挂载 ConfigManager.ts)
  167. * ├── TestScene (挂载 EnemySpawnerTestScene.ts)
  168. * └── EnemyTest
  169. * ├── EnemySpawner (挂载 EnemySpawnerExample.ts)
  170. * └── EnemyContainer (空节点,用于容纳生成的敌人)
  171. *
  172. * 2. 创建敌人预制体:
  173. * - 创建一个Node,命名为 Enemy
  174. * - 添加Sprite组件 (用于显示敌人图标)
  175. * - 添加RigidBody2D组件 (用于物理移动)
  176. * - 添加Collider2D组件 (用于碰撞检测)
  177. * - 设置合适的尺寸和物理属性
  178. * - 保存为预制体
  179. *
  180. * 3. 配置EnemySpawnerExample组件:
  181. * - Enemy Prefab: 拖入刚创建的敌人预制体
  182. * - Enemy Container: 拖入EnemyContainer节点
  183. * - Spawn Interval: 设置生成间隔 (默认2秒)
  184. * - Max Enemies: 设置最大敌人数量 (默认10个)
  185. *
  186. * 4. 配置EnemySpawnerTestScene组件:
  187. * - Enemy Spawner: 拖入EnemySpawner节点的EnemySpawnerExample组件
  188. *
  189. * 5. 运行测试:
  190. * - 播放场景
  191. * - 使用键盘控制生成敌人
  192. * - 查看控制台输出
  193. * - 观察敌人移动和行为
  194. */
  195. }