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

public class EaterController : MonoBehaviour
{
    public IdleState IdleState;
    public RunningState RunningState;
    public ExaustedState ExaustedState;
    public AttackState AttackState;

    public Transform Eater;
    //public Rigidbody CatBoyRigidBody;
    //public Collider GroundCollider;

    public float DetectRange;

    public float WaitTime;
    private float waitTimer;
    public float WanderRange;

    public Transform CatBoy;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        
        if(AttackState.enabled)
            return;
        if(ExaustedState.enabled)
            return;

        if(Vector3.Distance(Eater.position, CatBoy.position) < DetectRange)
        {
             //be angry

            if(IdleState.enabled)
                IdleState.enabled = false;
            if(RunningState.enabled)
                RunningState.enabled = false;

            AttackState.Duration = 3;
            AttackState.Direction = (CatBoy.position - Eater.position).normalized;
            AttackState.enabled = true;
        }
        else
        {
            if(IdleState.enabled)
            {
                waitTimer += Time.deltaTime;
                if(waitTimer > WaitTime)
                {
                    //get new Wander target
                    IdleState.enabled = false;
                    Vector2 RandomPoint = Random.insideUnitCircle;
                    RunningState.Target = new Vector3(RandomPoint.x, 0, RandomPoint.y) * WanderRange;
                    RunningState.enabled = true;
                }
                    
            }
            else
            {
                if(Vector3.Distance(RunningState.Target, Eater.position) < 1)
                {             
                    RunningState.enabled = false;
                    IdleState.enabled = true;
                    waitTimer = 0;
                }
            }

            
        }


    }
}
