EnemySpawnerExample.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, Vec2, Sprite, SpriteFrame, resources, RigidBody2D, CCFloat, CCInteger } from 'cc';
  2. import { sp } from 'cc';
  3. import { ConfigManager, EnemyConfig } from '../Core/ConfigManager';
  4. import { EnemyComponent } from './EnemyComponent';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 敌人生成器示例脚本
  8. * 展示如何使用ConfigManager来创建不同类型的敌人
  9. */
  10. @ccclass('EnemySpawnerExample')
  11. export class EnemySpawnerExample extends Component {
  12. @property({
  13. type: Prefab,
  14. tooltip: '基础敌人预制体'
  15. })
  16. public enemyPrefab: Prefab = null;
  17. @property({
  18. type: Node,
  19. tooltip: '敌人容器节点'
  20. })
  21. public enemyContainer: Node = null;
  22. @property({
  23. type: CCFloat,
  24. tooltip: '生成间隔(秒)'
  25. })
  26. public spawnInterval: number = 2.0;
  27. @property({
  28. type: CCInteger,
  29. tooltip: '最大敌人数量'
  30. })
  31. public maxEnemies: number = 10;
  32. private configManager: ConfigManager = null;
  33. private currentWave: number = 1;
  34. private currentEnemyCount: number = 0;
  35. private spawnTimer: number = 0;
  36. start() {
  37. // 获取配置管理器实例
  38. this.configManager = ConfigManager.getInstance();
  39. // 等待配置加载完成后开始生成
  40. this.scheduleOnce(() => {
  41. this.startSpawning();
  42. }, 1.0);
  43. }
  44. update(deltaTime: number) {
  45. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  46. return;
  47. }
  48. // 更新生成计时器
  49. this.spawnTimer += deltaTime;
  50. if (this.spawnTimer >= this.spawnInterval && this.currentEnemyCount < this.maxEnemies) {
  51. this.spawnRandomEnemy();
  52. this.spawnTimer = 0;
  53. }
  54. }
  55. // 开始生成敌人
  56. private startSpawning() {
  57. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  58. console.warn('配置管理器未准备好');
  59. return;
  60. }
  61. console.log('开始生成敌人');
  62. this.spawnTimer = 0;
  63. }
  64. // 生成随机敌人
  65. private spawnRandomEnemy() {
  66. // 根据当前波次获取合适的敌人
  67. const enemyConfig = this.configManager.getEnemyForWave(this.currentWave);
  68. if (!enemyConfig) {
  69. console.warn('无法获取敌人配置');
  70. return;
  71. }
  72. this.spawnEnemy(enemyConfig);
  73. }
  74. // 生成指定类型的敌人
  75. public spawnEnemyByType(enemyType: string) {
  76. const enemyConfig = this.configManager.getEnemyById(enemyType);
  77. if (!enemyConfig) {
  78. console.warn(`找不到类型为 ${enemyType} 的敌人配置`);
  79. return;
  80. }
  81. this.spawnEnemy(enemyConfig);
  82. }
  83. // 生成指定稀有度的敌人
  84. public spawnEnemyByRarity(rarity: string) {
  85. const enemyConfig = this.configManager.getRandomEnemy(rarity);
  86. if (!enemyConfig) {
  87. console.warn(`无法获取稀有度为 ${rarity} 的敌人配置`);
  88. return;
  89. }
  90. this.spawnEnemy(enemyConfig);
  91. }
  92. // 根据敌人配置生成敌人
  93. private spawnEnemy(enemyConfig: EnemyConfig) {
  94. if (!this.enemyPrefab) {
  95. console.error('敌人预制体未设置');
  96. return;
  97. }
  98. // 实例化敌人
  99. const enemyNode = instantiate(this.enemyPrefab);
  100. if (!enemyNode) {
  101. console.error('敌人实例化失败');
  102. return;
  103. }
  104. // 设置生成位置(右侧屏幕外)
  105. const spawnPosition = new Vec3(800, Math.random() * 400 - 200, 0);
  106. enemyNode.setPosition(spawnPosition);
  107. // 添加到容器
  108. if (this.enemyContainer) {
  109. this.enemyContainer.addChild(enemyNode);
  110. } else {
  111. this.node.addChild(enemyNode);
  112. }
  113. // 设置敌人配置
  114. this.setupEnemy(enemyNode, enemyConfig);
  115. this.currentEnemyCount++;
  116. console.log(`生成敌人: ${enemyConfig.name} (${enemyConfig.rarity})`);
  117. }
  118. // 设置敌人配置
  119. private setupEnemy(enemyNode: Node, enemyConfig: EnemyConfig) {
  120. // 添加敌人组件
  121. const enemyComponent = enemyNode.addComponent(EnemyComponent);
  122. if (enemyComponent) {
  123. enemyComponent.enemyConfig = enemyConfig;
  124. enemyComponent.spawner = this;
  125. }
  126. // 设置敌人外观
  127. this.setupEnemyVisual(enemyNode, enemyConfig);
  128. // 设置敌人物理
  129. this.setupEnemyPhysics(enemyNode, enemyConfig);
  130. // 设置敌人名称
  131. enemyNode.name = `Enemy_${enemyConfig.id}_${Date.now()}`;
  132. }
  133. // 设置敌人外观
  134. private setupEnemyVisual(enemyNode: Node, enemyConfig: EnemyConfig) {
  135. // 查找Spine动画组件
  136. const spineComponent = enemyNode.getComponent(sp.Skeleton);
  137. if (spineComponent) {
  138. // 如果有Spine组件,设置Spine动画
  139. this.setupEnemySpineAnimation(spineComponent, enemyConfig);
  140. } else {
  141. // 如果没有Spine组件,使用Sprite组件作为后备
  142. const spriteComponent = enemyNode.getComponent(Sprite);
  143. if (spriteComponent) {
  144. // 根据稀有度设置颜色
  145. const rarityColors = {
  146. 'common': { r: 200, g: 200, b: 200, a: 255 }, // 灰色
  147. 'uncommon': { r: 255, g: 255, b: 0, a: 255 }, // 黄色
  148. 'rare': { r: 255, g: 100, b: 0, a: 255 }, // 橙色
  149. 'boss': { r: 255, g: 0, b: 0, a: 255 } // 红色
  150. };
  151. const color = rarityColors[enemyConfig.rarity] || rarityColors['common'];
  152. spriteComponent.color = spriteComponent.color.set(color.r, color.g, color.b, color.a);
  153. }
  154. }
  155. // 设置缩放
  156. enemyNode.setScale(enemyConfig.visualConfig.scale, enemyConfig.visualConfig.scale, 1);
  157. }
  158. // 设置敌人Spine骨骼动画
  159. private setupEnemySpineAnimation(spineComponent: sp.Skeleton, enemyConfig: EnemyConfig) {
  160. const spineDataPath = enemyConfig.visualConfig.spritePrefab;
  161. if (!spineDataPath) {
  162. console.warn(`敌人 ${enemyConfig.name} 没有Spine动画路径`);
  163. return;
  164. }
  165. // 加载Spine动画数据
  166. resources.load(spineDataPath, sp.SkeletonData, (err, skeletonData) => {
  167. if (err) {
  168. console.warn(`加载敌人Spine动画失败: ${spineDataPath}`, err);
  169. return;
  170. }
  171. // 设置Spine数据
  172. spineComponent.skeletonData = skeletonData;
  173. // 播放默认动画(如果有的话)
  174. const animations = enemyConfig.visualConfig.animations;
  175. if (animations && animations.idle) {
  176. spineComponent.setAnimation(0, animations.idle, true);
  177. } else if (animations && animations.walk) {
  178. spineComponent.setAnimation(0, animations.walk, true);
  179. }
  180. console.log(`敌人 ${enemyConfig.name} Spine动画加载成功: ${spineDataPath}`);
  181. });
  182. }
  183. // 设置敌人物理
  184. private setupEnemyPhysics(enemyNode: Node, enemyConfig: EnemyConfig) {
  185. const rigidBody = enemyNode.getComponent(RigidBody2D);
  186. if (rigidBody) {
  187. // 设置线性速度(向左移动)
  188. const speed = enemyConfig.stats.speed;
  189. rigidBody.linearVelocity = new Vec2(-speed, 0);
  190. }
  191. }
  192. // 敌人死亡回调
  193. public onEnemyDeath(enemyNode: Node) {
  194. this.currentEnemyCount--;
  195. // 移除敌人节点
  196. if (enemyNode && enemyNode.isValid) {
  197. enemyNode.destroy();
  198. }
  199. }
  200. // 设置当前波次
  201. public setCurrentWave(wave: number) {
  202. this.currentWave = wave;
  203. console.log(`设置当前波次: ${wave}`);
  204. }
  205. // 获取当前波次
  206. public getCurrentWave(): number {
  207. return this.currentWave;
  208. }
  209. // 获取当前敌人数量
  210. public getCurrentEnemyCount(): number {
  211. return this.currentEnemyCount;
  212. }
  213. // 清除所有敌人
  214. public clearAllEnemies() {
  215. if (this.enemyContainer) {
  216. this.enemyContainer.destroyAllChildren();
  217. }
  218. this.currentEnemyCount = 0;
  219. }
  220. // 暂停生成
  221. public pauseSpawning() {
  222. this.spawnInterval = Number.MAX_VALUE;
  223. }
  224. // 恢复生成
  225. public resumeSpawning(interval: number = 2.0) {
  226. this.spawnInterval = interval;
  227. this.spawnTimer = 0;
  228. }
  229. }