using Core.Utilities;
using UnityEngine;
using UnityEngine.Events;
using TowerDefense.Level;
namespace TowerDefense.Towers
{
///
/// A helper component for self destruction
///
public class SelfDestroyTimer : MonoBehaviour
{
///
/// The time before destruction
///
public float time = 5;
///
/// The controlling timer
///
public Timer timer;
///
/// The exposed death callback
///
public UnityEvent death;
///
/// 防止子弹飞到怪物出生点后面
///
private float farthestZ;
private void Start()
{
farthestZ = EndlessLevelManager.instance.StartingNodeList[0].transform.position.z + 3f;
}
///
/// Potentially initialize the time if necessary
///
protected virtual void OnEnable()
{
if (timer == null)
{
timer = new Timer(time, OnTimeEnd);
}
else
{
timer.Reset();
}
}
///
/// Update the timer
///
protected virtual void Update()
{
if (timer == null)
{
return;
}
timer.Tick(Time.deltaTime);
if (gameObject.transform.position.z >= farthestZ)
OnTimeEnd();
}
///
/// Fires at the end of timer
///
protected virtual void OnTimeEnd()
{
death.Invoke();
Poolable.TryPool(gameObject);
timer.Reset();
}
}
}