EnemyController.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import { _decorator, Node, Label, Vec3, Prefab, instantiate, find, UITransform, resources } from 'cc';
  2. import { sp } from 'cc';
  3. import { ConfigManager, EnemyConfig } from '../Core/ConfigManager';
  4. import { EnemyComponent } from '../CombatSystem/EnemyComponent';
  5. import { EnemyInstance } from './EnemyInstance';
  6. import { BaseSingleton } from '../Core/BaseSingleton';
  7. const { ccclass, property } = _decorator;
  8. // 前向声明EnemyInstance类型,避免循环引用
  9. class EnemyInstanceType {
  10. public health: number;
  11. public maxHealth: number;
  12. public speed: number;
  13. public attackPower: number;
  14. public movingDirection: number;
  15. public targetY: number;
  16. public changeDirectionTime: number;
  17. public controller: any;
  18. public node: Node;
  19. public updateHealthDisplay: () => void;
  20. public takeDamage: (damage: number) => void;
  21. }
  22. @ccclass('EnemyController')
  23. export class EnemyController extends BaseSingleton {
  24. // 仅类型声明,实例由 BaseSingleton 维护
  25. public static _instance: EnemyController;
  26. // 敌人预制体
  27. @property({
  28. type: Prefab,
  29. tooltip: '拖拽Enemy预制体到这里'
  30. })
  31. public enemyPrefab: Prefab = null;
  32. // 敌人容器节点
  33. @property({
  34. type: Node,
  35. tooltip: '拖拽enemyContainer节点到这里(Canvas/GameLevelUI/enemyContainer)'
  36. })
  37. public enemyContainer: Node = null;
  38. // === 生成 & 属性参数(保留需要可在内部自行设定,Inspector 不再显示) ===
  39. private spawnInterval: number = 3;
  40. // === 默认数值(当配置文件尚未加载时使用) ===
  41. private defaultEnemySpeed: number = 50;
  42. private defaultAttackPower: number = 10;
  43. private defaultHealth: number = 30;
  44. // 墙体属性
  45. @property({
  46. tooltip: '墙体初始血量'
  47. })
  48. public wallHealth: number = 1200;
  49. @property({
  50. type: Node,
  51. tooltip: '墙体血量显示节点'
  52. })
  53. public wallHealthNode: Node = null;
  54. // 游戏区域边界 - 改为public,让敌人实例可以访问
  55. public gameBounds = {
  56. left: 0,
  57. right: 0,
  58. top: 0,
  59. bottom: 0
  60. };
  61. // 活跃的敌人列表
  62. private activeEnemies: Node[] = [];
  63. // 游戏是否已开始
  64. private gameStarted: boolean = false;
  65. // 墙体节点
  66. private wallNodes: Node[] = [];
  67. // 私有属性
  68. private gameManager: any = null;
  69. // 配置管理器
  70. private configManager: ConfigManager = null;
  71. /**
  72. * BaseSingleton 首次实例化回调
  73. */
  74. protected init() {
  75. // 获取配置管理器实例
  76. this.configManager = ConfigManager.getInstance();
  77. // 如果没有指定enemyContainer,尝试找到它
  78. if (!this.enemyContainer) {
  79. this.enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
  80. if (!this.enemyContainer) {
  81. console.warn('找不到enemyContainer节点,将尝试创建');
  82. }
  83. }
  84. // 获取游戏区域边界
  85. this.calculateGameBounds();
  86. // 查找墙体节点
  87. this.findWallNodes();
  88. // 初始化墙体血量显示
  89. this.initWallHealthDisplay();
  90. // 确保enemyContainer节点存在
  91. this.ensureEnemyContainer();
  92. // 查找GameManager
  93. this.findGameManager();
  94. }
  95. // 计算游戏区域边界
  96. private calculateGameBounds() {
  97. const gameArea = find('Canvas/GameLevelUI/GameArea');
  98. if (!gameArea) {
  99. return;
  100. }
  101. const uiTransform = gameArea.getComponent(UITransform);
  102. if (!uiTransform) {
  103. return;
  104. }
  105. const worldPos = gameArea.worldPosition;
  106. const width = uiTransform.width;
  107. const height = uiTransform.height;
  108. this.gameBounds = {
  109. left: worldPos.x - width / 2,
  110. right: worldPos.x + width / 2,
  111. top: worldPos.y + height / 2,
  112. bottom: worldPos.y - height / 2
  113. };
  114. }
  115. // 查找墙体节点
  116. private findWallNodes() {
  117. const gameArea = find('Canvas/GameLevelUI/GameArea');
  118. if (gameArea) {
  119. this.wallNodes = [];
  120. for (let i = 0; i < gameArea.children.length; i++) {
  121. const child = gameArea.children[i];
  122. if (child.name.includes('Wall') || child.name.includes('wall') || child.name.includes('墙')) {
  123. this.wallNodes.push(child);
  124. }
  125. }
  126. }
  127. }
  128. // 初始化墙体血量显示
  129. initWallHealthDisplay() {
  130. if (!this.wallHealthNode) {
  131. // 尝试查找墙体血量显示节点
  132. this.wallHealthNode = find('Canvas/GameLevelUI/HeartNode');
  133. }
  134. if (this.wallHealthNode) {
  135. // 更新墙体血量显示
  136. this.updateWallHealthDisplay();
  137. } else {
  138. console.warn('未设置墙体血量显示节点');
  139. }
  140. }
  141. // 更新墙体血量显示
  142. public updateWallHealthDisplay() {
  143. if (!this.wallHealthNode) {
  144. return;
  145. }
  146. const heartLabel = this.wallHealthNode.getComponent(Label);
  147. if (heartLabel) {
  148. heartLabel.string = this.wallHealth.toString();
  149. }
  150. }
  151. // 确保enemyContainer节点存在
  152. ensureEnemyContainer() {
  153. // 如果已经通过拖拽设置了节点,直接使用
  154. if (this.enemyContainer && this.enemyContainer.isValid) {
  155. return;
  156. }
  157. // 尝试查找节点
  158. this.enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
  159. if (this.enemyContainer) {
  160. console.log('找到已存在的enemyContainer节点');
  161. return;
  162. }
  163. // 如果找不到,创建新节点
  164. const gameLevelUI = find('Canvas/GameLevelUI');
  165. if (!gameLevelUI) {
  166. console.error('找不到GameLevelUI节点,无法创建enemyContainer');
  167. return;
  168. }
  169. this.enemyContainer = new Node('enemyContainer');
  170. gameLevelUI.addChild(this.enemyContainer);
  171. if (!this.enemyContainer.getComponent(UITransform)) {
  172. this.enemyContainer.addComponent(UITransform);
  173. }
  174. console.log('已在GameLevelUI下创建enemyContainer节点');
  175. }
  176. // 游戏开始
  177. startGame() {
  178. this.gameStarted = true;
  179. // 确保enemyContainer节点存在
  180. this.ensureEnemyContainer();
  181. // 开始生成敌人
  182. this.schedule(this.spawnEnemy, this.spawnInterval);
  183. console.log('开始生成敌人');
  184. }
  185. // 游戏结束
  186. stopGame() {
  187. this.gameStarted = false;
  188. // 停止生成敌人
  189. this.unschedule(this.spawnEnemy);
  190. // 清除所有敌人
  191. this.clearAllEnemies();
  192. console.log('停止生成敌人');
  193. }
  194. // 生成敌人
  195. spawnEnemy() {
  196. if (!this.gameStarted || !this.enemyPrefab) return;
  197. // 随机决定从上方还是下方生成
  198. const fromTop = Math.random() > 0.5;
  199. // 实例化敌人
  200. const enemy = instantiate(this.enemyPrefab);
  201. enemy.name = 'Enemy'; // 确保敌人节点名称为Enemy
  202. // 添加到场景中
  203. const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
  204. if (!enemyContainer) {
  205. return;
  206. }
  207. enemyContainer.addChild(enemy);
  208. // 设置敌人位置
  209. const xPos = this.gameBounds.left + Math.random() * (this.gameBounds.right - this.gameBounds.left);
  210. const yPos = fromTop ? this.gameBounds.top + 100 : this.gameBounds.bottom - 100;
  211. // 将世界坐标转换为相对于enemyContainer的本地坐标
  212. const localPos = enemyContainer.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(xPos, yPos, 0));
  213. enemy.position = localPos;
  214. // === 根据配置设置敌人 ===
  215. const enemyComp = enemy.addComponent(EnemyInstance);
  216. let enemyConfig: EnemyConfig = null;
  217. if (this.configManager && this.configManager.isConfigLoaded()) {
  218. enemyConfig = this.configManager.getRandomEnemy();
  219. }
  220. if (enemyConfig) {
  221. // 添加EnemyComponent保存配置
  222. const cfgComp = enemy.addComponent(EnemyComponent);
  223. cfgComp.enemyConfig = enemyConfig;
  224. cfgComp.spawner = this;
  225. // 应用数值
  226. enemyComp.health = enemyConfig.stats.health;
  227. enemyComp.maxHealth = enemyConfig.stats.health;
  228. enemyComp.speed = enemyConfig.stats.speed;
  229. enemyComp.attackPower = enemyConfig.stats.damage;
  230. // 加载动画
  231. this.loadEnemyAnimation(enemy, enemyConfig);
  232. } else {
  233. // 使用默认值
  234. enemyComp.health = this.defaultHealth;
  235. enemyComp.maxHealth = this.defaultHealth;
  236. enemyComp.speed = this.defaultEnemySpeed;
  237. enemyComp.attackPower = this.defaultAttackPower;
  238. }
  239. enemyComp.movingDirection = Math.random() > 0.5 ? 1 : -1;
  240. enemyComp.targetY = fromTop ? this.gameBounds.top - 50 : this.gameBounds.bottom + 50;
  241. enemyComp.changeDirectionTime = 0;
  242. enemyComp.controller = this;
  243. // 更新敌人血量显示
  244. enemyComp.updateHealthDisplay();
  245. // 添加到活跃敌人列表
  246. this.activeEnemies.push(enemy);
  247. console.log(`生成敌人,当前共有 ${this.activeEnemies.length} 个敌人`);
  248. }
  249. // 清除所有敌人
  250. clearAllEnemies() {
  251. for (const enemy of this.activeEnemies) {
  252. if (enemy && enemy.isValid) {
  253. enemy.destroy();
  254. }
  255. }
  256. this.activeEnemies = [];
  257. }
  258. // 获取所有活跃的敌人
  259. getActiveEnemies(): Node[] {
  260. // 过滤掉已经无效的敌人
  261. this.activeEnemies = this.activeEnemies.filter(enemy => enemy && enemy.isValid);
  262. return this.activeEnemies;
  263. }
  264. // 获取当前敌人数量
  265. getCurrentEnemyCount(): number {
  266. return this.getActiveEnemies().length;
  267. }
  268. // 获取游戏是否已开始状态
  269. public isGameStarted(): boolean {
  270. return this.gameStarted;
  271. }
  272. // 暂停生成敌人
  273. public pauseSpawning(): void {
  274. if (this.gameStarted) {
  275. this.unschedule(this.spawnEnemy);
  276. }
  277. }
  278. // 恢复生成敌人
  279. public resumeSpawning(): void {
  280. if (this.gameStarted) {
  281. this.schedule(this.spawnEnemy, this.spawnInterval);
  282. }
  283. }
  284. // 敌人受到伤害
  285. damageEnemy(enemy: Node, damage: number) {
  286. if (!enemy || !enemy.isValid) return;
  287. // 获取敌人组件
  288. const enemyComp = enemy.getComponent(EnemyInstance);
  289. if (!enemyComp) return;
  290. // 减少敌人血量
  291. enemyComp.takeDamage(damage);
  292. // 检查敌人是否死亡
  293. if (enemyComp.health <= 0) {
  294. // 从活跃敌人列表中移除
  295. const index = this.activeEnemies.indexOf(enemy);
  296. if (index !== -1) {
  297. this.activeEnemies.splice(index, 1);
  298. }
  299. // 销毁敌人(在 onDestroy 中由 EnemyInstance -> notifyEnemyDead 计数)
  300. enemy.destroy();
  301. }
  302. }
  303. // 墙体受到伤害
  304. damageWall(damage: number) {
  305. // 减少墙体血量
  306. this.wallHealth -= damage;
  307. // 更新墙体血量显示
  308. this.updateWallHealthDisplay();
  309. // 检查墙体是否被摧毁
  310. if (this.wallHealth <= 0) {
  311. // 游戏结束
  312. this.gameOver();
  313. }
  314. }
  315. // 游戏结束
  316. gameOver() {
  317. // 停止游戏
  318. this.stopGame();
  319. // 通知GameManager游戏结束
  320. const gameManagerNode = find('Canvas/GameLevelUI/GameManager');
  321. if (gameManagerNode) {
  322. const gameManager = gameManagerNode.getComponent('GameManager') as any;
  323. if (gameManager) {
  324. gameManager.gameOver();
  325. }
  326. }
  327. }
  328. update(dt: number) {
  329. if (!this.gameStarted) return;
  330. // 更新所有敌人
  331. for (let i = this.activeEnemies.length - 1; i >= 0; i--) {
  332. const enemy = this.activeEnemies[i];
  333. if (!enemy || !enemy.isValid) {
  334. this.activeEnemies.splice(i, 1);
  335. continue;
  336. }
  337. // 敌人更新由各自的组件处理
  338. // 不再需要检查敌人是否到达墙体,因为敌人到达游戏区域后会自动攻击
  339. // 敌人的攻击逻辑已经在EnemyInstance中处理
  340. }
  341. }
  342. // === 调试方法 ===
  343. public testEnemyAttack() {
  344. console.log('=== 测试敌人攻击墙体 ===');
  345. // 手动触发墙体受到伤害
  346. const testDamage = 50;
  347. console.log(`模拟敌人攻击,伤害: ${testDamage}`);
  348. this.damageWall(testDamage);
  349. return this.wallHealth;
  350. }
  351. public getCurrentWallHealth(): number {
  352. return this.wallHealth;
  353. }
  354. public forceEnemyAttack() {
  355. console.log('=== 强制所有敌人进入攻击状态 ===');
  356. const activeEnemies = this.getActiveEnemies();
  357. console.log(`当前活跃敌人数量: ${activeEnemies.length}`);
  358. for (const enemy of activeEnemies) {
  359. const enemyComp = enemy.getComponent(EnemyInstance);
  360. if (enemyComp) {
  361. console.log(`强制敌人 ${enemy.name} 进入攻击状态`);
  362. // 直接调用damageWall方法进行测试
  363. this.damageWall(enemyComp.attackPower);
  364. }
  365. }
  366. }
  367. // === 查找GameManager ===
  368. private findGameManager() {
  369. const gameManagerNode = find('Canvas/GameLevelUI/GameManager');
  370. if (gameManagerNode) {
  371. this.gameManager = gameManagerNode.getComponent('GameManager');
  372. }
  373. }
  374. /** 供 EnemyInstance 在 onDestroy 中调用 */
  375. public notifyEnemyDead(enemyNode?: Node) {
  376. if (enemyNode) {
  377. const idx = this.activeEnemies.indexOf(enemyNode);
  378. if (idx !== -1) this.activeEnemies.splice(idx, 1);
  379. }
  380. if (this.gameManager && this.gameManager.onEnemyKilled) {
  381. this.gameManager.onEnemyKilled();
  382. }
  383. }
  384. /**
  385. * 加载敌人骨骼动画
  386. */
  387. private loadEnemyAnimation(enemyNode: Node, enemyConfig: EnemyConfig) {
  388. let spinePath: string | undefined = enemyConfig.visualConfig?.spritePrefab;
  389. if (!spinePath) return;
  390. if (spinePath.startsWith('@EnemyAni')) {
  391. spinePath = spinePath.replace('@EnemyAni', 'Animation/EnemyAni');
  392. }
  393. if (spinePath.startsWith('@')) {
  394. spinePath = spinePath.substring(1);
  395. }
  396. resources.load(spinePath, sp.SkeletonData, (err, skeletonData) => {
  397. if (err) {
  398. console.warn(`加载敌人Spine动画失败: ${spinePath}`, err);
  399. return;
  400. }
  401. let skeleton = enemyNode.getComponent(sp.Skeleton);
  402. if (!skeleton) {
  403. skeleton = enemyNode.addComponent(sp.Skeleton);
  404. }
  405. skeleton.skeletonData = skeletonData;
  406. const anims = enemyConfig.visualConfig.animations;
  407. const walkName = anims?.walk ?? 'walk';
  408. const idleName = anims?.idle ?? 'idle';
  409. if (skeleton.findAnimation(walkName)) {
  410. skeleton.setAnimation(0, walkName, true);
  411. } else if (skeleton.findAnimation(idleName)) {
  412. skeleton.setAnimation(0, idleName, true);
  413. }
  414. });
  415. }
  416. }