多轮审查和修复
This commit is contained in:
67
Assets/Scripts/Combat/Projectile.cs
Normal file
67
Assets/Scripts/Combat/Projectile.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Pool;
|
||||
|
||||
namespace BaseGames.Combat
|
||||
{
|
||||
/// <summary>
|
||||
/// 抛射物基类。子类通过重写 <see cref="OnInitialized"/> 设定初速度。
|
||||
/// 依赖 <see cref="HitBox"/> 子组件进行碰撞伤害检测。
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody2D), typeof(HitBox))]
|
||||
public abstract class Projectile : MonoBehaviour
|
||||
{
|
||||
[HideInInspector] public DamageInfo DamageInfo;
|
||||
[HideInInspector] public Vector2 Direction;
|
||||
|
||||
protected ProjectileConfigSO _config;
|
||||
protected Rigidbody2D _rb;
|
||||
protected HitBox _hitBox;
|
||||
protected float _aliveTimer;
|
||||
|
||||
private PooledObject _pooledObject;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
_rb = GetComponent<Rigidbody2D>();
|
||||
_hitBox = GetComponent<HitBox>();
|
||||
_pooledObject = GetComponent<PooledObject>();
|
||||
}
|
||||
|
||||
/// <summary>从对象池取出后的初始化入口。</summary>
|
||||
public virtual void Initialize(ProjectileConfigSO config, DamageInfo damageInfo, Vector2 direction)
|
||||
{
|
||||
_config = config;
|
||||
DamageInfo = damageInfo;
|
||||
Direction = direction.normalized;
|
||||
_aliveTimer = 0f;
|
||||
|
||||
_hitBox.Activate(config.DamageSource);
|
||||
OnInitialized();
|
||||
}
|
||||
|
||||
/// <summary>子类在此设定初速度或附加初始化逻辑。</summary>
|
||||
protected virtual void OnInitialized() { }
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
_aliveTimer += Time.deltaTime;
|
||||
if (_config != null && _aliveTimer >= _config.Lifetime)
|
||||
ReturnToPool();
|
||||
}
|
||||
|
||||
/// <summary>停用并归还对象池。</summary>
|
||||
protected void ReturnToPool()
|
||||
{
|
||||
_hitBox.Deactivate();
|
||||
gameObject.SetActive(false);
|
||||
if (_pooledObject != null && _config != null)
|
||||
ServiceLocator.GetOrDefault<IObjectPoolService>()?.Despawn(_config.PoolKey, _pooledObject);
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
_aliveTimer = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user