
using UnityEngine;

// RandomLaneChangingBehavior.cs
using UnityEngine;

public class RandomLaneChangingBehavior : ICarBehavior
{
    public static readonly RandomLaneChangingBehavior Instance = new RandomLaneChangingBehavior();

    private readonly float _minChangeTime = 2f;
    private readonly float _maxChangeTime = 5f;

    public void UpdateBehavior(TrafficCar car)
    {
        car.transform.Translate(Vector3.forward * car.forwardSpeed * Time.deltaTime);

        Vector3 newPos = car.transform.position;
        newPos.x = Mathf.Lerp(newPos.x, car.TargetX, Time.deltaTime * car.laneChangeSpeed);
        car.transform.position = newPos;
        
        car.laneChangeTimer -= Time.deltaTime;
        if (car.laneChangeTimer <= 0)
        {
            ChangeLane(car);
            car.laneChangeTimer = Random.Range(_minChangeTime, _maxChangeTime);
        }
    }

    private void ChangeLane(TrafficCar car)
    {
        if (car.roadManager != null)
        {
            car.TargetX = car.roadManager.GetRandomSideLaneX(car.TargetX);
        }
    }
}
