EnemyController.ts 23 KB

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