EnemyComponent.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { _decorator, Component } from 'cc';
  2. import { ConfigManager, EnemyConfig } from '../Core/ConfigManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 敌人组件
  6. * 用于存储敌人的配置信息和基础属性
  7. */
  8. @ccclass('EnemyComponent')
  9. export class EnemyComponent extends Component {
  10. public enemyConfig: EnemyConfig = null;
  11. public spawner: any = null; // 对生成器的引用
  12. private rageActive: boolean = false;
  13. private rageStartTime: number = 0;
  14. // 获取敌人生命值
  15. public getHealth(): number {
  16. return this.enemyConfig?.stats?.health || 100;
  17. }
  18. // 获取敌人速度
  19. public getSpeed(): number {
  20. return this.enemyConfig?.stats?.speed || 50;
  21. }
  22. // 获取敌人伤害
  23. public getDamage(): number {
  24. return this.enemyConfig?.combat?.attackDamage || 20;
  25. }
  26. // 获取敌人攻击范围
  27. public getAttackRange(): number {
  28. return this.enemyConfig?.combat?.attackRange || 30;
  29. }
  30. // 获取敌人攻击速度
  31. public getAttackSpeed(): number {
  32. return this.enemyConfig?.combat?.attackSpeed || 1.0;
  33. }
  34. // 获取敌人防御力
  35. public getDefense(): number {
  36. return this.enemyConfig?.stats?.defense || 0;
  37. }
  38. // 获取击杀奖励
  39. public getCoinReward(): number {
  40. return this.enemyConfig?.stats?.dropEnergy || 1;
  41. }
  42. // === 移动相关配置 ===
  43. // 获取移动类型
  44. public getMovementType(): string {
  45. return this.enemyConfig?.movement?.moveType || 'straight';
  46. }
  47. // 获取移动模式
  48. public getMovementPattern(): string {
  49. return this.enemyConfig?.movement?.pattern || 'direct';
  50. }
  51. // 获取摆动幅度
  52. public getSwingAmplitude(): number {
  53. return this.enemyConfig?.movement?.swingAmplitude || 0.0;
  54. }
  55. // 获取摆动频率
  56. public getSwingFrequency(): number {
  57. return this.enemyConfig?.movement?.swingFrequency || 0.0;
  58. }
  59. // 获取速度变化
  60. public getSpeedVariation(): number {
  61. return this.enemyConfig?.movement?.speedVariation || 0.1;
  62. }
  63. // 获取旋转速度
  64. public getRotationSpeed(): number {
  65. return this.enemyConfig?.movement?.rotationSpeed || 180.0;
  66. }
  67. // === 战斗相关配置 ===
  68. // 获取攻击类型
  69. public getAttackType(): string {
  70. return this.enemyConfig?.combat?.attackType || 'melee';
  71. }
  72. // 获取攻击冷却时间
  73. public getAttackCooldown(): number {
  74. return this.enemyConfig?.combat?.attackCooldown || 2.0;
  75. }
  76. // 获取攻击延迟
  77. public getAttackDelay(): number {
  78. return this.enemyConfig?.combat?.attackDelay || 1.0;
  79. }
  80. // 获取武器类型
  81. public getWeaponType(): string {
  82. return this.enemyConfig?.combat?.weaponType || 'none';
  83. }
  84. // 获取投掷物类型
  85. public getProjectileType(): string {
  86. return this.enemyConfig?.combat?.projectileType || 'none';
  87. }
  88. // 获取投掷物速度
  89. public getProjectileSpeed(): number {
  90. return this.enemyConfig?.combat?.projectileSpeed || 100.0;
  91. }
  92. // === 格挡系统 ===
  93. // 是否可以格挡
  94. public canBlock(): boolean {
  95. return this.enemyConfig?.combat?.canBlock || false;
  96. }
  97. // 获取格挡几率
  98. public getBlockChance(): number {
  99. return this.enemyConfig?.combat?.blockChance || 0.0;
  100. }
  101. // 获取格挡伤害减免
  102. public getBlockDamageReduction(): number {
  103. return this.enemyConfig?.combat?.blockDamageReduction || 0.5;
  104. }
  105. // === 狂暴系统 ===
  106. // 是否有狂暴能力
  107. public hasRage(): boolean {
  108. return this.enemyConfig?.boss?.rage_threshold > 0 || false;
  109. }
  110. // 获取狂暴触发血量阈值
  111. public getRageThreshold(): number {
  112. return this.enemyConfig?.boss?.rage_threshold || 0.3;
  113. }
  114. // 获取狂暴触发血量阈值(别名方法)
  115. public getRageTriggerThreshold(): number {
  116. return this.getRageThreshold();
  117. }
  118. // 获取狂暴伤害倍率
  119. public getRageDamageMultiplier(): number {
  120. return this.enemyConfig?.boss?.rage_damage_multiplier || 1.5;
  121. }
  122. // 获取狂暴速度倍率
  123. public getRageSpeedMultiplier(): number {
  124. return this.enemyConfig?.boss?.rage_speed_multiplier || 1.3;
  125. }
  126. // 获取狂暴持续时间
  127. public getRageDuration(): number {
  128. return 10.0; // 固定持续时间
  129. }
  130. // === 特殊能力 ===
  131. // 获取特殊能力类型
  132. public getSpecialAbility(): string {
  133. if (this.enemyConfig?.special_abilities && this.enemyConfig.special_abilities.length > 0) {
  134. return this.enemyConfig.special_abilities[0].type || 'none';
  135. }
  136. return 'none';
  137. }
  138. // 获取特殊能力冷却时间
  139. public getSpecialAbilityCooldown(): number {
  140. if (this.enemyConfig?.special_abilities && this.enemyConfig.special_abilities.length > 0) {
  141. return this.enemyConfig.special_abilities[0].cooldown || 5.0;
  142. }
  143. return 5.0;
  144. }
  145. // === 狂暴状态管理 ===
  146. // 检查是否处于狂暴状态
  147. public isInRage(): boolean {
  148. if (!this.rageActive) return false;
  149. // 检查狂暴是否过期
  150. const currentTime = Date.now();
  151. const rageDuration = this.getRageDuration() * 1000; // 转换为毫秒
  152. if (currentTime - this.rageStartTime > rageDuration) {
  153. this.endRage();
  154. return false;
  155. }
  156. return true;
  157. }
  158. // 触发狂暴状态
  159. public triggerRage(): void {
  160. if (!this.hasRage()) return;
  161. this.rageActive = true;
  162. this.rageStartTime = Date.now();
  163. console.log(`[EnemyComponent] 狂暴状态激活,持续时间: ${this.getRageDuration()}秒`);
  164. }
  165. // 结束狂暴状态
  166. public endRage(): void {
  167. this.rageActive = false;
  168. this.rageStartTime = 0;
  169. console.log(`[EnemyComponent] 狂暴状态结束`);
  170. }
  171. // 获取当前攻击力(考虑狂暴加成)
  172. public getCurrentAttackPower(): number {
  173. const baseDamage = this.getDamage();
  174. if (this.isInRage()) {
  175. return baseDamage * this.getRageDamageMultiplier();
  176. }
  177. return baseDamage;
  178. }
  179. // 获取当前移动速度(考虑狂暴加成)
  180. public getCurrentSpeed(): number {
  181. const baseSpeed = this.getSpeed();
  182. if (this.isInRage()) {
  183. return baseSpeed * this.getRageSpeedMultiplier();
  184. }
  185. return baseSpeed;
  186. }
  187. // 获取动画配置
  188. public getAnimations(): any {
  189. return this.enemyConfig?.visualConfig?.animations || {};
  190. }
  191. // 获取音频配置
  192. public getAudioConfig(): any {
  193. return this.enemyConfig?.audioConfig || {};
  194. }
  195. // 检查是否有特殊能力
  196. public hasSpecialAbility(abilityType: string): boolean {
  197. if (!this.enemyConfig || !this.enemyConfig.special_abilities) return false;
  198. return this.enemyConfig.special_abilities.some(ability => ability.type === abilityType);
  199. }
  200. // 获取特殊配置
  201. public getBossConfig(): any {
  202. return this.enemyConfig?.boss || null;
  203. }
  204. // 获取敌人信息文本
  205. public getEnemyInfo(): string {
  206. if (!this.enemyConfig) return '未知敌人';
  207. return `${this.enemyConfig.name}\n` +
  208. `类型: ${this.enemyConfig.type}\n` +
  209. `生命值: ${this.enemyConfig.stats?.health || 0}\n` +
  210. `速度: ${this.enemyConfig.stats?.speed || 0}\n` +
  211. `伤害: ${this.enemyConfig.combat?.attackDamage || 0}\n` +
  212. `攻击范围: ${this.enemyConfig.combat?.attackRange || 0}`;
  213. }
  214. // 死亡时调用
  215. public onDeath() {
  216. if (this.spawner && this.spawner.onEnemyDeath) {
  217. this.spawner.onEnemyDeath(this.node);
  218. }
  219. }
  220. }