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

public abstract class Enemy : Entity
{
    public float TargetSearchRadius = 10.0f;
    public float TargetSearchInterval = 1.0f;
    public LayerMask TargetLayermask;

    private Entity target;
    public Entity Target
    {
        get
        {
            return target;
        }
    }

    public override void Start()
    {
        base.Start();
        StartCoroutine(searchTargetCoroutine());
    }

    private IEnumerator searchTargetCoroutine()
    {
        while (true)
        {
            yield return new WaitForSeconds(TargetSearchInterval);
            this.target = SearchTarget();
        }
    }

    public virtual bool TargetFilter(Entity e)
    {
        return e is Player; 
    }

    public virtual Entity SearchTarget()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(this.transform.position, TargetSearchRadius, TargetLayermask);
        foreach (Collider2D collider in colliders)
        {
            Entity e = collider.gameObject.GetComponent<Entity>();
            if (e != null && TargetFilter(e))
            {
                return e;
            }
        }

        return null;
    }

}
