import { _decorator, Component } from 'cc'; import { ConfigManager, EnemyConfig } from '../Core/ConfigManager'; const { ccclass, property } = _decorator; /** * 敌人组件 * 用于存储敌人的配置信息和基础属性 */ @ccclass('EnemyComponent') export class EnemyComponent extends Component { public enemyConfig: EnemyConfig = null; public spawner: any = null; // 对生成器的引用 private rageActive: boolean = false; private rageStartTime: number = 0; // 获取敌人生命值 public getHealth(): number { return this.enemyConfig?.stats?.health || 100; } // 获取敌人速度 public getSpeed(): number { return this.enemyConfig?.stats?.speed || 50; } // 获取敌人伤害 public getDamage(): number { return this.enemyConfig?.combat?.attackDamage || 20; } // 获取敌人攻击范围 public getAttackRange(): number { return this.enemyConfig?.combat?.attackRange || 30; } // 获取敌人攻击速度 public getAttackSpeed(): number { return this.enemyConfig?.combat?.attackSpeed || 1.0; } // 获取敌人防御力 public getDefense(): number { return this.enemyConfig?.stats?.defense || 0; } // 获取击杀奖励 public getCoinReward(): number { return this.enemyConfig?.stats?.dropEnergy || 1; } // === 移动相关配置 === // 获取移动类型 public getMovementType(): string { return this.enemyConfig?.movement?.moveType || 'straight'; } // 获取移动模式 public getMovementPattern(): string { return this.enemyConfig?.movement?.pattern || 'direct'; } // 获取摆动幅度 public getSwingAmplitude(): number { return this.enemyConfig?.movement?.swingAmplitude || 0.0; } // 获取摆动频率 public getSwingFrequency(): number { return this.enemyConfig?.movement?.swingFrequency || 0.0; } // 获取速度变化 public getSpeedVariation(): number { return this.enemyConfig?.movement?.speedVariation || 0.1; } // 获取旋转速度 public getRotationSpeed(): number { return this.enemyConfig?.movement?.rotationSpeed || 180.0; } // === 战斗相关配置 === // 获取攻击类型 public getAttackType(): string { return this.enemyConfig?.combat?.attackType || 'melee'; } // 获取攻击冷却时间 public getAttackCooldown(): number { return this.enemyConfig?.combat?.attackCooldown || 2.0; } // 获取攻击延迟 public getAttackDelay(): number { return this.enemyConfig?.combat?.attackDelay || 1.0; } // 获取武器类型 public getWeaponType(): string { return this.enemyConfig?.combat?.weaponType || 'none'; } // 获取投掷物类型 public getProjectileType(): string { return this.enemyConfig?.combat?.projectileType || 'none'; } // 获取投掷物速度 public getProjectileSpeed(): number { return this.enemyConfig?.combat?.projectileSpeed || 100.0; } // === 格挡系统 === // 是否可以格挡 public canBlock(): boolean { return this.enemyConfig?.combat?.canBlock || false; } // 获取格挡几率 public getBlockChance(): number { return this.enemyConfig?.combat?.blockChance || 0.0; } // 获取格挡伤害减免 public getBlockDamageReduction(): number { return this.enemyConfig?.combat?.blockDamageReduction || 0.5; } // === 狂暴系统 === // 是否有狂暴能力 public hasRage(): boolean { return this.enemyConfig?.boss?.rage_threshold > 0 || false; } // 获取狂暴触发血量阈值 public getRageThreshold(): number { return this.enemyConfig?.boss?.rage_threshold || 0.3; } // 获取狂暴触发血量阈值(别名方法) public getRageTriggerThreshold(): number { return this.getRageThreshold(); } // 获取狂暴伤害倍率 public getRageDamageMultiplier(): number { return this.enemyConfig?.boss?.rage_damage_multiplier || 1.5; } // 获取狂暴速度倍率 public getRageSpeedMultiplier(): number { return this.enemyConfig?.boss?.rage_speed_multiplier || 1.3; } // 获取狂暴持续时间 public getRageDuration(): number { return 10.0; // 固定持续时间 } // === 特殊能力 === // 获取特殊能力类型 public getSpecialAbility(): string { if (this.enemyConfig?.special_abilities && this.enemyConfig.special_abilities.length > 0) { return this.enemyConfig.special_abilities[0].type || 'none'; } return 'none'; } // 获取特殊能力冷却时间 public getSpecialAbilityCooldown(): number { if (this.enemyConfig?.special_abilities && this.enemyConfig.special_abilities.length > 0) { return this.enemyConfig.special_abilities[0].cooldown || 5.0; } return 5.0; } // === 狂暴状态管理 === // 检查是否处于狂暴状态 public isInRage(): boolean { if (!this.rageActive) return false; // 检查狂暴是否过期 const currentTime = Date.now(); const rageDuration = this.getRageDuration() * 1000; // 转换为毫秒 if (currentTime - this.rageStartTime > rageDuration) { this.endRage(); return false; } return true; } // 触发狂暴状态 public triggerRage(): void { if (!this.hasRage()) return; this.rageActive = true; this.rageStartTime = Date.now(); console.log(`[EnemyComponent] 狂暴状态激活,持续时间: ${this.getRageDuration()}秒`); } // 结束狂暴状态 public endRage(): void { this.rageActive = false; this.rageStartTime = 0; console.log(`[EnemyComponent] 狂暴状态结束`); } // 获取当前攻击力(考虑狂暴加成) public getCurrentAttackPower(): number { const baseDamage = this.getDamage(); if (this.isInRage()) { return baseDamage * this.getRageDamageMultiplier(); } return baseDamage; } // 获取当前移动速度(考虑狂暴加成) public getCurrentSpeed(): number { const baseSpeed = this.getSpeed(); if (this.isInRage()) { return baseSpeed * this.getRageSpeedMultiplier(); } return baseSpeed; } // 获取动画配置 public getAnimations(): any { return this.enemyConfig?.visualConfig?.animations || {}; } // 获取音频配置 public getAudioConfig(): any { return this.enemyConfig?.audioConfig || {}; } // 检查是否有特殊能力 public hasSpecialAbility(abilityType: string): boolean { if (!this.enemyConfig || !this.enemyConfig.special_abilities) return false; return this.enemyConfig.special_abilities.some(ability => ability.type === abilityType); } // 获取特殊配置 public getBossConfig(): any { return this.enemyConfig?.boss || null; } // 获取敌人信息文本 public getEnemyInfo(): string { if (!this.enemyConfig) return '未知敌人'; return `${this.enemyConfig.name}\n` + `类型: ${this.enemyConfig.type}\n` + `生命值: ${this.enemyConfig.stats?.health || 0}\n` + `速度: ${this.enemyConfig.stats?.speed || 0}\n` + `伤害: ${this.enemyConfig.combat?.attackDamage || 0}\n` + `攻击范围: ${this.enemyConfig.combat?.attackRange || 0}`; } // 死亡时调用 public onDeath() { if (this.spawner && this.spawner.onEnemyDeath) { this.spawner.onEnemyDeath(this.node); } } }