using UnityEngine;

namespace Shooter.Gameplay
{
    /// <summary>Бочка: при разрушении (через <see cref="DamageControl"/>) взрывается с короткой задержкой.</summary>
    public class Barrel : MonoBehaviour
    {
        public GameObject m_ExplodeParticle;

        [HideInInspector] public DamageControl m_DamageControl;
        bool exploded = false;

        void Start()
        {
            m_DamageControl = GetComponent<DamageControl>();
        }

        void Update()
        {
            if (!exploded && m_DamageControl.IsDead)
            {
                exploded = true;
                Invoke(nameof(Explode), .2f);
            }
        }

        public void Explode()
        {
            GameObject obj = Instantiate(m_ExplodeParticle);
            obj.transform.position = transform.position;
            Destroy(obj, 6);
            Destroy(gameObject);
        }
    }
}
