Files
zeling_v2/Assets/_Game/Scripts/Player/WeaponHitBoxInstance.cs
Joywayer 09bdda970a feat(feedback): 命中/受击反馈支持固定位置与击中位置两种生成模式
DamageInfo 新增运行时字段 HitPoint:HitBox 结算时写入精确接触点
(ClosestPoint),HurtBox.ReceiveDamage 以解析后命中点兜底盖戳,
陷阱/环境等直调路径同样可用。IFeedbackPlayer.PlayHit/PlayTakeHit
增加命中点参数,全链路(武器实例→PlayerCombat→HurtState→
EnemyFeedback)透传。各 Feedback 组件命中/受击槽位新增
FeedbackPositionMode(Fixed=反馈链编排的固定位置 /
HitPoint=传入命中点,启用位置选项的模块在该点生成),由编排
反馈链的开发者在 Inspector 按表现选择;武器命中默认 HitPoint
(火花贴点),角色级默认 Fixed(震屏/振动与位置无关)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 11:48:37 +08:00

121 lines
5.1 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;
using BaseGames.Feedback;
namespace BaseGames.Player
{
/// <summary>
/// 武器 HitBox 实例。
/// 由 WeaponManager 在切换武器时实例化到角色的 [WeaponSocket] 子节点下。
///
/// Prefab 层级示例(每段连击独立一个子节点,在 Prefab 内可视化编辑 Collider2D
/// [WPN_SkyBlade_HitBox] ← WeaponHitBoxInstance
/// ├── [HitBox_Ground] Id="" ← 方向默认ComboStepConfig.hitBoxId 为空时使用)
/// ├── [HitBox_Ground_1] Id="g1" ← 第 1 段专属判定
/// ├── [HitBox_Ground_2] Id="g2" ← 第 2 段专属判定
/// ├── [HitBox_Up] Id="" ← 上劈默认
/// ├── [HitBox_Down] Id="" ← 下劈默认
/// └── [HitBox_Air] Id="" ← 空中默认
///
/// ComboStepConfig.hitBoxId 留空 → 用方向默认;填 Id → 精确激活对应子节点。
/// </summary>
public class WeaponHitBoxInstance : MonoBehaviour
{
[Header("方向默认 HitBoxhitBoxId 为空时使用)")]
[SerializeField] private HitBox _hitBoxGround;
[SerializeField] private HitBox _hitBoxUp;
[SerializeField] private HitBox _hitBoxDown;
[SerializeField] private HitBox _hitBoxAir;
private HitBox[] _allHitBoxes;
private AttackDirection _activeDir;
private IFeedbackPlayer _feedback;
/// <summary>下劈命中确认事件(供 DownAttackState Pogo 逻辑)。命中 HurtBox 或可破坏物均触发。</summary>
public event System.Action<DamageInfo> OnDownHitConfirmed;
/// <summary>任意 HitBox 命中确认事件(供 PlayerCombat 订阅通用命中反馈)。</summary>
public event System.Action<DamageInfo> OnHitConfirmed;
private void Awake()
{
_allHitBoxes = GetComponentsInChildren<HitBox>(true);
foreach (var hb in _allHitBoxes)
{
hb.OnHitConfirmed += OnAnyHitConfirmed;
hb.OnBreakableHitConfirmed += OnAnyBreakableHitConfirmed;
}
_feedback = GetComponentInChildren<IFeedbackPlayer>() ?? NullFeedbackPlayer.Instance;
}
private void OnAnyHitConfirmed(DamageInfo info)
{
var weight = info.FinalDamage <= 5 ? HitWeight.Light
: info.FinalDamage <= 15 ? HitWeight.Medium
: HitWeight.Heavy;
_feedback.PlayHit(weight, info.HitPoint);
OnHitConfirmed?.Invoke(info);
if (_activeDir == AttackDirection.Down)
OnDownHitConfirmed?.Invoke(info);
}
/// <summary>
/// 命中可破坏物:播放轻量打击反馈;下劈方向时同样触发弹跳。
/// 不转发 OnHitConfirmed可破坏物不参与灵力获取
/// </summary>
private void OnAnyBreakableHitConfirmed(DamageInfo info)
{
_feedback.PlayHit(HitWeight.Light, info.HitPoint);
if (_activeDir == AttackDirection.Down)
OnDownHitConfirmed?.Invoke(info);
}
// ── 公共 API ──────────────────────────────────────────────────────────
/// <summary>
/// 激活 HitBox。
/// hitBoxId 非空时按 Id 精确查找 Prefab 子节点;空 = 使用该方向的默认 HitBox。
/// </summary>
public void Activate(AttackDirection dir, DamageSourceSO source, Transform attacker,
string hitBoxId = "")
{
_activeDir = dir;
_feedback.PlayAttackWhoosh();
var hitBox = string.IsNullOrEmpty(hitBoxId)
? GetHitBox(dir)
: (GetHitBoxById(hitBoxId) ?? GetHitBox(dir));
hitBox?.Activate(source, attacker);
}
/// <summary>停用指定方向的默认 HitBox。</summary>
public void Deactivate(AttackDirection dir) => GetHitBox(dir)?.Deactivate();
/// <summary>停用 Prefab 中所有 HitBox。</summary>
public void DeactivateAll()
{
if (_allHitBoxes == null) return;
foreach (var hb in _allHitBoxes) hb.Deactivate();
}
/// <summary>按 HitBox.Id 查找子节点(未找到返回 null。</summary>
public HitBox GetHitBoxById(string id)
{
if (_allHitBoxes == null || string.IsNullOrEmpty(id)) return null;
foreach (var hb in _allHitBoxes)
if (hb.Id == id) return hb;
return null;
}
/// <summary>按攻击方向返回默认 HitBox。</summary>
public HitBox GetHitBox(AttackDirection dir) => dir switch
{
AttackDirection.Ground => _hitBoxGround,
AttackDirection.Up => _hitBoxUp,
AttackDirection.Down => _hitBoxDown,
AttackDirection.Air => _hitBoxAir,
_ => null,
};
}
}