using UnityEngine;

public class RocketExplosion : MonoBehaviour
{
    [System.Serializable]
    public class References
    {
        public ParticleSystem Explosion;
    }

    [System.Serializable]
    public class Settings
    {
        public float Radius = 5;
        public float Strength = 1000;
    }

    [SerializeField] private References _references;
    [SerializeField] private Settings _settings;

    //private float radius;
    //private float time;

    void Start()
    {
        Vector3 originPos = transform.position;

        Collider[] colliders = Physics.OverlapSphere(originPos, _settings.Radius);

        foreach (Collider hit in colliders)
        {
            Rigidbody rb = hit.GetComponent<Rigidbody>();

            if (rb != null)
            {
                float strength = _settings.Strength;
                strength *= 1 - Vector3.Distance(originPos, hit.transform.position) / _settings.Radius;

                rb.AddExplosionForce(strength, originPos, _settings.Radius);
            }
        }
    }

    /*void Awake()
    {
        radius = 1;
        _references.Explosion.Play();
    }

    void Update()
    {
        radius += Time.deltaTime * 15;
        _references.Collider.radius = radius <= _settings.Radius ? radius : 0;

        // Destroy after 5 seconds
        time += Time.deltaTime;
        if (time > 5)
            Destroy(gameObject);
    }*/
}
