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

public class Enemy : MonoBehaviour
{
    [SerializeField]
    private EnemyManager _enemyManager;
    [SerializeField]
    private NavMeshAgent _agent;
    [SerializeField]
    private Transform _player;
    [SerializeField]
    private FindPlayer findPlayer;
    [SerializeField]
    private Animator animator;

    private void Start()
    {
        StartCoroutine(customUpdate());
    }

    // Update is called once per frame
    /*void Update()
    {
        if(findPlayer.playerFound)
        {
            print("target");
            _agent.SetDestination(_player.position);
        }else if (!_agent.hasPath)
        {
            _agent.SetDestination(_enemyManager.GetPos());
        } 
    }*/

    IEnumerator customUpdate()
    {
        while (true)
        {
            if(findPlayer.playerFound)
            {
                animator.SetFloat("Blend", 1f);
                _agent.SetDestination(_player.position);
            }else if (!_agent.hasPath)
            {
                animator.SetFloat("Blend", 0f);
                yield return new WaitForSeconds(2f);
                animator.SetFloat("Blend", 1f);
                _agent.SetDestination(_enemyManager.GetPos());
            } 
            yield return null;
        }
    }
}
