EnemyComponent.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // 获取敌人生命值
  13. public getHealth(): number {
  14. return this.enemyConfig?.health || 100;
  15. }
  16. // 获取敌人速度
  17. public getSpeed(): number {
  18. return this.enemyConfig?.speed || 50;
  19. }
  20. // 获取敌人伤害
  21. public getDamage(): number {
  22. return this.enemyConfig?.attack || 20;
  23. }
  24. // 获取敌人攻击范围
  25. public getAttackRange(): number {
  26. return this.enemyConfig?.range || 30;
  27. }
  28. // 获取敌人攻击速度
  29. public getAttackSpeed(): number {
  30. return this.enemyConfig?.attackSpeed || 1.0;
  31. }
  32. // 获取敌人防御力
  33. public getDefense(): number {
  34. return this.enemyConfig?.defense || 0;
  35. }
  36. // 获取击杀奖励
  37. public getCoinReward(): number {
  38. return this.enemyConfig?.goldReward || 10;
  39. }
  40. // 获取移动类型
  41. public getMovementType(): string {
  42. return this.enemyConfig?.movement?.type || 'straight';
  43. }
  44. // 获取移动模式
  45. public getMovementPattern(): string {
  46. return this.enemyConfig?.movement?.pattern || 'walk_forward';
  47. }
  48. // 获取攻击类型
  49. public getAttackType(): string {
  50. return this.enemyConfig?.combat?.attackType || 'melee';
  51. }
  52. // 获取动画配置
  53. public getAnimations(): any {
  54. return this.enemyConfig?.visualConfig?.animations || {};
  55. }
  56. // 获取音频配置
  57. public getAudioConfig(): any {
  58. return this.enemyConfig?.audioConfig || {};
  59. }
  60. // 检查是否有特殊能力
  61. public hasSpecialAbility(abilityType: string): boolean {
  62. if (!this.enemyConfig || !this.enemyConfig.specialAbilities) return false;
  63. return this.enemyConfig.specialAbilities.indexOf(abilityType) !== -1;
  64. }
  65. // 获取特殊配置
  66. public getStealthConfig(): any {
  67. return this.enemyConfig?.stealthConfig || null;
  68. }
  69. public getArmorConfig(): any {
  70. return this.enemyConfig?.armorConfig || null;
  71. }
  72. public getExplosionConfig(): any {
  73. return this.enemyConfig?.explosionConfig || null;
  74. }
  75. public getBossConfig(): any {
  76. return this.enemyConfig?.bossConfig || null;
  77. }
  78. public getProjectileConfig(): any {
  79. return this.enemyConfig?.projectileConfig || null;
  80. }
  81. // 获取敌人信息文本
  82. public getEnemyInfo(): string {
  83. if (!this.enemyConfig) return '未知敌人';
  84. return `${this.enemyConfig.name}\n` +
  85. `类型: ${this.enemyConfig.type}\n` +
  86. `稀有度: ${this.enemyConfig.rarity}\n` +
  87. `生命值: ${this.enemyConfig.health}\n` +
  88. `速度: ${this.enemyConfig.speed}\n` +
  89. `伤害: ${this.enemyConfig.attack}\n` +
  90. `攻击范围: ${this.enemyConfig.range}`;
  91. }
  92. // 死亡时调用
  93. public onDeath() {
  94. if (this.spawner && this.spawner.onEnemyDeath) {
  95. this.spawner.onEnemyDeath(this.node);
  96. }
  97. }
  98. }