
using UnityEngine;

public class TrafficCar : MonoBehaviour
{
    [Header("Движение")]
    public float forwardSpeed = 8f;
    public float laneChangeSpeed = 5f;
    public float despawnDistanceBehindPlayer = 30f;

    [HideInInspector] public GameObject originalPrefab;
    [HideInInspector] public RoadManager roadManager;
    [HideInInspector] public float laneChangeTimer;
    
    public float TargetX { get; set; }
    
    private Transform _playerTransform;
    private ICarBehavior _behavior;
    
    public void Init( Transform _player, RoadManager _roadManager)
    {
        _playerTransform = _player;
        roadManager = _roadManager;
        laneChangeTimer = Random.Range(2f, 5f);
    }

    void Update()
    {
        _behavior?.UpdateBehavior(this);

        if (_playerTransform != null && transform.position.z < _playerTransform.position.z - despawnDistanceBehindPlayer)
        {
            TrafficPool.Instance.ReturnCar(this);
        }
    }

    public void SetBehavior(ICarBehavior newBehavior)
    {
        _behavior = newBehavior;
    }

    public void SetStartPosition(Vector3 position)
    {
        transform.position = position;
        TargetX = position.x;
    }
}
