多轮审查和修复
This commit is contained in:
55
Assets/Scripts/Combat/ProjectileManager.cs
Normal file
55
Assets/Scripts/Combat/ProjectileManager.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
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)
|
||||
{
|
||||
if (proj == null || config == null) return;
|
||||
proj.Initialize(config, damageInfo, direction);
|
||||
proj.SetTarget(_playerTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user