using UnityEngine;

namespace ScrambleKit.Weapons.Projectile
{

    public class ExplodeLandingHit : MonoBehaviour
    {
        public Transform ExplosionPrefab;
        public bool ExplodeHittingAnyObject = true;

        private bool destroy = false; // using a flag to prevent double-destroy from multiple events happening

        void LandedHit()
        {
            SpawnExplosion();
        }

        void OnCollisionEnter2D(Collision2D col)
        {
            SpawnExplosion();
            destroy = true;
        }

        void LateUpdate()
        {
            if(destroy)
                Destroy(gameObject);
        }

        private void SpawnExplosion()
        {
            if (ExplosionPrefab == null)
                return;

            Transform explosion = Instantiate(ExplosionPrefab);
            explosion.position = transform.position;
        }
    }

}