Files
zeling_v2/Assets/Scripts/Player/PlayerCombat.cs
2026-05-12 15:34:08 +08:00

99 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using BaseGames.Combat;
namespace BaseGames.Player
{
/// <summary>
/// 玩家战斗组件。
/// 架构 05_PlayerModule §5HitBox 直接挂在 Player Prefab 子节点上,不经过 WeaponInstance。
/// 节点:[HitBoxGround]、[HitBoxUp]、[HitBoxDown]、[HitBoxAir]。
/// </summary>
public class PlayerCombat : MonoBehaviour
{
[SerializeField] private WeaponManager _weaponManager;
[Header("HitBoxPlayer 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<PlayerStats>();
}
private void OnEnable()
{
if (_weaponManager != null)
_weaponManager.OnWeaponChanged += RefreshWeaponData;
}
private void OnDisable()
{
if (_weaponManager != null)
_weaponManager.OnWeaponChanged -= RefreshWeaponData;
}
private void RefreshWeaponData(WeaponSO weapon)
{
// 武器切换时可在此更新 HitBox 默认尺寸等
}
// ── 连击段伤害来源切换 ────────────────────────────────────────────────
/// <summary>
/// 根据当前连招段切换 HitBox 的 DamageSource由 AttackState 在每段开始时调用)。
/// </summary>
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();
}
/// <summary>命中确认回调:增加灵力(由 HurtBox.ReceiveDamage 步骤 7 的 HitConfirmed 事件订阅)。</summary>
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,
};
}
}