
using System.Collections;
using UnityEngine;

public class BonusSpawner : MonoBehaviour
{
    [Header("Настройки спавна")]
    public GameObject[] bonusPrefabs;
    [Tooltip("Минимальное и максимальное время между спавнами бонусов")]
    public Vector2 spawnInterval = new Vector2(10f, 20f);
    [Tooltip("Как далеко впереди игрока спавнить бонус")]
    public float spawnDistance = 100f;

    private Transform _playerTransform;

    void Start()
    {
        StartCoroutine(SpawnBonusRoutine());
    }

    IEnumerator SpawnBonusRoutine()
    {
        while (true)
        {
            float waitTime = Random.Range(spawnInterval.x, spawnInterval.y);
            yield return new WaitForSeconds(waitTime);
            
            if (bonusPrefabs.Length == 0 || RoadManager.Instance == null || RoadManager.Instance.playerTransform == null)
            {
                continue; 
            }
            
            float spawnX = RoadManager.Instance.GetRandomLaneX();
            float spawnZ = RoadManager.Instance.playerTransform.position.z + spawnDistance;
            Vector3 spawnPosition = new Vector3(spawnX, 0.5f, spawnZ);
            
            GameObject bonusPrefab = bonusPrefabs[Random.Range(0, bonusPrefabs.Length)];
            
            GameObject bonus = SimpleObjectPool.Instance.Get(bonusPrefab, spawnPosition, Quaternion.identity);
            
            BonusPickup pickup = bonus.GetComponent<BonusPickup>();
            if (pickup != null)
            {
                pickup.Init(bonusPrefab);
            }
        }
    }
}

