using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;

public class Mon : MonoBehaviour
{
    Transform endPos;
    NavMeshAgent agent;
    public float Hp;
    public Image hpBar;
    float curHp = 0;
    public GameObject explosion;
    float speed;
    void Start()
    {
        curHp = Hp;
        endPos = GameObject.FindWithTag("endPos").transform;
        agent = GetComponent<NavMeshAgent>();
        speed = agent.speed;
    }

    void Update()
    {
        agent.SetDestination(endPos.position);
        if (Vector3.Distance(transform.position, endPos.position) < 0.5f)
        {
            Destroy(gameObject);
            GameManager.lives--;

        }
    }

    public void GetDam(float dam)
    {
        curHp -= dam;
        hpBar.fillAmount = curHp / Hp;
        if (curHp <= 0)
        {
            Destroy(gameObject);
            GameObject exp = Instantiate(explosion, transform.position, Quaternion.identity);
            Destroy(exp, 1);
        }
    }

    public void getSlow(float fac)
    {
        agent.speed = speed * (1 - fac);
        StartCoroutine(clearSlow());
    }

    IEnumerator clearSlow()
    {
        yield return new WaitForSeconds(3);
        agent.speed = speed;
    }
    private void OnDestroy()
    {
        Spawn.alive--;
    }
}
