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

public class RollingBoulder : MonoBehaviour
{
    Rigidbody rb;
    // Float for time in seconds to wait to destroy the arrow on contact with any other gameobject that is no the player.
    [SerializeField] private float waitToDestroy;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void OnTriggerEnter(Collider other)
    {
        // Checks to make sure other collider is the Player using tag.
        if (other.gameObject.CompareTag("Player"))
        {
            Mover mover =
            gameObject.GetComponent<Mover>();
            mover.enabled = true;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.TryGetComponent(out PlayerDeath playerDeath))
        {
            // Start Respawn coroutine.
            playerDeath.Respawn();
            // Destroy arrow on contact with player.
            Destroy(gameObject);
        }
        else
        {
            // If boulder makes contact with any other gameobject start DestroyBoulder corountine.
            StartCoroutine(DestoryBoulder());
        }
    }

    public IEnumerator DestoryBoulder()
    {
        // set boulder velocity to zero wait for destory time and then destory the boulder.
        rb.velocity = Vector3.zero;
        yield return new WaitForSeconds(waitToDestroy);
        Destroy(gameObject);
    }
}
