using Core.Utilities;
using UnityEngine;
using UnityEngine.Events;
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;
///
/// 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);
}
///
/// Fires at the end of timer
///
protected virtual void OnTimeEnd()
{
death.Invoke();
Poolable.TryPool(gameObject);
timer.Reset();
}
}
}