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

public class MeteorController : MonoBehaviour
{
    private Collider2D groundCollider;

    public LayerMask ground;

    private void Awake()
    {
        groundCollider = GetComponent<CircleCollider2D>();
    }

    void Update()
    {
        if (isGrounded()) 
        {
            StartCoroutine(waitForDestroy());
        }
    }

    private bool isGrounded()
    {
        Vector2 topLeftPoint = transform.position;
        topLeftPoint.x -= groundCollider.bounds.extents.x / 2;
        topLeftPoint.y += groundCollider.bounds.extents.y;

        Vector2 bottomRightPoint = transform.position;
        bottomRightPoint.x += groundCollider.bounds.extents.x / 2;
        bottomRightPoint.y -= groundCollider.bounds.extents.y + 0.3f;

        return Physics2D.OverlapArea(topLeftPoint, bottomRightPoint, ground);
    }

    private IEnumerator waitForDestroy() 
    {
        yield return new WaitForSeconds(0.3f);
        Destroy(gameObject);
    }
}
