EnemyController.ts 11 KB

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