forked from vladislove/pgiz4
109 lines
3.7 KiB
C#
109 lines
3.7 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class LevelGenerator : MonoBehaviour
|
|
{
|
|
public GameObject platformPrefab;
|
|
public Transform playerTransform;
|
|
public int numberOfPlatforms = 5;
|
|
public float platformLength = 50f;
|
|
public float laneWidth = 4f;
|
|
|
|
[Header("Spawning Objects")]
|
|
public GameObject coinPrefab;
|
|
public GameObject lowObstaclePrefab;
|
|
public GameObject highObstaclePrefab;
|
|
public float highObstacleYOffset = 1.5f; // Дополнительная высота для высоких препятствий
|
|
|
|
private Dictionary<GameObject, List<GameObject>> platformItems = new Dictionary<GameObject, List<GameObject>>();
|
|
private List<GameObject> activePlatforms = new List<GameObject>();
|
|
private float spawnPosition = 0f;
|
|
|
|
void Start()
|
|
{
|
|
for (int i = 0; i < numberOfPlatforms; i++) SpawnPlatform();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (playerTransform.position.z - platformLength > spawnPosition - (numberOfPlatforms * platformLength))
|
|
{
|
|
SpawnPlatform();
|
|
DeleteOldestPlatform();
|
|
}
|
|
}
|
|
|
|
private void SpawnPlatform()
|
|
{
|
|
GameObject newPlatform = Instantiate(platformPrefab, Vector3.forward * spawnPosition, Quaternion.Euler(0, 90, 0));
|
|
activePlatforms.Add(newPlatform);
|
|
platformItems[newPlatform] = new List<GameObject>();
|
|
PopulatePlatformByLanes(newPlatform);
|
|
spawnPosition += platformLength;
|
|
}
|
|
|
|
private void PopulatePlatformByLanes(GameObject platform)
|
|
{
|
|
int segments = 5;
|
|
float segmentLength = platformLength / segments;
|
|
List<Vector3> spawnPoints = new List<Vector3>();
|
|
|
|
for (int i = 0; i < segments; i++)
|
|
{
|
|
for (int j = -1; j <= 1; j++)
|
|
{
|
|
float zPos = platform.transform.position.z - (platformLength / 2) + (i * segmentLength) + (segmentLength / 2);
|
|
float xPos = j * laneWidth;
|
|
spawnPoints.Add(new Vector3(xPos, 0, zPos)); // Y будет настраиваться ниже
|
|
}
|
|
}
|
|
|
|
System.Random rng = new System.Random();
|
|
spawnPoints = spawnPoints.OrderBy(a => rng.Next()).ToList();
|
|
|
|
int itemsToSpawn = Random.Range(5, 10);
|
|
|
|
for (int i = 0; i < itemsToSpawn && i < spawnPoints.Count; i++)
|
|
{
|
|
GameObject itemToSpawn = null;
|
|
Vector3 spawnPos = spawnPoints[i];
|
|
|
|
int itemType = Random.Range(0, 5);
|
|
switch (itemType)
|
|
{
|
|
case 0: case 1: case 2:
|
|
itemToSpawn = coinPrefab;
|
|
spawnPos.y = 1f;
|
|
break;
|
|
case 3:
|
|
itemToSpawn = lowObstaclePrefab;
|
|
spawnPos.y = 1f;
|
|
break;
|
|
case 4:
|
|
itemToSpawn = highObstaclePrefab;
|
|
spawnPos.y = highObstacleYOffset; // Используем новую переменную
|
|
break;
|
|
}
|
|
|
|
if (itemToSpawn != null)
|
|
{
|
|
GameObject newItem = Instantiate(itemToSpawn, spawnPos, Quaternion.identity);
|
|
platformItems[platform].Add(newItem);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DeleteOldestPlatform()
|
|
{
|
|
GameObject platformToDelete = activePlatforms[0];
|
|
if (platformItems.ContainsKey(platformToDelete))
|
|
{
|
|
foreach (GameObject item in platformItems[platformToDelete]) Destroy(item);
|
|
platformItems.Remove(platformToDelete);
|
|
}
|
|
Destroy(platformToDelete);
|
|
activePlatforms.RemoveAt(0);
|
|
}
|
|
}
|