EnemyController.ts 13 KB

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