EnemyController.ts 13 KB

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