using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimedExplosion : MonoBehaviour
{
    public GameObject Explosion;
    public float LifeTIme = 2f;

    private Transform _target;

    private float _boomTime;

    // Use this for initialization
    public void Init(float time, Transform target)
    {
        _boomTime = Time.timeSinceLevelLoad + time;
        _target = target;
        StartCoroutine(BoomRoutine());
    }

    private IEnumerator BoomRoutine()
    {
        while (Time.timeSinceLevelLoad < _boomTime)
        {
            yield return null;
        }
        //Boom time
        Instantiate(Explosion, _target);

        yield return StartCoroutine(ByeBye());
    }

    private IEnumerator ByeBye()
    {
        yield return new WaitForSeconds(LifeTIme);

        Instantiate(Explosion, _target.transform.position, Quaternion.identity);
        Destroy(_target.gameObject);
        
    }
}