using UnityEngine;
using BaseGames.Core;
using BaseGames.Core.Events;
using BaseGames.Combat;
namespace BaseGames.VFX
{
///
/// 全局命中特效派发器:订阅 HitConfirmedEventChannel,
/// 根据 DamageInfo.FxType 从 VFXCatalog 查找并播放特效。
/// 放置在 Persistent 场景的 [Systems] GameObject 上。
///
public class HitFXSpawner : MonoBehaviour
{
[SerializeField] private HitConfirmedEventChannelSO _onHitConfirmed;
[SerializeField] private VFXCatalogSO _catalog;
private readonly CompositeDisposable _subs = new();
private void Awake()
{
Debug.Assert(_catalog != null, "[HitFXSpawner] _catalog 未赋值,请在 Inspector 中指定 VFXCatalogSO。", this);
_catalog.Initialize();
}
private void OnEnable()
{
_onHitConfirmed?.Subscribe(HandleHit).AddTo(_subs);
}
private void OnDisable()
{
_subs.Clear();
}
private void HandleHit(HitInfo info)
{
var pool = ServiceLocator.GetOrDefault();
if (pool == null) return;
if (_catalog.TryGetHitFX(info.DamageInfo.FxType, out var vfxRef))
pool.Play(vfxRef, info.HitPoint);
}
}
}