using UnityEngine;
using BaseGames.Combat;
namespace BaseGames.Player
{
///
/// 玩家战斗组件。
/// 架构 05_PlayerModule §5:HitBox 直接挂在 Player Prefab 子节点上,不经过 WeaponInstance。
/// 节点:[HitBoxGround]、[HitBoxUp]、[HitBoxDown]、[HitBoxAir]。
///
public class PlayerCombat : MonoBehaviour
{
[SerializeField] private WeaponManager _weaponManager;
[Header("HitBox(Player Prefab 子节点)")]
[SerializeField] private HitBox _hitBoxGround;
[SerializeField] private HitBox _hitBoxUp;
[SerializeField] private HitBox _hitBoxDown;
[SerializeField] private HitBox _hitBoxAir;
private PlayerStats _stats;
private void Awake()
{
_stats = GetComponentInParent();
}
private void OnEnable()
{
if (_weaponManager != null)
_weaponManager.OnWeaponChanged += RefreshWeaponData;
}
private void OnDisable()
{
if (_weaponManager != null)
_weaponManager.OnWeaponChanged -= RefreshWeaponData;
}
private void RefreshWeaponData(WeaponSO weapon)
{
// 武器切换时可在此更新 HitBox 默认尺寸等
}
// ── 连击段伤害来源切换 ────────────────────────────────────────────────
///
/// 根据当前连招段切换 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,
};
_hitBoxGround?.SetDamageSource(src);
}
// ── HitBox 激活(由 State / AnimationEvent 调用)─────────────────────
public void EnableWeaponHitBox(AttackDirection dir)
{
var source = _weaponManager?.ActiveWeapon?.GetSourceByDir(dir);
GetHitBox(dir)?.Activate(source, transform);
}
public void DisableWeaponHitBox(AttackDirection dir)
=> GetHitBox(dir)?.Deactivate();
public void DisableAllWeaponHitBoxes()
{
_hitBoxGround?.Deactivate();
_hitBoxUp?.Deactivate();
_hitBoxDown?.Deactivate();
_hitBoxAir?.Deactivate();
}
/// 命中确认回调:增加灵力(由 HurtBox.ReceiveDamage 步骤 7 的 HitConfirmed 事件订阅)。
public void OnHitConfirmed(DamageInfo info)
{
int gain = _weaponManager?.ActiveWeapon?.soulPowerGain ?? 10;
_stats?.AddSoulPower(gain);
}
private HitBox GetHitBox(AttackDirection dir) => dir switch
{
AttackDirection.Ground => _hitBoxGround,
AttackDirection.Up => _hitBoxUp,
AttackDirection.Down => _hitBoxDown,
AttackDirection.Air => _hitBoxAir,
_ => null,
};
}
}