Files
zeling_v2/Assets/_Game/Scripts/Combat/SkillHitBoxInstance.cs
Joywayer b7baf7ad6a Add WeaponFeedback component and AddressableManagerWindow meta file
- Implemented WeaponFeedback class for handling weapon-related feedbacks such as hit effects and attack sounds.
- Added meta file for AddressableManagerWindow to manage addressable assets.
- Included a new jump.data file for profiler data.
2026-05-22 22:03:32 +08:00

78 lines
2.7 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.Combat
{
/// <summary>
/// 技能 HitBox 实例(架构 09_ProgressionModule §9 SkillHitBoxInstance
/// 挂载于技能 HitBox Prefab 根节点。
/// 命名规范: Assets/Prefabs/Skills/SKL_{skillId}_HitBox.prefab
///
/// Prefab 内部层级示例(近战 AoE 技能):
/// [SKL_SkySlash_HitBox]
/// └── [HitBox] ← 扇形/圆形 PolygonCollider2D
/// └── HitBox.cs
/// </summary>
public class SkillHitBoxInstance : MonoBehaviour
{
[SerializeField] private HitBox[] _hitBoxes; // 技能可有多个 HitBox多段伤害
public event System.Action<DamageInfo> OnHitConfirmed;
private Coroutine _returnCoroutine;
// 按 duration 缓存 WaitForSeconds同一技能复用无 GC 分配
private WaitForSeconds _cachedWait;
private float _cachedWaitDuration = float.NaN;
private void Awake()
{
foreach (var hb in _hitBoxes)
{
if (hb == null) continue;
hb.OnHitConfirmed += info => OnHitConfirmed?.Invoke(info);
}
}
/// <summary>激活所有 HitBox传入伤害数据源和攻击者 Transform。</summary>
public void Activate(DamageSourceSO source, Transform attacker)
{
foreach (var hb in _hitBoxes)
hb?.Activate(source, attacker);
}
/// <summary>
/// duration 秒后归还对象池SetActive false
/// 由 SkillManager 对象池调用;替代旧版 Destroy 流程。
/// </summary>
public void AutoReturnAfter(float duration)
{
if (!Mathf.Approximately(_cachedWaitDuration, duration))
{
_cachedWaitDuration = duration;
_cachedWait = new WaitForSeconds(duration);
}
if (_returnCoroutine != null) StopCoroutine(_returnCoroutine);
_returnCoroutine = StartCoroutine(ReturnCoroutine());
}
private System.Collections.IEnumerator ReturnCoroutine()
{
yield return _cachedWait;
foreach (var hb in _hitBoxes)
hb?.Deactivate();
_returnCoroutine = null;
gameObject.SetActive(false); // 触发对象池回收
}
/// <summary>duration 秒后销毁(非池化路径,保留向后兼容)。</summary>
public void AutoDestroyAfter(float duration)
=> Destroy(gameObject, Mathf.Max(0f, duration));
private void OnDestroy()
{
foreach (var hb in _hitBoxes)
hb?.Deactivate();
}
}
}