Files
zeling_v2/Assets/_Game/Scripts/Combat/ProjectileManager.cs

56 lines
1.8 KiB
C#
Raw Permalink 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.Core;
using BaseGames.Core.Events;
namespace BaseGames.Combat
{
/// <summary>
/// 抛射物管理器(单例 MonoBehaviour
/// 缓存玩家 Transform供追踪类抛射物注入目标引用。
/// </summary>
public class ProjectileManager : MonoBehaviour, IProjectileService
{
[SerializeField] private TransformEventChannelSO _onPlayerSpawned;
private Transform _playerTransform;
private readonly CompositeDisposable _subs = new();
/// <summary>当前缓存的玩家 Transform生成追踪弹时使用。</summary>
public Transform PlayerTransform => _playerTransform;
private void Awake()
{
if (ServiceLocator.GetOrDefault<IProjectileService>() != null) { Destroy(gameObject); return; }
ServiceLocator.Register<IProjectileService>(this);
}
private void OnDestroy()
{
ServiceLocator.Unregister<IProjectileService>(this);
}
private void OnEnable()
{
_onPlayerSpawned?.Subscribe(OnPlayerSpawned).AddTo(_subs);
}
private void OnDisable()
{
_subs.Clear();
}
private void OnPlayerSpawned(Transform player) => _playerTransform = player;
/// <summary>
/// 完整初始化一枚 <see cref="HomingProjectile"/> 并注入追踪目标。
/// </summary>
public void LaunchHoming(HomingProjectile proj, Vector2 direction,
ProjectileConfigSO config, DamageInfo damageInfo, int ownerLayer = 0)
{
if (proj == null || config == null) return;
proj.Initialize(config, damageInfo, direction, ownerLayer);
proj.SetTarget(_playerTransform);
}
}
}