using UnityEngine;
using UnityEngine.UI;

public class MeleeEnemy : Enemy
{
    public float attackRange = 2f;
    public float attackCooldown = 1.5f;
    private float lastAttackTime;
    private float distance;
    public int damage = 5;
    public PlayerEvents playerEvents;

    public override void Start(){
        base.Start();
        playerEvents = GameObject.Find("Player").GetComponent<PlayerEvents>();
    }

    public override void TargetPlayer()
    {
        distance = Vector3.Distance(transform.position, player.position);

        if (distance > attackRange)
        {
            agent.isStopped = false;
            agent.SetDestination(player.position);
        }
        else
        {
            agent.isStopped = true;

            if (Time.time - lastAttackTime >= attackCooldown)
            {
                Attack();
                lastAttackTime = Time.time;
            }
        }
    }

    public void Attack()
    {
        if(distance <= attackRange){
            playerEvents.ReceiveDamage(damage);
        }
    }
}
