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

public class TrackFactoryCore : MonoBehaviour
{
    [SerializeField] private LevelConfig LevelConfig;
    private static List<BiomConfig.BiomConfigTemplate> bioms = new List<BiomConfig.BiomConfigTemplate>();
    private static List<BiomConfig.BiomBridgeTemplate> biomBridges = new List<BiomConfig.BiomBridgeTemplate>();
    private static Dictionary<string, int> mapBioms = new Dictionary<string, int>();
    
    private static List<GameObject> trackPrefList = new List<GameObject>();

    private static int biomStatus;
    private static int biomСounter = 7;

    private Transform targetTracker;
    private GameObject player;

    void Start()
    {
        foreach (BiomConfig indexer in LevelConfig.GetLevelConfigs())
        {
            bioms.Add(indexer.GetConfig());
        }
        foreach (BiomConfig indexer in LevelConfig.GetLevelConfigs())
        {
            biomBridges.Add(indexer.GetBridgeTemplate());
        }
        foreach (BiomConfig.BiomConfigTemplate indexer in bioms)
        {
            mapBioms.Add(indexer.biomName, bioms.IndexOf(indexer));
        }

        StartPoint();
    }

    private void Update()
    {
        PlayerTracker();
    }

    public void NextTrack()
    {
        if (biomСounter <= 0)
        {
            SpawnBridge();
        }
        else
        {
            SpawnTrack(bioms[biomStatus].biomPrefList[Random.Range(0, bioms[biomStatus].biomPrefList.Count)]);
        }

        biomСounter -= 1;
    }

    private void StartPoint()
    {
        biomStatus = 0;
        SpawnTrack(bioms[biomStatus].startPoint);

        for (int i = 0; i < 3; i++) 
        {
            SpawnTrack(bioms[biomStatus].biomPrefList[Random.Range(0, bioms[biomStatus].biomPrefList.Count)]);
        }
    }

    private void SpawnTrack(GameObject pref)
    { 
        if (trackPrefList.Count == 0) 
        {
            trackPrefList.Add(Instantiate(pref, transform.position, Quaternion.identity));
        }
        else
        {
            Transform pos = trackPrefList[trackPrefList.Count-1].GetComponent<Transform>();

            trackPrefList.Add(Instantiate(pref, new Vector3(pos.transform.position.x, pos.transform.position.y, pos.transform.position.z + pos.transform.localScale.z / 2) + new Vector3(0, 0, pref.transform.localScale.z) / 2, Quaternion.identity));
        }

        if (trackPrefList.Count >= 6)
        {
            Destroy(trackPrefList[0]);
            trackPrefList.RemoveAt(0);
        }
    }

    private void PlayerTracker()
    {
        if (player == null)
        {
            player = GameObject.FindGameObjectWithTag("Player");

            if (player != null)
            {
                targetTracker = player.GetComponent<Transform>();
            }
        }
        else
        {
            if(Vector3.Distance(targetTracker.position, trackPrefList[trackPrefList.Count - 1].GetComponent<Transform>().position) < 70)
            {
                NextTrack();
            }
        }
    }

    private void SpawnBridge()
    {
        if (biomBridges[biomStatus].bridgeNames.Count > 0)
        {
            int bridgeNum = Random.Range(0, biomBridges[biomStatus].bridgeNames.Count - 1);

            SpawnTrack(biomBridges[biomStatus].bridgePrefs[bridgeNum]);
            biomStatus = mapBioms[biomBridges[biomStatus].bridgeNames[bridgeNum]];

            biomСounter = 7;
        }
    }
}


скажи как можно улучшитьь этот скрипт и оптимизипровать и скажи что в нем сейчас не так