using Pathfinding;
using UnityEngine;

public class Move : MonoBehaviour
{
    Vector3 lastpos = Vector3.zero;
    public Population parent;
    private Transform target;
    private bool moving = false;
    //Set by Person
    public float Speed
    {
        set
        {
            aiLerp.speed = value;
        }
    }
    private Animator animator;
    bool ObstacleCollision = false;
    Vector2 ObstaclePos;

    private AIDestinationSetter destinationSetter;
    private AILerp aiLerp;

    // Update is called once per frame
    void Start()
    {
        aiLerp = GetComponent<AILerp>();
        destinationSetter = gameObject.AddComponent<AIDestinationSetter>();
        target = GameObject.Instantiate(new GameObject("Target")).transform;
        target.position = transform.position;
        destinationSetter.target = target;
        animator = GetComponent<Animator>();
        Invoke("MoveToRandom", Random.value * parent.MovementChangeRate);
    }

    private Vector3 velocity;

    private void Update()
    {
        //moces if distance 
        if (Vector2.Distance(gameObject.transform.position, lastpos) > 0.01f)
        {
            animator.SetBool("walking", true);
        }
        else
        {
            animator.SetBool("walking", false);
        }

        lastpos = gameObject.transform.position;
    }

    void MoveToRandom()
    {
        moving = true;
        target.position = Utils.GetRandomFreePositionInSquare(parent.Left, parent.Right, parent.Down, parent.Up);
        Invoke("MoveToRandom", Random.value * parent.MovementChangeRate);
      
    }
}
