using UnityEngine;

public class NPCMove : MonoBehaviour
{
    [Header("settings")]
    public float speed;
    public float idleTime;
    public Vector2 pointA;
    public Vector2 pointB;

    Vector2 currentTarget;
    float distanceToA;
    float distanceToB;
    float lastStopped;
    

    private void Start()
    {
        currentTarget = pointA;
    }
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    private void FixedUpdate()
    {
        distanceToA = Mathf.Abs(transform.position.x - pointA.x);
        distanceToB = Mathf.Abs(transform.position.x - pointB.x);

        if(lastStopped + idleTime < Time.time)
        {
            if (distanceToA == 0f)
            {
                currentTarget = pointB;
                lastStopped = Time.time;
            }
            if (distanceToB == 0f)
            {
                currentTarget = pointA;
                lastStopped = Time.time;
            }

            transform.position = Vector3.MoveTowards(transform.position, new Vector3(currentTarget.x, transform.position.y, 0), speed);
        }
    }
}
