using UnityEngine; using BaseGames.Combat; namespace BaseGames.Player { /// /// 玩家战斗组件。 /// HitBox 随装备武器动态实例化到 [WeaponSocket] 子节点,通过 WeaponManager.ActiveHitBoxInstance 访问。 /// public class PlayerCombat : MonoBehaviour { [SerializeField] private WeaponManager _weaponManager; private PlayerStats _stats; private WeaponHitBoxInstance _currentHitBoxInstance; /// 下劈 HitBox 命中确认事件(供 DownAttackState 订阅 pogo 弹跳逻辑)。 public event System.Action OnDownHitConfirmed; private void Awake() { _stats = GetComponentInParent(); } private void OnEnable() { if (_weaponManager != null) _weaponManager.OnWeaponChanged += HandleWeaponChanged; } private void OnDisable() { if (_weaponManager != null) _weaponManager.OnWeaponChanged -= HandleWeaponChanged; UnsubscribeDownHit(); } private void HandleWeaponChanged(WeaponSO _) { UnsubscribeDownHit(); _currentHitBoxInstance = _weaponManager.ActiveHitBoxInstance; if (_currentHitBoxInstance != null) _currentHitBoxInstance.OnDownHitConfirmed += HandleDownHitConfirmed; } private void UnsubscribeDownHit() { if (_currentHitBoxInstance == null) return; _currentHitBoxInstance.OnDownHitConfirmed -= HandleDownHitConfirmed; _currentHitBoxInstance = null; } private void HandleDownHitConfirmed(DamageInfo info) => OnDownHitConfirmed?.Invoke(info); // ── 连击段伤害来源切换 ──────────────────────────────────────────────── /// /// 根据当前连招段切换 HitBox 的 DamageSource(由 AttackState 在每段开始时调用)。 /// public void SetComboSegmentSource(int comboIndex) { WeaponSO w = _weaponManager?.ActiveWeapon; if (w == null) return; DamageSourceSO src = comboIndex switch { 0 => w.attack1Source, 1 => w.attack2Source, 2 => w.attack3Source, _ => w.attack1Source, }; _weaponManager.ActiveHitBoxInstance?.SetDamageSource(AttackDirection.Ground, src); } // ── HitBox 激活(由 State / AnimationEvent 调用)───────────────────── public void EnableWeaponHitBox(AttackDirection dir) { var source = _weaponManager?.ActiveWeapon?.GetSourceByDir(dir); _weaponManager?.ActiveHitBoxInstance?.Activate(dir, source, transform); } public void DisableWeaponHitBox(AttackDirection dir) => _weaponManager?.ActiveHitBoxInstance?.Deactivate(dir); public void DisableAllWeaponHitBoxes() => _weaponManager?.ActiveHitBoxInstance?.DeactivateAll(); /// 命中确认回调:增加灵力(由 HurtBox.ReceiveDamage 步骤 7 的 HitConfirmed 事件订阅)。 public void OnHitConfirmed(DamageInfo info) { int gain = _weaponManager?.ActiveWeapon?.soulPowerGain ?? 10; _stats?.AddSoulPower(gain); } } }