78 lines
No EOL
2.6 KiB
C#
78 lines
No EOL
2.6 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class MANAGER_TowerGeneraton : MonoBehaviour
|
||
{
|
||
[Header("настройки чанков")]
|
||
[SerializeField] private List<GameObject> chunkPrefabs;
|
||
[SerializeField] private List<GameObject> finalChunkPrefabs;
|
||
[SerializeField] private int chunksBeforeFinish = 10;
|
||
|
||
[Header("связи")]
|
||
[SerializeField] private Transform playerTransform;
|
||
[SerializeField] private float spawnThreshold = 15f;
|
||
[SerializeField] private int initialChunks = 5;
|
||
|
||
private Vector3 _nextSpawnPoint;
|
||
private int _chunksSpawnedCount = 0;
|
||
private bool _isFinishSpawned = false;
|
||
private Queue<GameObject> _activeChunks = new Queue<GameObject>();
|
||
|
||
void Start()
|
||
{
|
||
_nextSpawnPoint = Vector3.zero;
|
||
|
||
for (int i = 0; i < initialChunks; i++)
|
||
{
|
||
SpawnNextChunk();
|
||
}
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (_isFinishSpawned) return;
|
||
|
||
if (playerTransform.position.y > _nextSpawnPoint.y - spawnThreshold)
|
||
{
|
||
SpawnNextChunk();
|
||
}
|
||
|
||
if (_activeChunks.Count > 10)
|
||
{
|
||
Destroy(_activeChunks.Dequeue());
|
||
}
|
||
}
|
||
|
||
private void SpawnNextChunk()
|
||
{
|
||
GameObject prefabToSpawn = (_chunksSpawnedCount >= chunksBeforeFinish)
|
||
? finalChunkPrefabs[Random.Range(0, finalChunkPrefabs.Count)]
|
||
: chunkPrefabs[Random.Range(0, chunkPrefabs.Count)];
|
||
|
||
GameObject newChunk = Instantiate(prefabToSpawn);
|
||
|
||
// Находим точки входа и выхода в новом чанке
|
||
// Важно: в префабах должны быть дочерние объекты с именами "Entrance" и "Exit"
|
||
Transform entrance = newChunk.transform.Find("Entrance");
|
||
Transform exit = newChunk.transform.Find("Exit");
|
||
|
||
if (entrance != null && exit != null)
|
||
{
|
||
// Сдвигаем весь чанк так, чтобы его Entrance совпал с _nextSpawnPoint
|
||
Vector3 offset = newChunk.transform.position - entrance.position;
|
||
newChunk.transform.position = _nextSpawnPoint + offset;
|
||
|
||
// Запоминаем точку выхода этого чанка для следующего
|
||
_nextSpawnPoint = exit.position;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"В префабе {prefabToSpawn.name} не найдены точки Entrance/Exit!");
|
||
}
|
||
|
||
_activeChunks.Enqueue(newChunk);
|
||
_chunksSpawnedCount++;
|
||
|
||
if (_chunksSpawnedCount > chunksBeforeFinish) _isFinishSpawned = true;
|
||
}
|
||
} |