using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class Target : MonoBehaviour
{
    // Start is called before the first frame update

    public float health = 50f;
    public Blood playerBlood;
    public Animator animator;
    public float deathTime;
    private bool dead;

    private void Start()
    {
        dead = false;
        playerBlood = GameObject.Find("Player").GetComponent<Blood>();
        animator = GetComponent<Animator>();
    }
    public void TakeDamage(float amount)
    {
        health -= amount;
        if (health <= 0f && !dead)
        {
            Die();
            dead = true;
        }
    }

    void Die()
    {
        if (playerBlood.gainsblood)
        {
            playerBlood.GainBlood(playerBlood.bloodgain);
        }
        animator.SetTrigger("Death");
        Destroy(GetComponent<EnemyAI>());
        Destroy(GetComponent<NavMeshAgent>());
        StartCoroutine("DestroyGO");
    }

    IEnumerator DestroyGO()
    {
        yield return new WaitForSeconds(deathTime);
        Destroy(gameObject);
    }

}