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

public class RacersAI : MonoBehaviour
{
    public GameObject[] checkpoints;
    public int currentCheckpoint;
    public NavMeshAgent NMA;
    public float Laps;
    public float CurrentLap;
    public Vector3 startingplace;
    public AnimationCurve EnginePower;
    public float MotorPower;

    // Start is called before the first frame update
    void Start()
    {
        startingplace = transform.position;
        NMA = GetComponent<NavMeshAgent>();
        NMA.SetDestination(checkpoints[currentCheckpoint].transform.position);
    }

    void Update()
    {
        if (currentCheckpoint == checkpoints.Length)
        {
            currentCheckpoint = 0;
            CurrentLap++;
        }

        if (CurrentLap == Laps)
        {
            gameObject.GetComponent<Racers>().Done = true;
        }

        if(Vector3.Distance(transform.position, checkpoints[currentCheckpoint].transform.position) <= 10)
        {
            currentCheckpoint++;

            if (currentCheckpoint > checkpoints.Length)
            {
                currentCheckpoint = 0;
                CurrentLap++;
            }

            NMA.SetDestination(checkpoints[currentCheckpoint].transform.position);
        }
    }
}