EnemyController.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. import { SaveDataManager } from '../LevelSystem/SaveDataManager';
  8. const { ccclass, property } = _decorator;
  9. // 前向声明EnemyInstance类型,避免循环引用
  10. class EnemyInstanceType {
  11. public health: number;
  12. public maxHealth: number;
  13. public speed: number;
  14. public attackPower: number;
  15. public movingDirection: number;
  16. public targetY: number;
  17. public changeDirectionTime: number;
  18. public controller: any;
  19. public node: Node;
  20. public updateHealthDisplay: () => void;
  21. public takeDamage: (damage: number) => void;
  22. }
  23. @ccclass('EnemyController')
  24. export class EnemyController extends BaseSingleton {
  25. // 仅类型声明,实例由 BaseSingleton 维护
  26. public static _instance: EnemyController;
  27. // 敌人预制体
  28. @property({
  29. type: Prefab,
  30. tooltip: '拖拽Enemy预制体到这里'
  31. })
  32. public enemyPrefab: Prefab = null;
  33. // 敌人容器节点
  34. @property({
  35. type: Node,
  36. tooltip: '拖拽enemyContainer节点到这里(Canvas/GameLevelUI/enemyContainer)'
  37. })
  38. public enemyContainer: Node = null;
  39. // 金币预制体
  40. @property({ type: Prefab, tooltip: '金币预制体 CoinDrop' })
  41. public coinPrefab: Prefab = null;
  42. // === 生成 & 属性参数(保留需要可在内部自行设定,Inspector 不再显示) ===
  43. private spawnInterval: number = 3;
  44. // === 默认数值(当配置文件尚未加载时使用) ===
  45. private defaultEnemySpeed: number = 50;
  46. private defaultAttackPower: number = 10;
  47. private defaultHealth: number = 30;
  48. // 墙体属性
  49. // 最终数值将在 init() 中从存档或 GameManager 注入
  50. public wallHealth: number = 0;
  51. // 墙体血量显示节点(Inspector 拖拽 HeartLabel 节点到此)
  52. @property({ type: Node, tooltip: '墙体血量 Label 节点 (HeartLabel)' })
  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. @property({
  72. type: Node,
  73. tooltip: '敌人数量显示节点 (EnemyNumber)'
  74. })
  75. public enemyCountLabelNode: Node = null;
  76. @property({
  77. type: Node,
  78. tooltip: '波数显示Label (WaveNumber)'
  79. })
  80. public waveNumberLabelNode: Node = null;
  81. @property({
  82. type: Node,
  83. tooltip: '每波开始提示UI节点 (StartWaveUI)'
  84. })
  85. public startWaveUI: Node = null;
  86. private totalWaves: number = 1;
  87. private currentWave: number = 1;
  88. private currentWaveTotalEnemies: number = 0;
  89. private currentWaveEnemiesKilled: number = 0;
  90. /**
  91. * BaseSingleton 首次实例化回调
  92. */
  93. protected init() {
  94. // 获取配置管理器实例
  95. this.configManager = ConfigManager.getInstance();
  96. // 如果没有指定enemyContainer,尝试找到它
  97. if (!this.enemyContainer) {
  98. this.enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
  99. if (!this.enemyContainer) {
  100. console.warn('找不到enemyContainer节点,将尝试创建');
  101. }
  102. }
  103. // 获取游戏区域边界
  104. this.calculateGameBounds();
  105. // 查找墙体节点
  106. this.findWallNodes();
  107. // 从存档读取墙体基础血量,如果有的话
  108. const sdm = SaveDataManager.getInstance();
  109. const base = (sdm.getPlayerData() as any)?.wallBaseHealth;
  110. if (typeof base === 'number' && base > 0) this.wallHealth = base;
  111. // 初始化墙体血量显示
  112. this.initWallHealthDisplay();
  113. // 确保enemyContainer节点存在
  114. this.ensureEnemyContainer();
  115. // 查找GameManager
  116. this.findGameManager();
  117. // 如果没有指定enemyCountLabelNode,尝试找到它
  118. if (!this.enemyCountLabelNode) {
  119. this.enemyCountLabelNode = find('Canvas/GameLevelUI/EnemyNode/EnemyNumber');
  120. }
  121. if (!this.waveNumberLabelNode) {
  122. this.waveNumberLabelNode = find('Canvas/GameLevelUI/WaveInfo/WaveNumber');
  123. }
  124. if (!this.startWaveUI) {
  125. this.startWaveUI = find('Canvas/GameLevelUI/StartWaveUI') || find('Canvas/GameLevelUI/NextWaveUI');
  126. }
  127. // 初始化敌人数量显示
  128. this.updateEnemyCountLabel();
  129. }
  130. // 计算游戏区域边界
  131. private calculateGameBounds() {
  132. const gameArea = find('Canvas/GameLevelUI/GameArea');
  133. if (!gameArea) {
  134. return;
  135. }
  136. const uiTransform = gameArea.getComponent(UITransform);
  137. if (!uiTransform) {
  138. return;
  139. }
  140. const worldPos = gameArea.worldPosition;
  141. const width = uiTransform.width;
  142. const height = uiTransform.height;
  143. this.gameBounds = {
  144. left: worldPos.x - width / 2,
  145. right: worldPos.x + width / 2,
  146. top: worldPos.y + height / 2,
  147. bottom: worldPos.y - height / 2
  148. };
  149. }
  150. // 查找墙体节点
  151. private findWallNodes() {
  152. const gameArea = find('Canvas/GameLevelUI/GameArea');
  153. if (gameArea) {
  154. this.wallNodes = [];
  155. for (let i = 0; i < gameArea.children.length; i++) {
  156. const child = gameArea.children[i];
  157. if (child.name.includes('Wall') || child.name.includes('wall') || child.name.includes('墙')) {
  158. this.wallNodes.push(child);
  159. }
  160. }
  161. }
  162. }
  163. // 初始化墙体血量显示
  164. initWallHealthDisplay() {
  165. if (this.wallHealthNode) {
  166. this.updateWallHealthDisplay();
  167. } else {
  168. console.warn('EnemyController 未绑定 HeartLabel 节点,请在 Inspector 中拖拽 Canvas/GameLevelUI/HeartNode/HeartLabel');
  169. }
  170. }
  171. // 更新墙体血量显示
  172. public updateWallHealthDisplay() {
  173. if (!this.wallHealthNode) return;
  174. // 直接在当前节点或其子节点寻找 Label
  175. let heartLabel = this.wallHealthNode.getComponent(Label);
  176. if (!heartLabel) {
  177. heartLabel = this.wallHealthNode.getComponentInChildren(Label);
  178. }
  179. if (heartLabel) {
  180. heartLabel.string = this.wallHealth.toString();
  181. }
  182. }
  183. // 确保enemyContainer节点存在
  184. ensureEnemyContainer() {
  185. // 如果已经通过拖拽设置了节点,直接使用
  186. if (this.enemyContainer && this.enemyContainer.isValid) {
  187. return;
  188. }
  189. // 尝试查找节点
  190. this.enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
  191. if (this.enemyContainer) {
  192. console.log('找到已存在的enemyContainer节点');
  193. return;
  194. }
  195. // 如果找不到,创建新节点
  196. const gameLevelUI = find('Canvas/GameLevelUI');
  197. if (!gameLevelUI) {
  198. console.error('找不到GameLevelUI节点,无法创建enemyContainer');
  199. return;
  200. }
  201. this.enemyContainer = new Node('enemyContainer');
  202. gameLevelUI.addChild(this.enemyContainer);
  203. if (!this.enemyContainer.getComponent(UITransform)) {
  204. this.enemyContainer.addComponent(UITransform);
  205. }
  206. console.log('已在GameLevelUI下创建enemyContainer节点');
  207. }
  208. // 游戏开始
  209. startGame() {
  210. this.gameStarted = true;
  211. // 确保enemyContainer节点存在
  212. this.ensureEnemyContainer();
  213. // 开始生成敌人
  214. this.schedule(this.spawnEnemy, this.spawnInterval);
  215. console.log('开始生成敌人');
  216. }
  217. // 游戏结束
  218. stopGame() {
  219. this.gameStarted = false;
  220. // 停止生成敌人
  221. this.unschedule(this.spawnEnemy);
  222. // 清除所有敌人
  223. this.clearAllEnemies();
  224. console.log('停止生成敌人');
  225. }
  226. // 生成敌人
  227. spawnEnemy() {
  228. if (!this.gameStarted || !this.enemyPrefab) return;
  229. // 随机决定从上方还是下方生成
  230. const fromTop = Math.random() > 0.5;
  231. // 实例化敌人
  232. const enemy = instantiate(this.enemyPrefab);
  233. enemy.name = 'Enemy'; // 确保敌人节点名称为Enemy
  234. // 添加到场景中
  235. const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
  236. if (!enemyContainer) {
  237. return;
  238. }
  239. enemyContainer.addChild(enemy);
  240. // 生成在对应线(Line1 / Line2)上的随机位置
  241. const lineName = fromTop ? 'Line1' : 'Line2';
  242. const lineNode = enemyContainer.getChildByName(lineName);
  243. if (!lineNode) {
  244. console.warn(`[EnemyController] 未找到 ${lineName} 节点,取消本次敌人生成`);
  245. enemy.destroy();
  246. return;
  247. }
  248. // 在对应 line 上随机 X 坐标
  249. const spawnWorldX = this.gameBounds.left + Math.random() * (this.gameBounds.right - this.gameBounds.left);
  250. const spawnWorldY = lineNode.worldPosition.y;
  251. const worldPos = new Vec3(spawnWorldX, spawnWorldY, 0);
  252. const localPos = enemyContainer.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
  253. enemy.position = localPos;
  254. // === 根据配置设置敌人 ===
  255. const enemyComp = enemy.addComponent(EnemyInstance);
  256. let enemyConfig: EnemyConfig = null;
  257. if (this.configManager && this.configManager.isConfigLoaded()) {
  258. enemyConfig = this.configManager.getRandomEnemy();
  259. }
  260. if (enemyConfig) {
  261. // 添加EnemyComponent保存配置
  262. const cfgComp = enemy.addComponent(EnemyComponent);
  263. cfgComp.enemyConfig = enemyConfig;
  264. cfgComp.spawner = this;
  265. // 应用数值
  266. enemyComp.health = enemyConfig.stats.health;
  267. enemyComp.maxHealth = enemyConfig.stats.health;
  268. enemyComp.speed = enemyConfig.stats.speed;
  269. enemyComp.attackPower = enemyConfig.stats.damage;
  270. // 加载动画
  271. this.loadEnemyAnimation(enemy, enemyConfig);
  272. } else {
  273. // 使用默认值
  274. enemyComp.health = this.defaultHealth;
  275. enemyComp.maxHealth = this.defaultHealth;
  276. enemyComp.speed = this.defaultEnemySpeed;
  277. enemyComp.attackPower = this.defaultAttackPower;
  278. }
  279. // 额外的属性设置
  280. enemyComp.spawnFromTop = fromTop;
  281. enemyComp.targetFence = find(fromTop ? 'Canvas/GameLevelUI/GameArea/TopFence' : 'Canvas/GameLevelUI/GameArea/BottomFence');
  282. enemyComp.movingDirection = Math.random() > 0.5 ? 1 : -1;
  283. enemyComp.targetY = fromTop ? this.gameBounds.top - 50 : this.gameBounds.bottom + 50;
  284. enemyComp.changeDirectionTime = 0;
  285. enemyComp.controller = this;
  286. // 更新敌人血量显示
  287. enemyComp.updateHealthDisplay();
  288. // 添加到活跃敌人列表
  289. this.activeEnemies.push(enemy);
  290. // 更新敌人数量显示
  291. this.updateEnemyCountLabel();
  292. console.log(`生成敌人,当前共有 ${this.activeEnemies.length} 个敌人`);
  293. }
  294. // 清除所有敌人
  295. clearAllEnemies() {
  296. for (const enemy of this.activeEnemies) {
  297. if (enemy && enemy.isValid) {
  298. enemy.destroy();
  299. }
  300. }
  301. this.activeEnemies = [];
  302. // 更新敌人数量显示
  303. this.updateEnemyCountLabel();
  304. }
  305. // 获取所有活跃的敌人
  306. getActiveEnemies(): Node[] {
  307. // 过滤掉已经无效的敌人
  308. this.activeEnemies = this.activeEnemies.filter(enemy => enemy && enemy.isValid);
  309. return this.activeEnemies;
  310. }
  311. // 获取当前敌人数量
  312. getCurrentEnemyCount(): number {
  313. return this.getActiveEnemies().length;
  314. }
  315. // 获取游戏是否已开始状态
  316. public isGameStarted(): boolean {
  317. return this.gameStarted;
  318. }
  319. // 暂停生成敌人
  320. public pauseSpawning(): void {
  321. if (this.gameStarted) {
  322. this.unschedule(this.spawnEnemy);
  323. }
  324. }
  325. // 恢复生成敌人
  326. public resumeSpawning(): void {
  327. if (this.gameStarted) {
  328. this.schedule(this.spawnEnemy, this.spawnInterval);
  329. }
  330. }
  331. // 敌人受到伤害
  332. damageEnemy(enemy: Node, damage: number) {
  333. if (!enemy || !enemy.isValid) return;
  334. // 获取敌人组件
  335. const enemyComp = enemy.getComponent(EnemyInstance);
  336. if (!enemyComp) return;
  337. // 减少敌人血量
  338. enemyComp.takeDamage(damage);
  339. // 检查敌人是否死亡
  340. if (enemyComp.health <= 0) {
  341. // 从活跃敌人列表中移除
  342. const index = this.activeEnemies.indexOf(enemy);
  343. if (index !== -1) {
  344. this.activeEnemies.splice(index, 1);
  345. }
  346. // 更新敌人数量显示
  347. this.updateEnemyCountLabel();
  348. // 销毁敌人
  349. enemy.destroy();
  350. }
  351. }
  352. // 墙体受到伤害
  353. damageWall(damage: number) {
  354. // 减少墙体血量
  355. this.wallHealth -= damage;
  356. // 更新墙体血量显示
  357. this.updateWallHealthDisplay();
  358. // 检查墙体是否被摧毁
  359. if (this.wallHealth <= 0) {
  360. // 游戏结束
  361. this.gameOver();
  362. }
  363. }
  364. // 游戏结束
  365. gameOver() {
  366. // 停止游戏
  367. this.stopGame();
  368. // 通知GameManager游戏结束
  369. const gameManagerNode = find('Canvas/GameLevelUI/GameManager');
  370. if (gameManagerNode) {
  371. const gameManager = gameManagerNode.getComponent('GameManager') as any;
  372. if (gameManager) {
  373. gameManager.gameOver();
  374. }
  375. }
  376. }
  377. update(dt: number) {
  378. if (!this.gameStarted) return;
  379. // 更新所有敌人
  380. for (let i = this.activeEnemies.length - 1; i >= 0; i--) {
  381. const enemy = this.activeEnemies[i];
  382. if (!enemy || !enemy.isValid) {
  383. this.activeEnemies.splice(i, 1);
  384. continue;
  385. }
  386. // 敌人更新由各自的组件处理
  387. // 不再需要检查敌人是否到达墙体,因为敌人到达游戏区域后会自动攻击
  388. // 敌人的攻击逻辑已经在EnemyInstance中处理
  389. }
  390. }
  391. // === 调试方法 ===
  392. public testEnemyAttack() {
  393. console.log('=== 测试敌人攻击墙体 ===');
  394. // 手动触发墙体受到伤害
  395. const testDamage = 50;
  396. console.log(`模拟敌人攻击,伤害: ${testDamage}`);
  397. this.damageWall(testDamage);
  398. return this.wallHealth;
  399. }
  400. public getCurrentWallHealth(): number {
  401. return this.wallHealth;
  402. }
  403. public forceEnemyAttack() {
  404. console.log('=== 强制所有敌人进入攻击状态 ===');
  405. const activeEnemies = this.getActiveEnemies();
  406. console.log(`当前活跃敌人数量: ${activeEnemies.length}`);
  407. for (const enemy of activeEnemies) {
  408. const enemyComp = enemy.getComponent(EnemyInstance);
  409. if (enemyComp) {
  410. console.log(`强制敌人 ${enemy.name} 进入攻击状态`);
  411. // 直接调用damageWall方法进行测试
  412. this.damageWall(enemyComp.attackPower);
  413. }
  414. }
  415. }
  416. // === 查找GameManager ===
  417. private findGameManager() {
  418. const gameManagerNode = find('Canvas/GameLevelUI/GameManager');
  419. if (gameManagerNode) {
  420. this.gameManager = gameManagerNode.getComponent('GameManager');
  421. }
  422. }
  423. /** 供 EnemyInstance 在 onDestroy 中调用 */
  424. public notifyEnemyDead(enemyNode?: Node) {
  425. if (enemyNode) {
  426. const idx = this.activeEnemies.indexOf(enemyNode);
  427. if (idx !== -1) this.activeEnemies.splice(idx, 1);
  428. this.currentWaveEnemiesKilled++;
  429. this.updateEnemyCountLabel();
  430. }
  431. if (this.gameManager && this.gameManager.onEnemyKilled) {
  432. this.gameManager.onEnemyKilled();
  433. }
  434. }
  435. /**
  436. * 加载敌人骨骼动画
  437. */
  438. private loadEnemyAnimation(enemyNode: Node, enemyConfig: EnemyConfig) {
  439. let spinePath: string | undefined = enemyConfig.visualConfig?.spritePrefab;
  440. if (!spinePath) return;
  441. if (spinePath.startsWith('@EnemyAni')) {
  442. spinePath = spinePath.replace('@EnemyAni', 'Animation/EnemyAni');
  443. }
  444. if (spinePath.startsWith('@')) {
  445. spinePath = spinePath.substring(1);
  446. }
  447. resources.load(spinePath, sp.SkeletonData, (err, skeletonData) => {
  448. if (err) {
  449. console.warn(`加载敌人Spine动画失败: ${spinePath}`, err);
  450. return;
  451. }
  452. let skeleton = enemyNode.getComponent(sp.Skeleton);
  453. if (!skeleton) {
  454. skeleton = enemyNode.addComponent(sp.Skeleton);
  455. }
  456. skeleton.skeletonData = skeletonData;
  457. const anims = enemyConfig.visualConfig.animations;
  458. const walkName = anims?.walk ?? 'walk';
  459. const idleName = anims?.idle ?? 'idle';
  460. if (skeleton.findAnimation(walkName)) {
  461. skeleton.setAnimation(0, walkName, true);
  462. } else if (skeleton.findAnimation(idleName)) {
  463. skeleton.setAnimation(0, idleName, true);
  464. }
  465. });
  466. }
  467. // 更新敌人数量显示
  468. private updateEnemyCountLabel() {
  469. if (!this.enemyCountLabelNode) return;
  470. const label = this.enemyCountLabelNode.getComponent(Label);
  471. if (label) {
  472. const remaining = Math.max(0, this.currentWaveTotalEnemies - this.currentWaveEnemiesKilled);
  473. label.string = remaining.toString();
  474. console.log(`EnemyController 剩余敌人数量: ${remaining}`);
  475. }
  476. }
  477. public startWave(waveNum: number, totalWaves: number, totalEnemies: number) {
  478. this.currentWave = waveNum;
  479. this.totalWaves = totalWaves;
  480. this.currentWaveTotalEnemies = totalEnemies;
  481. this.currentWaveEnemiesKilled = 0;
  482. this.updateWaveLabel();
  483. this.updateEnemyCountLabel();
  484. if (this.startWaveUI) this.startWaveUI.active = false;
  485. }
  486. private updateWaveLabel() {
  487. if (!this.waveNumberLabelNode) return;
  488. const label = this.waveNumberLabelNode.getComponent(Label);
  489. if (label) {
  490. label.string = `${this.currentWave}/${this.totalWaves}`;
  491. }
  492. }
  493. /** 显示每波开始提示,随后开启敌人生成 */
  494. public showStartWavePromptUI(duration: number = 2) {
  495. if (!this.startWaveUI) return;
  496. this.startWaveUI.active = true;
  497. // 暂停生成(确保未重复)
  498. this.pauseSpawning();
  499. if (duration > 0) {
  500. this.scheduleOnce(() => {
  501. if (this.startWaveUI) this.startWaveUI.active = false;
  502. // 真正开始/恢复生成敌人
  503. this.startGame();
  504. }, duration);
  505. } else {
  506. this.startGame();
  507. }
  508. }
  509. }