using System.Linq; using UnityEngine; using System.Collections.Generic; using System.Collections; public class WorldSpawnManager : MonoBehaviour, IBase, IBootLoader, IDataLoader { [SerializeField] private Transform planesParent; [SerializeField] private GameObject testPrefab; [SerializeField] private int testPrefabCount; [SerializeField] private GameObject prefab; [SerializeField] private float blockOffsetZ = 5; [SerializeField] private float environmentMoveSpeed; [SerializeField] private float queueEnqueueDelay = 1f; private float lastZOffset; private Queue environmentBlocksQueue = new Queue(); private Transform passedEnvironmentBlock = null; private EnvironmentBlock passedEnvironmentBlockComp = null; private EnvironmentBlock newlyEncounteredEnvironmentBlock = null; private EnvironmentBlock firstEnvironmentBlock = null; private PlayerCarController playerCarController = null; public float EnvironmentMoveSpeed => environmentMoveSpeed; public void Initialize() { InterfaceManager.Instance?.RegisterInterface(this); } public void InitializeData() { playerCarController = InterfaceManager.Instance?.GetInterfaceInstance(); } public void SetEnvironmentMoveSpeed(float newSpeedVal) { Debug.Log($"newSpeedVal: {newSpeedVal}"); environmentMoveSpeed = newSpeedVal; } public EnvironmentBlock GetFirstEnvironmentBlock() { return firstEnvironmentBlock; } public EnvironmentBlock GetNewlyEncounteredEnvironmentBlock() { return newlyEncounteredEnvironmentBlock; } public void ResetNewlyEncounteredEnvironmentBlock() { newlyEncounteredEnvironmentBlock = null; } [ContextMenu("Create Test Blocks")] public void CreateBlocks() { float zOffsetSum = 0; for (int i=0; i(); block.Init(i + 1); if (firstEnvironmentBlock == null) firstEnvironmentBlock = block; zOffsetSum += blockOffsetZ; } } public void SetEnvironmentBlocks(Transform newBlock) { passedEnvironmentBlock = newBlock; if (passedEnvironmentBlock != null) { SendBlockTowardsEnd(); InitNewEnvironmentBlock(); } } public float GetResultBasedOnDifficultyProgressiveFormula(float startVal, float endVal, float fraction) { return startVal + (endVal - startVal) * fraction; } private void SendBlockTowardsEnd() { passedEnvironmentBlockComp = environmentBlocksQueue.Dequeue(); Invoke(nameof(UpdatePositionForDequeuedElement), queueEnqueueDelay); } private void UpdatePositionForDequeuedElement() { GameManager gameManager = InterfaceManager.Instance?.GetInterfaceInstance(); Debug.Log($"SendBlockTowardsEnd :: passedEnvironmentBlockComp: {passedEnvironmentBlockComp.name}"); if (!gameManager.IsGameInProgress) { gameManager.OnGameBackInProgress = () => Invoke(nameof(UpdatePositionForDequeuedElement), queueEnqueueDelay); return; } lastZOffset = environmentBlocksQueue.Last().transform.position.z + blockOffsetZ; passedEnvironmentBlockComp.transform.position = new Vector3(passedEnvironmentBlockComp.transform.position.x, passedEnvironmentBlockComp.transform.position.y, lastZOffset); environmentBlocksQueue.Enqueue(passedEnvironmentBlockComp); } private void InitNewEnvironmentBlock() { newlyEncounteredEnvironmentBlock = environmentBlocksQueue.Peek(); Debug.Log($":: newlyEncounteredEnvironmentBlock: {newlyEncounteredEnvironmentBlock.name}"); } private void Awake() { CreateBlocks(); foreach (Transform transformObj in planesParent) { environmentBlocksQueue.Enqueue(transformObj.GetComponent()); } } }