EnemyInstance.ts 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. import { _decorator, Component, Node, ProgressBar, Label, Vec3, find, UITransform, Collider2D, Contact2DType, IPhysics2DContact, instantiate, resources, Prefab, JsonAsset, RigidBody2D, ERigidBody2DType, BoxCollider2D, CircleCollider2D, Vec2, Color, UIOpacity, tween } from 'cc';
  2. import { JsonConfigLoader } from '../Core/JsonConfigLoader';
  3. import { sp } from 'cc';
  4. import { DamageNumberAni } from '../Animations/DamageNumberAni';
  5. import { HPBarAnimation } from '../Animations/HPBarAnimation';
  6. import { EnemyComponent } from './EnemyComponent';
  7. import { EnemyAudio } from '../AudioManager/EnemyAudios';
  8. import { ScreenShakeManager } from './EnemyWeapon/ScreenShakeManager';
  9. import { EnemyAttackStateManager } from './EnemyAttackStateManager';
  10. import EventBus, { GameEvents } from '../Core/EventBus';
  11. const { ccclass, property } = _decorator;
  12. // 前向声明EnemyController接口,避免循环引用
  13. interface EnemyControllerType {
  14. gameBounds: {
  15. left: number;
  16. right: number;
  17. top: number;
  18. bottom: number;
  19. };
  20. topFenceNode: Node;
  21. bottomFenceNode: Node;
  22. damageWall: (damage: number) => void;
  23. getComponent: (componentType: any) => any;
  24. }
  25. // 敌人状态枚举
  26. enum EnemyState {
  27. DRIFTING, // 漂移中(从生成位置移动到线上)
  28. MOVING, // 移动中
  29. IDLE, // 待机中(碰到墙体后停止移动但可以攻击)
  30. ATTACKING, // 攻击中
  31. STEALTH, // 隐身中(不被方块武器检测到)
  32. DEAD // 死亡
  33. }
  34. // 单个敌人实例的组件
  35. @ccclass('EnemyInstance')
  36. export class EnemyInstance extends Component {
  37. // 敌人属性(从配置文件读取)
  38. public health: number = 0;
  39. public maxHealth: number = 0;
  40. public speed: number = 0;
  41. public attackPower: number = 0;
  42. // 敌人配置ID
  43. public enemyId: string = '';
  44. // 敌人配置数据
  45. private enemyConfig: any = null;
  46. // 敌人配置数据库
  47. private static enemyDatabase: any = null;
  48. // === 新增属性 ===
  49. /** 是否从上方生成 */
  50. public spawnFromTop: boolean = true;
  51. /** 目标 Fence 节点(TopFence / BottomFence) */
  52. public targetFence: Node | null = null;
  53. /** 目标位置(墙体上的随机点) */
  54. public targetPosition: Vec3 | null = null;
  55. /** 漂移目标位置(线上的位置) */
  56. public driftTargetPosition: Vec3 | null = null;
  57. // 移动相关属性
  58. public movingDirection: number = 1; // 1: 向右, -1: 向左
  59. public targetY: number = 0; // 目标Y位置
  60. public changeDirectionTime: number = 0; // 下次改变方向的时间
  61. // 移动配置属性(从配置文件读取)
  62. public movementPattern: string = 'direct'; // 移动模式:direct, patrol等
  63. public moveType: string = 'straight'; // 移动类型:straight, sway等
  64. public patrolRange: number = 100; // 巡逻范围
  65. public rotationSpeed: number = 90; // 旋转速度
  66. public speedVariation: number = 0; // 速度变化
  67. public swingAmplitude: number = 0; // 摆动幅度
  68. public swingFrequency: number = 1; // 摆动频率
  69. // 摆动移动相关属性
  70. private swayTime: number = 0; // 摆动时间累计
  71. private basePosition: Vec3 = new Vec3(); // 基础位置(摆动的中心点)
  72. private swayOffset: Vec3 = new Vec3(); // 当前摆动偏移
  73. // 攻击属性
  74. public attackInterval: number = 0; // 攻击间隔(秒),从配置文件读取
  75. public attackRange: number = 0; // 攻击范围,从配置文件读取
  76. private attackTimer: number = 0;
  77. // 对控制器的引用
  78. public controller: EnemyControllerType = null;
  79. // 敌人当前状态
  80. private state: EnemyState = EnemyState.DRIFTING;
  81. // 游戏区域中心
  82. private gameAreaCenter: Vec3 = new Vec3();
  83. // 碰撞的墙体
  84. private collidedWall: Node = null;
  85. // 骨骼动画组件
  86. private skeleton: sp.Skeleton | null = null;
  87. // 血条动画组件
  88. private hpBarAnimation: HPBarAnimation | null = null;
  89. // 暂停状态标记
  90. private isPaused: boolean = false;
  91. // 当前播放的动画名称,用于避免重复播放
  92. private currentAnimationName: string = '';
  93. // 攻击状态下的动画管理
  94. private isPlayingAttackAnimation: boolean = false;
  95. // 隐身相关属性
  96. private stealthDuration: number = 0; // 隐身持续时间
  97. private stealthTimer: number = 0; // 隐身计时器
  98. private originalOpacity: number = 255; // 原始透明度
  99. // 隐身循环相关
  100. private hasStealthAbility: boolean = false;
  101. private stealthCooldown: number = 5.0;
  102. private initialStealthScheduled: boolean = false; // 首次隐身定时是否已安排
  103. private stealthActive: boolean = false; // 隐身为叠加标记,不改变行为状态
  104. start() {
  105. // 初始化敌人
  106. this.initializeEnemy();
  107. }
  108. // 静态方法:加载敌人配置数据库
  109. public static async loadEnemyDatabase(): Promise<void> {
  110. if (EnemyInstance.enemyDatabase) return;
  111. try {
  112. EnemyInstance.enemyDatabase = await JsonConfigLoader.getInstance().loadConfig('enemies');
  113. if (!EnemyInstance.enemyDatabase) {
  114. throw new Error('敌人配置文件内容为空');
  115. }
  116. } catch (error) {
  117. console.error('[EnemyInstance] 加载敌人配置失败:', error);
  118. throw error;
  119. }
  120. }
  121. // 设置敌人配置
  122. public setEnemyConfig(enemyId: string): void {
  123. this.enemyId = enemyId;
  124. if (!EnemyInstance.enemyDatabase) {
  125. console.error('[EnemyInstance] 敌人配置数据库未加载');
  126. return;
  127. }
  128. // 从数据库中查找敌人配置
  129. // 修复:enemies.json是直接的数组结构,不需要.enemies包装
  130. const enemies = EnemyInstance.enemyDatabase;
  131. this.enemyConfig = enemies.find((enemy: any) => enemy.id === enemyId);
  132. if (!this.enemyConfig) {
  133. console.error(`[EnemyInstance] 未找到敌人配置: ${enemyId}`);
  134. return;
  135. }
  136. // 应用配置到敌人属性
  137. this.applyEnemyConfig();
  138. // 应用配置后,重新初始化特殊能力(确保在运行时添加组件后也能按配置触发)
  139. this.initializeSpecialAbilities();
  140. }
  141. // 应用敌人配置到属性
  142. private applyEnemyConfig(): void {
  143. if (!this.enemyConfig) return;
  144. // 从stats节点读取基础属性
  145. const stats = this.enemyConfig.stats || {};
  146. this.health = stats.health || 30;
  147. this.maxHealth = stats.maxHealth || this.health;
  148. // 从movement节点读取移动配置
  149. const movement = this.enemyConfig.movement || {};
  150. this.speed = movement.speed || 50;
  151. // 读取移动模式配置
  152. this.movementPattern = movement.pattern || 'direct';
  153. this.moveType = movement.moveType || 'straight';
  154. this.patrolRange = movement.patrolRange || 100;
  155. this.rotationSpeed = movement.rotationSpeed || 90;
  156. this.speedVariation = movement.speedVariation || 0;
  157. // 读取摆动配置
  158. this.swingAmplitude = movement.swingAmplitude || 0;
  159. this.swingFrequency = movement.swingFrequency || 1;
  160. // 从combat节点读取攻击力
  161. const combat = this.enemyConfig.combat || {};
  162. this.attackPower = combat.attackDamage || 10;
  163. // 设置攻击间隔
  164. this.attackInterval = combat.attackCooldown || 2.0;
  165. console.log(`[EnemyInstance] 应用配置 ${this.enemyId}: 移动模式=${this.movementPattern}, 移动类型=${this.moveType}, 速度=${this.speed}, 摆动幅度=${this.swingAmplitude}`);
  166. }
  167. // 获取敌人配置信息
  168. public getEnemyConfig(): any {
  169. return this.enemyConfig;
  170. }
  171. // 获取敌人名称
  172. public getEnemyName(): string {
  173. return this.enemyConfig?.name || '未知敌人';
  174. }
  175. // 获取敌人类型
  176. public getEnemyType(): string {
  177. return this.enemyConfig?.type || 'basic';
  178. }
  179. // 获取敌人稀有度
  180. public getEnemyRarity(): string {
  181. return this.enemyConfig?.rarity || 'common';
  182. }
  183. // 获取金币奖励
  184. public getGoldReward(): number {
  185. return this.enemyConfig?.goldReward || 1;
  186. }
  187. // 获取掉落金币数量
  188. public getDropCoins(): number {
  189. const stats = this.enemyConfig?.stats || {};
  190. return stats.dropCoins || 1;
  191. }
  192. // 初始化敌人
  193. private initializeEnemy() {
  194. // 确保血量正确设置
  195. if (this.maxHealth > 0) {
  196. this.health = this.maxHealth;
  197. }
  198. this.state = EnemyState.DRIFTING; // 从漂移状态开始
  199. // 只有在攻击间隔未设置时才使用默认值
  200. if (this.attackInterval <= 0) {
  201. this.attackInterval = 2.0; // 默认攻击间隔
  202. }
  203. this.attackTimer = 0;
  204. // 初始化血条动画组件
  205. this.initializeHPBarAnimation();
  206. // 初始化骨骼动画组件 - 从EnemySprite子节点获取
  207. const enemySprite = this.node.getChildByName('EnemySprite');
  208. if (enemySprite) {
  209. this.skeleton = enemySprite.getComponent(sp.Skeleton);
  210. } else {
  211. console.error('[EnemyInstance] 未找到EnemySprite子节点,无法获取骨骼动画组件');
  212. }
  213. this.playWalkAnimation();
  214. // 计算游戏区域中心
  215. this.calculateGameAreaCenter();
  216. // 初始化碰撞检测
  217. this.setupCollider();
  218. // 注册到攻击状态管理器
  219. const attackStateManager = EnemyAttackStateManager.getInstance();
  220. attackStateManager.registerEnemy(this.node, true); // 初始状态为可攻击
  221. // 初始化特殊能力
  222. console.log(`[EnemyInstance] 准备初始化特殊能力 - 敌人节点: ${this.node.name}`);
  223. this.initializeSpecialAbilities();
  224. }
  225. // 初始化特殊能力
  226. private initializeSpecialAbilities(): void {
  227. console.log(`[EnemyInstance] 初始化特殊能力 - 敌人: ${this.getEnemyName()}, 配置存在: ${!!this.enemyConfig}, 特殊能力: ${this.enemyConfig?.special_abilities ? JSON.stringify(this.enemyConfig.special_abilities) : '无'}`);
  228. if (!this.enemyConfig || !this.enemyConfig.special_abilities) {
  229. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 没有特殊能力配置`);
  230. return;
  231. }
  232. // 检查是否有隐身能力
  233. const stealthAbility = this.enemyConfig.special_abilities.find(ability => ability.type === 'stealth');
  234. if (stealthAbility) {
  235. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 具有隐身能力,冷却时间: ${stealthAbility.cooldown}秒`);
  236. this.hasStealthAbility = true;
  237. this.stealthCooldown = typeof stealthAbility.cooldown === 'number' ? stealthAbility.cooldown : 5.0;
  238. // 生成后按冷却时间开始循环隐身:首次隐身在 cooldown 秒后触发
  239. if (!this.initialStealthScheduled) {
  240. this.initialStealthScheduled = true;
  241. this.scheduleOnce(() => {
  242. if (!this.node || !this.node.isValid) return;
  243. if (this.state === EnemyState.DEAD) return;
  244. this.enterStealth(this.stealthCooldown);
  245. }, this.stealthCooldown);
  246. }
  247. }
  248. }
  249. // 设置碰撞器
  250. setupCollider() {
  251. // 从EnemySprite子节点获取碰撞器组件
  252. const enemySprite = this.node.getChildByName('EnemySprite');
  253. if (!enemySprite) {
  254. console.error('[EnemyInstance] 未找到EnemySprite子节点,无法设置碰撞器组件');
  255. return;
  256. }
  257. // 检查EnemySprite节点是否有碰撞器
  258. let collider = enemySprite.getComponent(Collider2D);
  259. if (!collider) {
  260. console.warn(`[EnemyInstance] 敌人节点 ${this.node.name} 的EnemySprite子节点没有碰撞器组件`);
  261. return;
  262. }
  263. // 确保有RigidBody2D组件,这对于碰撞检测是必需的
  264. let rigidBody = enemySprite.getComponent(RigidBody2D);
  265. if (!rigidBody) {
  266. console.log(`[EnemyInstance] 为敌人EnemySprite节点添加RigidBody2D组件`);
  267. rigidBody = enemySprite.addComponent(RigidBody2D);
  268. }
  269. // 设置刚体属性
  270. if (rigidBody) {
  271. rigidBody.type = ERigidBody2DType.Dynamic; // 动态刚体
  272. rigidBody.enabledContactListener = true; // 启用碰撞监听
  273. rigidBody.gravityScale = 0; // 不受重力影响
  274. rigidBody.linearDamping = 0; // 无线性阻尼
  275. rigidBody.angularDamping = 0; // 无角阻尼
  276. rigidBody.allowSleep = false; // 不允许休眠
  277. rigidBody.fixedRotation = true; // 固定旋转
  278. }
  279. // 根据ContentSize自适应碰撞体大小
  280. this.adaptColliderToContentSize(collider);
  281. // 设置碰撞事件监听
  282. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  283. console.log(`[EnemyInstance] 敌人 ${this.node.name} 碰撞器设置完成,碰撞器启用: ${collider.enabled}, 刚体启用: ${rigidBody?.enabled}`);
  284. }
  285. /**
  286. * 根据ContentSize自适应碰撞体大小
  287. */
  288. private adaptColliderToContentSize(collider: Collider2D): void {
  289. // 从EnemySprite子节点获取UITransform组件
  290. const enemySprite = this.node.getChildByName('EnemySprite');
  291. if (!enemySprite) {
  292. console.error('[EnemyInstance] 未找到EnemySprite子节点,无法获取UITransform组件');
  293. return;
  294. }
  295. const uiTransform = enemySprite.getComponent(UITransform);
  296. if (!uiTransform) {
  297. console.warn(`[EnemyInstance] EnemySprite节点没有UITransform组件,无法自适应碰撞体大小`);
  298. return;
  299. }
  300. // 尝试获取骨骼动画组件来获取更准确的尺寸
  301. const skeleton = enemySprite.getComponent(sp.Skeleton);
  302. let actualWidth = uiTransform.contentSize.width;
  303. let actualHeight = uiTransform.contentSize.height;
  304. if (skeleton && skeleton.skeletonData) {
  305. // 如果有骨骼动画,尝试从骨骼数据获取尺寸信息
  306. try {
  307. // 使用骨骼数据的默认尺寸,或者根据动画缩放计算
  308. const skeletonData = skeleton.skeletonData;
  309. if (skeletonData) {
  310. // 获取骨骼的缩放比例
  311. const scaleX = Math.abs(skeleton.node.scale.x);
  312. const scaleY = Math.abs(skeleton.node.scale.y);
  313. // 使用合理的默认尺寸并应用缩放
  314. actualWidth = Math.max(60 * scaleX, uiTransform.contentSize.width * 0.8);
  315. actualHeight = Math.max(80 * scaleY, uiTransform.contentSize.height * 0.8);
  316. console.log(`[EnemyInstance] 使用骨骼动画尺寸 (scale: ${scaleX.toFixed(2)}, ${scaleY.toFixed(2)}): ${actualWidth.toFixed(2)} x ${actualHeight.toFixed(2)}`);
  317. } else {
  318. // 如果无法获取骨骼数据,使用默认尺寸
  319. actualWidth = Math.max(60, uiTransform.contentSize.width * 0.8);
  320. actualHeight = Math.max(80, uiTransform.contentSize.height * 0.8);
  321. console.log(`[EnemyInstance] 使用默认骨骼尺寸: ${actualWidth.toFixed(2)} x ${actualHeight.toFixed(2)}`);
  322. }
  323. } catch (error) {
  324. console.warn(`[EnemyInstance] 获取骨骼信息失败,使用默认尺寸:`, error);
  325. actualWidth = Math.max(60, uiTransform.contentSize.width * 0.8);
  326. actualHeight = Math.max(80, uiTransform.contentSize.height * 0.8);
  327. }
  328. }
  329. console.log(`[EnemyInstance] 敌人 ${this.node.name} 实际尺寸: ${actualWidth.toFixed(2)} x ${actualHeight.toFixed(2)}`);
  330. // 根据碰撞器类型设置大小
  331. if (collider instanceof BoxCollider2D) {
  332. // 方形碰撞器:使用实际尺寸,稍微缩小以避免过于敏感的碰撞
  333. const boxCollider = collider as BoxCollider2D;
  334. boxCollider.size.width = actualWidth * 0.9; // 缩小10%
  335. boxCollider.size.height = actualHeight * 0.9; // 缩小10%
  336. // 调整偏移量,使碰撞器居中对齐到EnemySprite
  337. const anchorPoint = uiTransform.anchorPoint;
  338. // 对于骨骼动画,通常锚点在底部中心(0.5, 0),需要向上偏移
  339. boxCollider.offset.x = 0; // X轴居中
  340. boxCollider.offset.y = actualHeight * (0.5 - anchorPoint.y) * 0.9; // Y轴根据锚点调整,并匹配缩放后的尺寸
  341. console.log(`[EnemyInstance] BoxCollider2D 已自适应: size(${boxCollider.size.width.toFixed(2)}, ${boxCollider.size.height.toFixed(2)}), offset(${boxCollider.offset.x.toFixed(2)}, ${boxCollider.offset.y.toFixed(2)}), anchor(${anchorPoint.x.toFixed(2)}, ${anchorPoint.y.toFixed(2)})`);
  342. } else if (collider instanceof CircleCollider2D) {
  343. // 圆形碰撞器:使用实际尺寸的较小值作为直径,稍微缩小
  344. const circleCollider = collider as CircleCollider2D;
  345. const radius = Math.min(actualWidth, actualHeight) / 2 * 0.9; // 缩小10%
  346. circleCollider.radius = radius;
  347. // 调整偏移量,使碰撞器居中对齐到EnemySprite
  348. const anchorPoint = uiTransform.anchorPoint;
  349. // 对于骨骼动画,通常锚点在底部中心(0.5, 0),需要向上偏移
  350. circleCollider.offset.x = 0; // X轴居中
  351. circleCollider.offset.y = actualHeight * (0.5 - anchorPoint.y) * 0.9; // Y轴根据锚点调整,并匹配缩放后的尺寸
  352. console.log(`[EnemyInstance] CircleCollider2D 已自适应: radius(${radius.toFixed(2)}), offset(${circleCollider.offset.x.toFixed(2)}, ${circleCollider.offset.y.toFixed(2)}), anchor(${anchorPoint.x.toFixed(2)}, ${anchorPoint.y.toFixed(2)})`);
  353. } else {
  354. console.warn(`[EnemyInstance] 不支持的碰撞器类型: ${collider.constructor.name}`);
  355. }
  356. // 碰撞体调整完成后,同步更新血条位置
  357. this.updateHPBarPosition();
  358. }
  359. /**
  360. * 动态调整血条位置,使其始终显示在碰撞体上方
  361. */
  362. private updateHPBarPosition(): void {
  363. // 获取血条根节点
  364. const hpBarRoot = this.node.getChildByName('HPBar');
  365. if (!hpBarRoot) {
  366. console.warn('[EnemyInstance] 未找到HPBar节点,无法调整血条位置');
  367. return;
  368. }
  369. // 获取EnemySprite节点和其碰撞器
  370. const enemySprite = this.node.getChildByName('EnemySprite');
  371. if (!enemySprite) {
  372. console.error('[EnemyInstance] 未找到EnemySprite子节点,无法计算血条位置');
  373. return;
  374. }
  375. const collider = enemySprite.getComponent(Collider2D);
  376. const uiTransform = enemySprite.getComponent(UITransform);
  377. if (!collider || !uiTransform) {
  378. console.warn('[EnemyInstance] EnemySprite节点缺少必要组件,无法计算血条位置');
  379. return;
  380. }
  381. // 计算碰撞体的顶部位置
  382. let colliderTopY = 0;
  383. const anchorPoint = uiTransform.anchorPoint;
  384. if (collider instanceof BoxCollider2D) {
  385. const boxCollider = collider as BoxCollider2D;
  386. // 碰撞体顶部 = 碰撞体偏移Y + 碰撞体高度的一半
  387. colliderTopY = boxCollider.offset.y + boxCollider.size.height / 2;
  388. } else if (collider instanceof CircleCollider2D) {
  389. const circleCollider = collider as CircleCollider2D;
  390. // 碰撞体顶部 = 碰撞体偏移Y + 半径
  391. colliderTopY = circleCollider.offset.y + circleCollider.radius;
  392. }
  393. // 血条位置设置为碰撞体顶部上方一定距离
  394. const hpBarOffset = -35; // 血条距离碰撞体顶部的距离
  395. const hpBarY = colliderTopY + hpBarOffset;
  396. // 设置血条位置
  397. const hpBarTransform = hpBarRoot.getComponent(UITransform);
  398. if (hpBarTransform) {
  399. hpBarRoot.setPosition(0, hpBarY, 0);
  400. console.log(`[EnemyInstance] 血条位置已更新: Y = ${hpBarY.toFixed(2)} (碰撞体顶部: ${colliderTopY.toFixed(2)})`);
  401. }
  402. }
  403. // 碰撞开始事件
  404. onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  405. const nodeName = otherCollider.node.name;
  406. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 碰撞检测 - 碰撞对象: ${nodeName}, 当前状态: ${EnemyState[this.state]}`);
  407. // 只有在移动状态或攻击状态下才能响应墙体碰撞,避免重复触发
  408. if (this.state !== EnemyState.MOVING && this.state !== EnemyState.ATTACKING) {
  409. return;
  410. }
  411. // 检查是否碰到墙体(更严格的墙体识别)
  412. const isWall = this.isWallNode(otherCollider.node);
  413. if (isWall) {
  414. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 碰撞到墙体 ${nodeName},切换到待机状态`);
  415. this.state = EnemyState.IDLE;
  416. this.attackTimer = 0; // 立即开始攻击
  417. this.collidedWall = otherCollider.node; // 记录碰撞的墙体
  418. // 停止移动
  419. this.stopRigidBodyMovement();
  420. // 切换到待机动画
  421. this.playIdleAnimation();
  422. }
  423. }
  424. // 检查节点是否为墙体
  425. private isWallNode(node: Node): boolean {
  426. const nodeName = node.name.toLowerCase();
  427. // 检查节点名称是否包含墙体关键词
  428. const wallKeywords = ['wall', 'fence', 'jiguang', '墙', '围栏'];
  429. const isWallByName = wallKeywords.some(keyword => nodeName.includes(keyword));
  430. // 检查节点是否有Wall组件
  431. const hasWallComponent = node.getComponent('Wall') !== null;
  432. return isWallByName || hasWallComponent;
  433. }
  434. // 获取节点路径
  435. getNodePath(node: Node): string {
  436. let path = node.name;
  437. let current = node;
  438. while (current.parent) {
  439. current = current.parent;
  440. path = current.name + '/' + path;
  441. }
  442. return path;
  443. }
  444. // 计算游戏区域中心
  445. private calculateGameAreaCenter() {
  446. const gameArea = find('Canvas/GameLevelUI/GameArea');
  447. if (gameArea) {
  448. this.gameAreaCenter = gameArea.worldPosition;
  449. }
  450. }
  451. /**
  452. * 初始化血条动画组件
  453. */
  454. private initializeHPBarAnimation() {
  455. // 检查是否已经存在血条动画组件,避免重复添加
  456. if (this.hpBarAnimation) {
  457. console.log(`[EnemyInstance] 血条动画组件已存在,跳过初始化`);
  458. return;
  459. }
  460. const hpBar = this.node.getChildByName('HPBar');
  461. if (hpBar) {
  462. // 查找红色和黄色血条节点
  463. const redBarNode = hpBar.getChildByName('RedBar');
  464. const yellowBarNode = hpBar.getChildByName('YellowBar');
  465. if (redBarNode && yellowBarNode) {
  466. // 检查节点是否已经有HPBarAnimation组件
  467. let existingHPBarAnimation = this.node.getComponent(HPBarAnimation);
  468. if (existingHPBarAnimation) {
  469. this.hpBarAnimation = existingHPBarAnimation;
  470. console.log(`[EnemyInstance] 使用已存在的血条动画组件`);
  471. } else {
  472. // 添加血条动画组件
  473. this.hpBarAnimation = this.node.addComponent(HPBarAnimation);
  474. console.log(`[EnemyInstance] 新建血条动画组件`);
  475. }
  476. if (this.hpBarAnimation) {
  477. // 正确设置红色和黄色血条节点引用
  478. this.hpBarAnimation.redBarNode = redBarNode;
  479. this.hpBarAnimation.yellowBarNode = yellowBarNode;
  480. this.hpBarAnimation.hpBarRootNode = hpBar; // 设置HPBar根节点
  481. console.log(`[EnemyInstance] 血条动画组件已初始化`);
  482. }
  483. } else {
  484. console.warn(`[EnemyInstance] HPBar下未找到RedBar或YellowBar节点,RedBar: ${!!redBarNode}, YellowBar: ${!!yellowBarNode}`);
  485. }
  486. } else {
  487. console.warn(`[EnemyInstance] 未找到HPBar节点,无法初始化血条动画`);
  488. }
  489. }
  490. // 更新血量显示
  491. updateHealthDisplay(showBar: boolean = false) {
  492. // 确保血量值在有效范围内
  493. this.health = Math.max(0, Math.min(this.maxHealth, this.health));
  494. const healthProgress = this.maxHealth > 0 ? this.health / this.maxHealth : 0;
  495. console.log(`[EnemyInstance] 更新血量显示: ${this.health}/${this.maxHealth} (${(healthProgress * 100).toFixed(1)}%)`);
  496. // 使用血条动画组件更新血条
  497. if (this.hpBarAnimation) {
  498. this.hpBarAnimation.updateProgress(healthProgress, showBar);
  499. } else {
  500. // 备用方案:直接更新血条
  501. const hpBar = this.node.getChildByName('HPBar');
  502. if (hpBar) {
  503. const progressBar = hpBar.getComponent(ProgressBar);
  504. if (progressBar) {
  505. progressBar.progress = healthProgress;
  506. }
  507. // 根据showBar参数控制血条显示
  508. hpBar.active = showBar;
  509. }
  510. }
  511. // 更新血量数字
  512. const hpLabel = this.node.getChildByName('HPLabel');
  513. if (hpLabel) {
  514. const label = hpLabel.getComponent(Label);
  515. if (label) {
  516. // 显示整数血量值
  517. label.string = Math.ceil(this.health).toString();
  518. }
  519. }
  520. }
  521. // 受到伤害
  522. takeDamage(damage: number, isCritical: boolean = false) {
  523. // 如果已经死亡,不再处理伤害
  524. if (this.state === EnemyState.DEAD) {
  525. return;
  526. }
  527. // 确保伤害值为正数
  528. if (damage <= 0) {
  529. console.warn(`[EnemyInstance] 无效的伤害值: ${damage}`);
  530. return;
  531. }
  532. // 应用防御力减少伤害
  533. const enemyComponent = this.getComponent(EnemyComponent);
  534. const defense = enemyComponent ? enemyComponent.getDefense() : 0;
  535. let finalDamage = Math.max(1, damage - defense); // 至少造成1点伤害
  536. // 检查格挡
  537. if (enemyComponent && enemyComponent.canBlock() && this.tryBlock()) {
  538. finalDamage *= (1 - enemyComponent.getBlockDamageReduction());
  539. this.showBlockEffect();
  540. }
  541. // 计算新的血量,确保不会低于0
  542. const oldHealth = this.health;
  543. const newHealth = Math.max(0, this.health - finalDamage);
  544. const actualHealthLoss = oldHealth - newHealth; // 实际血量损失
  545. this.health = newHealth;
  546. // 检查是否触发狂暴
  547. this.checkRageTrigger();
  548. // 日志显示武器的真实伤害值,而不是血量差值
  549. console.log(`[EnemyInstance] 敌人受到伤害: ${damage} (武器伤害), 防御后伤害: ${finalDamage}, 实际血量损失: ${actualHealthLoss}, 剩余血量: ${this.health}/${this.maxHealth}`);
  550. // 受击音效已移除
  551. // 显示伤害数字动画(在敌人头顶)- 显示防御后的实际伤害
  552. // 优先使用EnemyController节点上的DamageNumberAni组件实例
  553. if (this.controller) {
  554. const damageAni = this.controller.getComponent(DamageNumberAni);
  555. if (damageAni) {
  556. damageAni.showDamageNumber(finalDamage, this.node.worldPosition, isCritical);
  557. } else {
  558. // 如果没有找到组件实例,使用静态方法作为备用
  559. DamageNumberAni.showDamageNumber(finalDamage, this.node.worldPosition, isCritical);
  560. }
  561. } else {
  562. // 如果没有controller引用,使用静态方法
  563. DamageNumberAni.showDamageNumber(finalDamage, this.node.worldPosition, isCritical);
  564. }
  565. // 更新血量显示和动画,受伤时显示血条
  566. this.updateHealthDisplay(true);
  567. // 如果血量低于等于0,销毁敌人
  568. if (this.health <= 0) {
  569. console.log(`[EnemyInstance] 敌人死亡,开始销毁流程`);
  570. this.state = EnemyState.DEAD;
  571. this.spawnCoin();
  572. // 进入死亡流程,禁用碰撞避免重复命中
  573. const enemySprite = this.node.getChildByName('EnemySprite');
  574. if (enemySprite) {
  575. const col = enemySprite.getComponent(Collider2D);
  576. if (col) col.enabled = false;
  577. }
  578. this.playDeathAnimationAndDestroy();
  579. }
  580. }
  581. onDestroy() {
  582. console.log(`[EnemyInstance] onDestroy 被调用,准备通知控制器`);
  583. // 取消所有调度,避免隐身循环在销毁后继续
  584. this.unscheduleAllCallbacks();
  585. // 从攻击状态管理器中移除敌人
  586. const attackStateManager = EnemyAttackStateManager.getInstance();
  587. attackStateManager.unregisterEnemy(this.node);
  588. // 通知控制器 & GameManager
  589. if (this.controller && typeof (this.controller as any).notifyEnemyDead === 'function') {
  590. // 检查控制器是否处于清理状态,避免在清理过程中触发游戏事件
  591. const isClearing = (this.controller as any).isClearing;
  592. if (isClearing) {
  593. console.log(`[EnemyInstance] 控制器处于清理状态,跳过死亡通知`);
  594. return;
  595. }
  596. console.log(`[EnemyInstance] 调用 notifyEnemyDead`);
  597. (this.controller as any).notifyEnemyDead(this.node);
  598. } else {
  599. console.warn(`[EnemyInstance] 无法调用 notifyEnemyDead: controller=${!!this.controller}`);
  600. }
  601. }
  602. update(deltaTime: number) {
  603. // 如果敌人被暂停,则不执行任何更新逻辑
  604. if (this.isPaused) {
  605. return;
  606. }
  607. if (this.state === EnemyState.DRIFTING) {
  608. this.updateDrifting(deltaTime);
  609. // 在漂移状态也检查墙体碰撞
  610. this.checkWallCollisionByDistance();
  611. } else if (this.state === EnemyState.MOVING) {
  612. this.updateMovement(deltaTime);
  613. // 在移动状态检查墙体碰撞
  614. this.checkWallCollisionByDistance();
  615. } else if (this.state === EnemyState.IDLE) {
  616. this.updateIdle(deltaTime);
  617. } else if (this.state === EnemyState.ATTACKING) {
  618. this.updateAttack(deltaTime);
  619. }
  620. // 隐身为叠加效果:不改变行为状态,但仍需要计时与退出逻辑
  621. if (this.stealthActive) {
  622. this.updateStealth(deltaTime);
  623. }
  624. // 不再每帧播放攻击动画,避免日志刷屏
  625. }
  626. // 更新漂移逻辑(从生成位置移动到线上)
  627. private updateDrifting(deltaTime: number) {
  628. if (!this.driftTargetPosition) {
  629. // 如果没有漂移目标位置,直接切换到移动状态
  630. this.state = EnemyState.MOVING;
  631. return;
  632. }
  633. const currentWorldPos = this.node.worldPosition.clone();
  634. const targetWorldPos = this.driftTargetPosition.clone();
  635. const dir = targetWorldPos.subtract(currentWorldPos);
  636. const distanceToTarget = dir.length();
  637. // 如果距离目标很近,切换到移动状态
  638. const stopDistance = 5; // 减小停止距离以提高精度
  639. if (distanceToTarget <= stopDistance) {
  640. this.state = EnemyState.MOVING;
  641. // 确保刚体速度为零
  642. this.stopRigidBodyMovement();
  643. return;
  644. }
  645. if (distanceToTarget === 0) return;
  646. dir.normalize();
  647. // 使用代码控制移动,不依赖物理系统
  648. const driftSpeed = Math.min(this.speed * 8, 500); // 限制最大漂移速度为5500
  649. const moveDistance = driftSpeed * deltaTime;
  650. const actualMoveDistance = Math.min(moveDistance, distanceToTarget);
  651. const newWorldPos = currentWorldPos.add(dir.multiplyScalar(actualMoveDistance));
  652. // 直接设置世界坐标
  653. this.node.setWorldPosition(newWorldPos);
  654. // 确保刚体速度为零,避免物理系统干扰
  655. this.stopRigidBodyMovement();
  656. }
  657. // 更新移动逻辑
  658. private updateMovement(deltaTime: number) {
  659. // 根据配置的移动模式执行不同的移动逻辑
  660. switch (this.movementPattern) {
  661. case 'direct':
  662. this.updateDirectMovement(deltaTime);
  663. break;
  664. case 'patrol':
  665. this.updatePatrolMovement(deltaTime);
  666. break;
  667. default:
  668. this.updateDirectMovement(deltaTime);
  669. break;
  670. }
  671. }
  672. // 直线移动模式
  673. private updateDirectMovement(deltaTime: number) {
  674. this.moveTowardsTarget(deltaTime);
  675. }
  676. // 巡逻移动模式
  677. private updatePatrolMovement(deltaTime: number) {
  678. // 更新方向变化计时器
  679. this.changeDirectionTime += deltaTime;
  680. console.log(`[EnemyInstance] 摆动 调用,changeDirectionTime=${this.changeDirectionTime}`);
  681. // 根据巡逻范围决定方向变化
  682. const enemySprite = this.node.getChildByName('EnemySprite');
  683. if (enemySprite && this.controller) {
  684. const currentPos = enemySprite.worldPosition;
  685. const bounds = this.controller.gameBounds;
  686. // 检查是否到达巡逻边界
  687. const leftBound = bounds.left + this.patrolRange;
  688. const rightBound = bounds.right - this.patrolRange;
  689. if (currentPos.x <= leftBound && this.movingDirection < 0) {
  690. this.movingDirection = 1; // 向右
  691. this.changeDirectionTime = 0;
  692. } else if (currentPos.x >= rightBound && this.movingDirection > 0) {
  693. this.movingDirection = -1; // 向左
  694. this.changeDirectionTime = 0;
  695. }
  696. // 随机改变Y目标位置
  697. if (this.changeDirectionTime >= 3 + Math.random() * 2) {
  698. const centerY = (bounds.top + bounds.bottom) / 2;
  699. const range = (bounds.top - bounds.bottom) * 0.4;
  700. this.targetY = centerY + (Math.random() - 0.5) * range;
  701. this.changeDirectionTime = 0;
  702. }
  703. }
  704. this.moveTowardsTarget(deltaTime);
  705. }
  706. // 向目标移动
  707. private moveTowardsTarget(deltaTime: number) {
  708. // 获取当前速度(考虑狂暴加成和速度变化)
  709. const enemyComponent = this.getComponent(EnemyComponent);
  710. let currentSpeed = enemyComponent ? enemyComponent.getCurrentSpeed() : this.speed;
  711. // 应用速度变化配置,限制变化幅度防止过大跳跃
  712. if (this.speedVariation > 0) {
  713. const maxVariation = Math.min(this.speedVariation, currentSpeed * 0.3); // 限制变化不超过当前速度的30%
  714. const variation = (Math.random() - 0.5) * 2 * maxVariation;
  715. currentSpeed = Math.max(currentSpeed + variation, currentSpeed * 0.5); // 确保速度不会过低
  716. }
  717. // 统一使用主节点的世界坐标,与updateDrifting保持一致
  718. const currentPos = this.node.worldPosition.clone();
  719. let dir = new Vec3();
  720. // 水平移动方向
  721. dir.x = this.movingDirection;
  722. // 垂直移动方向 - 向目标Y位置移动
  723. const yDiff = this.targetY - currentPos.y;
  724. if (Math.abs(yDiff) > 10) { // 如果距离目标Y位置超过10像素
  725. dir.y = yDiff > 0 ? 0.5 : -0.5; // 缓慢向目标Y移动
  726. } else {
  727. dir.y = 0;
  728. }
  729. // 如果有具体的目标位置(墙体),优先移动到墙体
  730. if (this.targetPosition) {
  731. const targetDir = new Vec3();
  732. Vec3.subtract(targetDir, this.targetPosition.clone(), currentPos);
  733. if (targetDir.length() > 20) { // 距离墙体还有一定距离时
  734. targetDir.normalize();
  735. dir.x = targetDir.x * 0.7; // 70%朝向墙体
  736. dir.y = targetDir.y * 0.7;
  737. }
  738. } else if (this.targetFence) {
  739. const fencePos = this.targetFence.worldPosition.clone();
  740. const targetDir = new Vec3();
  741. Vec3.subtract(targetDir, fencePos, currentPos);
  742. if (targetDir.length() > 20) {
  743. targetDir.normalize();
  744. dir.x = targetDir.x * 0.7;
  745. dir.y = targetDir.y * 0.7;
  746. }
  747. }
  748. // 归一化方向向量
  749. if (dir.length() > 0) {
  750. dir.normalize();
  751. }
  752. // 应用摆动效果(根据配置)
  753. if (this.moveType === 'sway' && this.swingAmplitude > 0) {
  754. this.swayTime += deltaTime;
  755. const swayAmount = Math.sin(this.swayTime * this.swingFrequency) * this.swingAmplitude;
  756. // 计算垂直于移动方向的摆动
  757. const swayDir = new Vec3(-dir.y, dir.x, 0);
  758. swayDir.multiplyScalar(swayAmount / 100); // 将摆动幅度转换为合适的比例
  759. dir.add(swayDir);
  760. }
  761. // 限制单帧移动距离,防止突然跳跃
  762. const moveDistance = currentSpeed * deltaTime;
  763. const maxMoveDistance = Math.min(15, currentSpeed * 0.1); // 限制单帧最大移动距离
  764. const actualMoveDistance = Math.min(moveDistance, maxMoveDistance);
  765. const moveVector = dir.multiplyScalar(actualMoveDistance);
  766. const newPos = new Vec3();
  767. Vec3.add(newPos, currentPos, moveVector);
  768. // 统一使用主节点的setWorldPosition,与updateDrifting保持一致
  769. this.node.setWorldPosition(newPos);
  770. // 确保刚体速度为零,避免物理系统干扰
  771. this.stopRigidBodyMovement();
  772. }
  773. // 更新攻击逻辑
  774. private updateAttack(deltaTime: number) {
  775. // 获取敌人的攻击范围配置
  776. const enemyComponent = this.getComponent(EnemyComponent);
  777. const attackRange = enemyComponent ? enemyComponent.getAttackRange() : 30;
  778. // 无论近战还是远程攻击,在攻击状态下都停止移动
  779. const enemySprite = this.node.getChildByName('EnemySprite');
  780. if (enemySprite) {
  781. const rigidBody = enemySprite.getComponent(RigidBody2D);
  782. if (rigidBody) {
  783. // 停止物理移动
  784. rigidBody.linearVelocity = new Vec2(0, 0);
  785. }
  786. }
  787. this.attackTimer -= deltaTime;
  788. if (this.attackTimer <= 0) {
  789. // 执行攻击
  790. this.performAttack();
  791. // 重置攻击计时器
  792. this.attackTimer = this.attackInterval;
  793. } else {
  794. // 攻击冷却期间播放待机动画(只有在不播放攻击动画时)
  795. if (!this.isPlayingAttackAnimation) {
  796. this.playIdleAnimationSafe();
  797. }
  798. }
  799. }
  800. // 待机状态更新(停止移动但可以攻击)
  801. private updateIdle(deltaTime: number) {
  802. // 在待机状态下停止移动
  803. const enemySprite = this.node.getChildByName('EnemySprite');
  804. if (enemySprite) {
  805. const rigidBody = enemySprite.getComponent(RigidBody2D);
  806. if (rigidBody) {
  807. // 停止物理移动
  808. rigidBody.linearVelocity = new Vec2(0, 0);
  809. }
  810. }
  811. this.attackTimer -= deltaTime;
  812. if (this.attackTimer <= 0) {
  813. // 执行攻击
  814. this.performAttack();
  815. // 重置攻击计时器
  816. this.attackTimer = this.attackInterval;
  817. } else {
  818. // 攻击冷却期间播放待机动画(只有在不播放攻击动画时)
  819. if (!this.isPlayingAttackAnimation) {
  820. this.playIdleAnimationSafe();
  821. }
  822. }
  823. }
  824. // 执行攻击
  825. private performAttack() {
  826. if (!this.controller) {
  827. return;
  828. }
  829. const enemyComponent = this.getComponent(EnemyComponent);
  830. const attackType = enemyComponent ? enemyComponent.getAttackType() : 'melee';
  831. // 播放攻击音效
  832. EnemyAudio.playAttackSound(this.enemyConfig);
  833. // 根据攻击类型执行不同的攻击逻辑
  834. if (attackType === 'ranged' || attackType === 'projectile' || attackType.includes('projectile')) {
  835. console.log(`[EnemyInstance] 执行远程攻击,攻击类型: ${attackType}`);
  836. this.performRangedAttack();
  837. } else {
  838. // 近战攻击:播放攻击动画,动画结束后造成伤害
  839. console.log(`[EnemyInstance] 执行近战攻击,攻击类型: ${attackType}`);
  840. this.playAttackAnimationWithDamage();
  841. }
  842. }
  843. // 执行远程攻击(投掷)
  844. private performRangedAttack() {
  845. const enemyComponent = this.getComponent(EnemyComponent);
  846. if (!enemyComponent) return;
  847. // 远程攻击也需要播放攻击动画,在动画即将结束时发射抛掷物
  848. this.playRangedAttackAnimationWithProjectile();
  849. }
  850. // 播放远程攻击动画并在动画结束时发射抛掷物
  851. private playRangedAttackAnimationWithProjectile() {
  852. if (!this.skeleton) {
  853. // 如果没有骨骼动画,直接发射抛掷物
  854. this.fireProjectile();
  855. return;
  856. }
  857. const enemyComp = this.getComponent('EnemyComponent') as any;
  858. const anims = enemyComp?.getAnimations ? enemyComp.getAnimations() : {};
  859. const attackName = anims.attack ?? 'attack';
  860. const animation = this.skeleton.findAnimation(attackName);
  861. if (animation) {
  862. // 播放攻击动画(不循环)
  863. this.skeleton.setAnimation(0, attackName, false);
  864. this.currentAnimationName = attackName;
  865. this.isPlayingAttackAnimation = true;
  866. console.log(`[EnemyInstance] 播放远程攻击动画: ${attackName}`);
  867. // 动画切换后延迟更新血条位置,确保动画尺寸已生效
  868. this.scheduleOnce(() => {
  869. this.updateHPBarPosition();
  870. }, 0.1);
  871. // 获取动画时长并在动画即将结束时发射抛掷物
  872. const animationDuration = animation.duration;
  873. console.log(`[EnemyInstance] 远程攻击动画时长: ${animationDuration}秒`);
  874. // 在动画播放到90%时发射抛掷物,让抛掷物发射与动画同步
  875. const projectileFireTime = animationDuration * 0.9;
  876. // 在动画播放到90%时发射抛掷物
  877. this.scheduleOnce(() => {
  878. // 发射抛掷物
  879. this.fireProjectile();
  880. }, projectileFireTime);
  881. // 动画完成后根据当前状态切换动画
  882. this.scheduleOnce(() => {
  883. this.isPlayingAttackAnimation = false;
  884. if (this.state === EnemyState.IDLE || this.state === EnemyState.ATTACKING) {
  885. // 如果是待机状态或攻击状态,切换回待机动画
  886. this.playIdleAnimation();
  887. } else {
  888. // 其他状态切换回行走动画
  889. this.playWalkAnimation();
  890. }
  891. }, animationDuration);
  892. } else {
  893. // 如果找不到攻击动画,直接发射抛掷物
  894. this.fireProjectile();
  895. }
  896. }
  897. // 发射抛掷物
  898. private fireProjectile() {
  899. const enemyComponent = this.getComponent(EnemyComponent);
  900. if (!enemyComponent) return;
  901. const projectileType = enemyComponent.getProjectileType();
  902. const projectileSpeed = enemyComponent.getProjectileSpeed();
  903. const attackDamage = enemyComponent.getDamage();
  904. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 发射投掷物: ${projectileType}, 速度: ${projectileSpeed}, 伤害: ${attackDamage}`);
  905. // 获取EnemyProjectile组件来创建抛掷物
  906. if (this.controller) {
  907. console.log('[EnemyInstance] 找到controller,开始获取EnemyProjectile组件');
  908. const enemyProjectile = this.controller.getComponent('EnemyProjectile') as any;
  909. if (enemyProjectile) {
  910. console.log('[EnemyInstance] 找到EnemyProjectile组件,准备创建抛掷物');
  911. // 计算抛掷物发射方向(朝向目标墙体)
  912. let direction = new Vec3(0, -1, 0); // 默认向下
  913. if (this.collidedWall) {
  914. const currentPos = this.node.worldPosition;
  915. const wallPos = this.collidedWall.worldPosition;
  916. direction = wallPos.clone().subtract(currentPos).normalize();
  917. console.log('[EnemyInstance] 计算抛掷物方向,目标墙体:', this.collidedWall.name, '方向:', direction);
  918. } else {
  919. console.log('[EnemyInstance] 未找到目标墙体,使用默认方向');
  920. }
  921. // 创建抛掷物配置
  922. const projectileConfig = {
  923. damage: attackDamage,
  924. speed: projectileSpeed,
  925. direction: direction,
  926. projectileType: projectileType,
  927. startPosition: this.node.worldPosition.clone()
  928. };
  929. console.log('[EnemyInstance] 抛掷物配置:', projectileConfig);
  930. // 创建抛掷物
  931. const projectileNode = enemyProjectile.createProjectile(projectileConfig);
  932. if (projectileNode) {
  933. console.log(`[EnemyInstance] 成功创建抛掷物,目标方向: ${direction.toString()}`);
  934. } else {
  935. console.error(`[EnemyInstance] 创建抛掷物失败 - 远程敌人攻击无效`);
  936. }
  937. } else {
  938. console.error(`[EnemyInstance] 未找到EnemyProjectile组件 - 远程敌人攻击无效`);
  939. }
  940. } else {
  941. console.error(`[EnemyInstance] 没有controller引用 - 远程敌人攻击无效`);
  942. }
  943. }
  944. // 播放行走动画
  945. private playWalkAnimation() {
  946. if (!this.skeleton) return;
  947. const enemyComp = this.getComponent('EnemyComponent') as any;
  948. const anims = enemyComp?.getAnimations ? enemyComp.getAnimations() : {};
  949. const walkName = anims.walk ?? 'walk';
  950. const idleName = anims.idle ?? 'idle';
  951. let animationToPlay = '';
  952. if (this.skeleton.findAnimation(walkName)) {
  953. animationToPlay = walkName;
  954. } else if (this.skeleton.findAnimation(idleName)) {
  955. animationToPlay = idleName;
  956. }
  957. if (animationToPlay && this.currentAnimationName !== animationToPlay) {
  958. this.skeleton.setAnimation(0, animationToPlay, true);
  959. this.currentAnimationName = animationToPlay;
  960. this.isPlayingAttackAnimation = false;
  961. console.log(`[EnemyInstance] 播放行走动画: ${animationToPlay}`);
  962. }
  963. // 动画切换后延迟更新血条位置,确保动画尺寸已生效
  964. this.scheduleOnce(() => {
  965. this.updateHPBarPosition();
  966. }, 0.1);
  967. }
  968. // 播放攻击动画
  969. private playAttackAnimation() {
  970. if (!this.skeleton) return;
  971. const enemyComp2 = this.getComponent('EnemyComponent') as any;
  972. const anims2 = enemyComp2?.getAnimations ? enemyComp2.getAnimations() : {};
  973. const attackName = anims2.attack ?? 'attack';
  974. if (this.skeleton.findAnimation(attackName) && this.currentAnimationName !== attackName) {
  975. this.skeleton.setAnimation(0, attackName, true);
  976. this.currentAnimationName = attackName;
  977. this.isPlayingAttackAnimation = true;
  978. console.log(`[EnemyInstance] 播放攻击动画: ${attackName}`);
  979. }
  980. // 动画切换后延迟更新血条位置,确保动画尺寸已生效
  981. this.scheduleOnce(() => {
  982. this.updateHPBarPosition();
  983. }, 0.1);
  984. }
  985. // 播放待机动画
  986. private playIdleAnimation() {
  987. if (!this.skeleton) return;
  988. const enemyComp = this.getComponent('EnemyComponent') as any;
  989. const anims = enemyComp?.getAnimations ? enemyComp.getAnimations() : {};
  990. const idleName = anims.idle ?? 'idle';
  991. if (this.skeleton.findAnimation(idleName) && this.currentAnimationName !== idleName) {
  992. this.skeleton.setAnimation(0, idleName, true);
  993. this.currentAnimationName = idleName;
  994. this.isPlayingAttackAnimation = false;
  995. console.log(`[EnemyInstance] 播放待机动画: ${idleName}`);
  996. }
  997. // 动画切换后延迟更新血条位置,确保动画尺寸已生效
  998. this.scheduleOnce(() => {
  999. this.updateHPBarPosition();
  1000. }, 0.1);
  1001. }
  1002. // 安全播放待机动画(避免重复调用)
  1003. private playIdleAnimationSafe() {
  1004. if (!this.skeleton) return;
  1005. const enemyComp = this.getComponent('EnemyComponent') as any;
  1006. const anims = enemyComp?.getAnimations ? enemyComp.getAnimations() : {};
  1007. const idleName = anims.idle ?? 'idle';
  1008. // 只有当前不是待机动画时才播放
  1009. if (this.skeleton.findAnimation(idleName) && this.currentAnimationName !== idleName && !this.isPlayingAttackAnimation) {
  1010. this.skeleton.setAnimation(0, idleName, true);
  1011. this.currentAnimationName = idleName;
  1012. console.log(`[EnemyInstance] 安全播放待机动画: ${idleName}`);
  1013. // 动画切换后延迟更新血条位置,确保动画尺寸已生效
  1014. this.scheduleOnce(() => {
  1015. this.updateHPBarPosition();
  1016. }, 0.1);
  1017. }
  1018. }
  1019. // 播放攻击动画并在动画结束时造成伤害
  1020. private playAttackAnimationWithDamage() {
  1021. if (!this.skeleton) {
  1022. // 如果没有骨骼动画,直接造成伤害
  1023. this.dealDamageToWall();
  1024. return;
  1025. }
  1026. const enemyComp = this.getComponent('EnemyComponent') as any;
  1027. const anims = enemyComp?.getAnimations ? enemyComp.getAnimations() : {};
  1028. const attackName = anims.attack ?? 'attack';
  1029. const animation = this.skeleton.findAnimation(attackName);
  1030. if (animation) {
  1031. // 播放攻击动画(不循环)
  1032. this.skeleton.setAnimation(0, attackName, false);
  1033. this.currentAnimationName = attackName;
  1034. this.isPlayingAttackAnimation = true;
  1035. console.log(`[EnemyInstance] 播放近战攻击动画: ${attackName}`);
  1036. // 动画切换后延迟更新血条位置,确保动画尺寸已生效
  1037. this.scheduleOnce(() => {
  1038. this.updateHPBarPosition();
  1039. }, 0.1);
  1040. // 获取动画时长并在动画结束时造成伤害
  1041. const animationDuration = animation.duration;
  1042. console.log(`[EnemyInstance] 攻击动画时长: ${animationDuration}秒`);
  1043. // 使用定时器在动画结束时造成伤害
  1044. this.scheduleOnce(() => {
  1045. this.dealDamageToWall();
  1046. this.isPlayingAttackAnimation = false;
  1047. // 动画完成后根据当前状态切换动画
  1048. if (this.state === EnemyState.IDLE || this.state === EnemyState.ATTACKING) {
  1049. // 如果是待机状态或攻击状态,切换回待机动画
  1050. this.playIdleAnimation();
  1051. } else {
  1052. // 其他状态切换回行走动画
  1053. this.playWalkAnimation();
  1054. }
  1055. }, animationDuration);
  1056. } else {
  1057. // 如果找不到攻击动画,直接造成伤害
  1058. this.dealDamageToWall();
  1059. }
  1060. }
  1061. // 对墙体造成伤害
  1062. private dealDamageToWall() {
  1063. if (this.controller) {
  1064. // 获取当前攻击力(考虑狂暴加成)
  1065. const enemyComponent = this.getComponent(EnemyComponent);
  1066. const currentAttackPower = enemyComponent ? enemyComponent.getCurrentAttackPower() : this.attackPower;
  1067. // 检查是否需要触发画面震动
  1068. if (enemyComponent && enemyComponent.getCausesWallShake()) {
  1069. const attackType = enemyComponent.getAttackType();
  1070. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 攻击墙体,触发画面震动,攻击类型: ${attackType}`);
  1071. // 根据攻击类型触发对应的震动效果
  1072. ScreenShakeManager.getInstance().playShakeByAttackType(attackType);
  1073. }
  1074. // 使用统一的事件机制触发墙体伤害
  1075. EventBus.getInstance().emit(GameEvents.ENEMY_DAMAGE_WALL, {
  1076. damage: currentAttackPower,
  1077. source: this.node
  1078. });
  1079. }
  1080. }
  1081. private playDeathAnimationAndDestroy() {
  1082. console.log(`[EnemyInstance] 开始播放死亡动画并销毁`);
  1083. // 播放死亡音效
  1084. EnemyAudio.playDeathSound(this.enemyConfig);
  1085. if (this.skeleton) {
  1086. const enemyComp = this.getComponent('EnemyComponent') as any;
  1087. const anims = enemyComp?.getAnimations ? enemyComp.getAnimations() : {};
  1088. const deathName = anims.dead ?? 'dead';
  1089. if (this.skeleton.findAnimation(deathName)) {
  1090. this.skeleton.setAnimation(0, deathName, false);
  1091. // 销毁节点在动画完毕后
  1092. this.skeleton.setCompleteListener(() => {
  1093. this.node.destroy();
  1094. });
  1095. return;
  1096. }
  1097. }
  1098. this.node.destroy();
  1099. }
  1100. private spawnCoin() {
  1101. const ctrl = this.controller as any; // EnemyController
  1102. if (!ctrl?.coinPrefab) return;
  1103. // 获取配置中的掉落金币数量
  1104. const dropCoinsCount = this.getDropCoins();
  1105. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 掉落 ${dropCoinsCount} 个金币`);
  1106. // 根据配置掉落相应数量的金币
  1107. for (let i = 0; i < dropCoinsCount; i++) {
  1108. const coin = instantiate(ctrl.coinPrefab);
  1109. find('Canvas')!.addChild(coin); // 放到 UI 层
  1110. const pos = new Vec3();
  1111. this.node.getWorldPosition(pos); // 取死亡敌人的世界坐标
  1112. // 如果掉落多个金币,添加一些随机偏移避免重叠
  1113. if (dropCoinsCount > 1) {
  1114. const offsetX = (Math.random() - 0.5) * 40; // 随机X偏移 -20到20
  1115. const offsetY = (Math.random() - 0.5) * 40; // 随机Y偏移 -20到20
  1116. pos.x += offsetX;
  1117. pos.y += offsetY;
  1118. }
  1119. coin.worldPosition = pos; // 金币就在敌人身上出现
  1120. }
  1121. }
  1122. /**
  1123. * 暂停敌人
  1124. */
  1125. public pause(): void {
  1126. this.isPaused = true;
  1127. // 暂停物理系统
  1128. const enemySprite = this.node.getChildByName('EnemySprite');
  1129. if (enemySprite) {
  1130. const rigidBody = enemySprite.getComponent(RigidBody2D);
  1131. if (rigidBody) {
  1132. // 保存当前速度并停止物理移动
  1133. (this as any).savedVelocity = rigidBody.linearVelocity;
  1134. rigidBody.linearVelocity = new Vec2(0, 0);
  1135. }
  1136. }
  1137. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 已暂停`);
  1138. }
  1139. /**
  1140. * 恢复敌人
  1141. */
  1142. public resume(): void {
  1143. this.isPaused = false;
  1144. // 恢复物理系统
  1145. const enemySprite = this.node.getChildByName('EnemySprite');
  1146. if (enemySprite) {
  1147. const rigidBody = enemySprite.getComponent(RigidBody2D);
  1148. if (rigidBody && (this as any).savedVelocity) {
  1149. // 恢复之前保存的速度
  1150. rigidBody.linearVelocity = (this as any).savedVelocity;
  1151. (this as any).savedVelocity = null;
  1152. }
  1153. }
  1154. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 已恢复`);
  1155. }
  1156. /**
  1157. * 检查是否暂停
  1158. */
  1159. public isPausedState(): boolean {
  1160. return this.isPaused;
  1161. }
  1162. /**
  1163. * 获取敌人当前状态
  1164. */
  1165. public getState(): EnemyState {
  1166. return this.state;
  1167. }
  1168. /**
  1169. * 检查敌人是否处于漂移状态
  1170. */
  1171. public isDrifting(): boolean {
  1172. return this.state === EnemyState.DRIFTING;
  1173. }
  1174. /**
  1175. * 重置血条状态(满血并隐藏)
  1176. */
  1177. public resetHealthBar(): void {
  1178. if (this.hpBarAnimation) {
  1179. this.hpBarAnimation.resetToFullAndHide();
  1180. } else {
  1181. // 备用方案:直接隐藏血条
  1182. const hpBar = this.node.getChildByName('HPBar');
  1183. if (hpBar) {
  1184. hpBar.active = false;
  1185. const progressBar = hpBar.getComponent(ProgressBar);
  1186. if (progressBar) {
  1187. progressBar.progress = 1.0;
  1188. }
  1189. }
  1190. }
  1191. }
  1192. /**
  1193. * 尝试格挡攻击
  1194. */
  1195. private tryBlock(): boolean {
  1196. const enemyComponent = this.getComponent(EnemyComponent);
  1197. if (!enemyComponent) return false;
  1198. const blockChance = enemyComponent.getBlockChance();
  1199. // 将百分比值转换为0-1范围(例如:30.0 -> 0.3)
  1200. const blockChanceNormalized = blockChance / 100.0;
  1201. return Math.random() < blockChanceNormalized;
  1202. }
  1203. /**
  1204. * 基于距离检查墙体碰撞和攻击范围
  1205. */
  1206. private checkWallCollisionByDistance(): void {
  1207. if (this.state === EnemyState.ATTACKING || this.state === EnemyState.IDLE) {
  1208. return; // 已经在攻击状态或待机状态,不需要再检查
  1209. }
  1210. const enemySprite = this.node.getChildByName('EnemySprite');
  1211. if (!enemySprite) return;
  1212. const currentPos = enemySprite.worldPosition;
  1213. // 获取敌人的攻击范围配置
  1214. const enemyComponent = this.getComponent(EnemyComponent);
  1215. const attackRange = enemyComponent ? enemyComponent.getAttackRange() : 30;
  1216. // 根据攻击范围确定检测距离和攻击类型
  1217. let detectionDistance: number;
  1218. let isRangedAttack: boolean;
  1219. if (attackRange <= 0) {
  1220. // 近战攻击:需要接近墙体
  1221. detectionDistance = 30;
  1222. isRangedAttack = false;
  1223. } else {
  1224. // 远程攻击:在攻击范围内就开始攻击
  1225. detectionDistance = attackRange;
  1226. isRangedAttack = true;
  1227. }
  1228. // 获取所有墙体节点进行距离检测
  1229. const wallNodes = this.getAllWallNodes();
  1230. for (const wallNode of wallNodes) {
  1231. const wallPos = wallNode.worldPosition;
  1232. const distance = Vec3.distance(currentPos, wallPos);
  1233. if (distance <= detectionDistance) {
  1234. const attackTypeStr = isRangedAttack ? '远程攻击' : '近战攻击';
  1235. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 进入${attackTypeStr}范围,距离墙体 ${wallNode.name}: ${distance.toFixed(2)},攻击范围: ${attackRange}`);
  1236. if (isRangedAttack) {
  1237. // 远程攻击:切换到待机状态,停止移动并轮流播放攻击和待机动画
  1238. this.state = EnemyState.IDLE;
  1239. this.attackTimer = 0; // 立即开始攻击
  1240. this.collidedWall = wallNode; // 记录目标墙体
  1241. // 停止移动
  1242. this.stopRigidBodyMovement();
  1243. // 切换到待机动画
  1244. this.playIdleAnimation();
  1245. console.log(`[EnemyInstance] 远程敌人 ${this.getEnemyName()} 进入待机状态,停止移动并准备攻击`);
  1246. } else {
  1247. // 近战攻击:切换到待机状态,停止移动
  1248. this.state = EnemyState.IDLE;
  1249. this.attackTimer = 0; // 立即开始攻击
  1250. this.collidedWall = wallNode; // 记录碰撞的墙体
  1251. // 停止移动
  1252. this.stopRigidBodyMovement();
  1253. // 切换到待机动画
  1254. this.playIdleAnimation();
  1255. }
  1256. return; // 找到一个墙体就停止检查
  1257. }
  1258. }
  1259. }
  1260. /**
  1261. * 获取所有墙体节点
  1262. */
  1263. private getAllWallNodes(): Node[] {
  1264. const wallNodes: Node[] = [];
  1265. // 通过EnemyController获取墙体节点
  1266. if (this.controller) {
  1267. if (this.controller.topFenceNode) {
  1268. wallNodes.push(this.controller.topFenceNode);
  1269. }
  1270. if (this.controller.bottomFenceNode) {
  1271. wallNodes.push(this.controller.bottomFenceNode);
  1272. }
  1273. }
  1274. // 备用方案:在场景中查找墙体节点
  1275. if (wallNodes.length === 0) {
  1276. const gameArea = find('Canvas/GameLevelUI/GameArea');
  1277. if (gameArea) {
  1278. for (let i = 0; i < gameArea.children.length; i++) {
  1279. const child = gameArea.children[i];
  1280. if (this.isWallNode(child)) {
  1281. wallNodes.push(child);
  1282. }
  1283. }
  1284. }
  1285. }
  1286. return wallNodes;
  1287. }
  1288. /**
  1289. * 停止刚体移动,确保物理系统不干扰代码控制的移动
  1290. */
  1291. private stopRigidBodyMovement(): void {
  1292. const enemySprite = this.node.getChildByName('EnemySprite');
  1293. if (enemySprite) {
  1294. const rigidBody = enemySprite.getComponent(RigidBody2D);
  1295. if (rigidBody) {
  1296. rigidBody.linearVelocity = new Vec2(0, 0);
  1297. }
  1298. }
  1299. }
  1300. /**
  1301. * 显示格挡效果
  1302. */
  1303. private showBlockEffect(): void {
  1304. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 格挡了攻击`);
  1305. // 显示"格挡"文字效果
  1306. this.showBlockText();
  1307. }
  1308. /**
  1309. * 显示格挡文字
  1310. */
  1311. private showBlockText(): void {
  1312. // 使用DamageNumberAni组件来显示格挡文字,就像显示伤害数字一样
  1313. if (this.controller) {
  1314. const damageAni = this.controller.getComponent(DamageNumberAni);
  1315. if (damageAni) {
  1316. damageAni.showBlockText(this.node.worldPosition);
  1317. } else {
  1318. console.warn('[EnemyInstance] 未找到DamageNumberAni组件,无法显示格挡文字');
  1319. }
  1320. } else {
  1321. console.warn('[EnemyInstance] 未找到controller引用,无法显示格挡文字');
  1322. }
  1323. }
  1324. /**
  1325. * 检查是否触发狂暴状态
  1326. */
  1327. private checkRageTrigger(): void {
  1328. const enemyComponent = this.getComponent(EnemyComponent);
  1329. if (!enemyComponent) return;
  1330. const healthPercent = this.health / this.maxHealth;
  1331. const rageTriggerThreshold = enemyComponent.getRageTriggerThreshold();
  1332. if (healthPercent <= rageTriggerThreshold && !enemyComponent.isInRage()) {
  1333. enemyComponent.triggerRage();
  1334. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 进入狂暴状态`);
  1335. }
  1336. }
  1337. // ===== 隐身相关方法 =====
  1338. /**
  1339. * 进入隐身状态
  1340. * @param duration 隐身持续时间(秒)
  1341. */
  1342. public enterStealth(duration: number = 5.0): void {
  1343. if (this.state === EnemyState.DEAD) {
  1344. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 已死亡,无法进入隐身状态`);
  1345. return; // 死亡状态下不能隐身
  1346. }
  1347. if (this.stealthActive) {
  1348. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 已经处于隐身状态,跳过重复进入`);
  1349. return; // 已经在隐身状态,不重复进入
  1350. }
  1351. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 进入隐身状态,持续时间: ${duration}秒,当前状态: ${this.state}`);
  1352. // 隐身为叠加标记,不改变行为状态
  1353. this.stealthActive = true;
  1354. this.stealthDuration = duration;
  1355. this.stealthTimer = 0;
  1356. // 应用隐身视觉效果
  1357. this.applyStealthVisualEffect();
  1358. // 直接更新攻击状态,确保不依赖事件监听顺序
  1359. const attackStateManager = EnemyAttackStateManager.getInstance();
  1360. attackStateManager.setEnemyAttackable(this.node, false);
  1361. console.log(`[EnemyInstance] 已直接将 ${this.getEnemyName()} 标记为不可攻击(隐身中)`);
  1362. // 触发隐身事件
  1363. EventBus.getInstance().emit(GameEvents.ENEMY_STEALTH_START, {
  1364. enemy: this,
  1365. duration: duration
  1366. });
  1367. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 隐身状态激活,透明度已调整`);
  1368. }
  1369. /**
  1370. * 退出隐身状态
  1371. */
  1372. public exitStealth(): void {
  1373. if (!this.stealthActive) {
  1374. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 不在隐身状态(当前状态: ${this.state}),无需退出`);
  1375. return; // 不在隐身状态
  1376. }
  1377. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 退出隐身状态`);
  1378. // 结束隐身:行为状态不变,仅关闭叠加标记
  1379. this.stealthActive = false;
  1380. this.stealthTimer = 0;
  1381. this.stealthDuration = 0;
  1382. // 移除隐身视觉效果
  1383. this.removeStealthVisualEffect();
  1384. // 触发隐身结束事件
  1385. EventBus.getInstance().emit(GameEvents.ENEMY_STEALTH_END, {
  1386. enemy: this
  1387. });
  1388. // 直接更新攻击状态,确保不依赖事件监听顺序
  1389. const attackStateManager2 = EnemyAttackStateManager.getInstance();
  1390. attackStateManager2.setEnemyAttackable(this.node, true);
  1391. console.log(`[EnemyInstance] 已直接将 ${this.getEnemyName()} 标记为可攻击(退出隐身)`);
  1392. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 隐身状态结束,透明度已恢复`);
  1393. // 若具备隐身能力,按照冷却时间再次尝试进入隐身(在回调中再检查是否已死亡/节点失效)
  1394. if (this.hasStealthAbility) {
  1395. this.scheduleOnce(() => {
  1396. if (!this.node || !this.node.isValid) return;
  1397. if (this.state === EnemyState.DEAD) return;
  1398. this.enterStealth(this.stealthCooldown);
  1399. }, this.stealthCooldown);
  1400. }
  1401. }
  1402. /**
  1403. * 检查是否处于隐身状态
  1404. * @returns 是否隐身
  1405. */
  1406. public isStealth(): boolean {
  1407. return this.stealthActive;
  1408. }
  1409. /**
  1410. * 应用隐身视觉效果
  1411. */
  1412. private applyStealthVisualEffect(): void {
  1413. const enemySprite = this.node.getChildByName('EnemySprite');
  1414. if (!enemySprite) {
  1415. console.warn('[EnemyInstance] 未找到EnemySprite节点,无法应用隐身视觉效果');
  1416. return;
  1417. }
  1418. // 保存原始透明度
  1419. const uiOpacity = enemySprite.getComponent(UIOpacity);
  1420. if (uiOpacity) {
  1421. this.originalOpacity = uiOpacity.opacity;
  1422. // 使用缓动动画降低透明度到30%
  1423. tween(uiOpacity)
  1424. .to(0.5, { opacity: 77 }) // 30% 透明度 (255 * 0.3 ≈ 77)
  1425. .start();
  1426. } else {
  1427. // 如果没有UIOpacity组件,添加一个
  1428. const newUIOpacity = enemySprite.addComponent(UIOpacity);
  1429. this.originalOpacity = 255;
  1430. newUIOpacity.opacity = 255;
  1431. tween(newUIOpacity)
  1432. .to(0.5, { opacity: 77 })
  1433. .start();
  1434. }
  1435. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 应用隐身视觉效果`);
  1436. }
  1437. /**
  1438. * 移除隐身视觉效果
  1439. */
  1440. private removeStealthVisualEffect(): void {
  1441. const enemySprite = this.node.getChildByName('EnemySprite');
  1442. if (!enemySprite) {
  1443. console.warn('[EnemyInstance] 未找到EnemySprite节点,无法移除隐身视觉效果');
  1444. return;
  1445. }
  1446. const uiOpacity = enemySprite.getComponent(UIOpacity);
  1447. if (uiOpacity) {
  1448. // 使用缓动动画恢复原始透明度
  1449. tween(uiOpacity)
  1450. .to(0.5, { opacity: this.originalOpacity })
  1451. .start();
  1452. }
  1453. console.log(`[EnemyInstance] 敌人 ${this.getEnemyName()} 移除隐身视觉效果`);
  1454. }
  1455. /**
  1456. * 更新隐身状态
  1457. * @param deltaTime 帧时间间隔
  1458. */
  1459. private updateStealth(deltaTime: number): void {
  1460. if (!this.stealthActive) {
  1461. return;
  1462. }
  1463. this.stealthTimer += deltaTime;
  1464. // 检查隐身时间是否结束
  1465. if (this.stealthTimer >= this.stealthDuration) {
  1466. this.exitStealth();
  1467. }
  1468. }
  1469. }