2022-01-29 13:39:34 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class ProceduralGeneration : MonoBehaviour
|
|
|
|
{
|
|
|
|
public List<GameObject> levelBlocks = new List<GameObject>();
|
2022-01-29 17:57:24 +00:00
|
|
|
public List<GameObject> spawnedLevelBlocks = new List<GameObject>();
|
2022-01-29 15:58:11 +00:00
|
|
|
public GameObject player = null;
|
2022-01-29 17:57:24 +00:00
|
|
|
public GameObject lastBlock;
|
2022-01-29 15:58:11 +00:00
|
|
|
private GameObject lastBlockPrefab;
|
2022-01-29 17:30:08 +00:00
|
|
|
private int spavnetobjectIndex = 0;
|
2022-01-29 19:30:18 +00:00
|
|
|
private int maximumNumberOfPlatformsAtScene = 100;
|
|
|
|
private float maximumDistanceOfPlatformFromPlayer = 20.0f;
|
2022-01-29 21:04:46 +00:00
|
|
|
private GameObject levelParrent = null;
|
2022-01-29 13:39:34 +00:00
|
|
|
|
2022-01-29 19:30:18 +00:00
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
Bounds getPrefabBounds(GameObject go)
|
2022-01-29 13:39:34 +00:00
|
|
|
{
|
2022-01-29 21:04:46 +00:00
|
|
|
Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
|
2022-01-29 19:30:18 +00:00
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
if (renderers.Length > 0)
|
|
|
|
{
|
|
|
|
Bounds bounds = renderers[0].bounds;
|
|
|
|
for (int i = 1, ni = renderers.Length; i < ni; i++)
|
|
|
|
{
|
|
|
|
bounds.Encapsulate(renderers[i].bounds);
|
|
|
|
}
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new Bounds();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GameObject drawPlatform(GameObject lastObject, GameObject objToSpawn, GameObject parentLevelObject)
|
|
|
|
{
|
|
|
|
Bounds bounds = this.getPrefabBounds(lastObject);
|
|
|
|
Bounds b = new Bounds(bounds.center, bounds.size);
|
2022-01-29 19:30:18 +00:00
|
|
|
|
|
|
|
Vector3 nextBlockLocation = new Vector3(lastObject.transform.position.x, lastObject.transform.position.y, lastObject.transform.position.z + b.size.z + 1.0f);
|
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
GameObject newObject = Instantiate(objToSpawn, nextBlockLocation, (Quaternion.identity));
|
|
|
|
newObject.transform.parent = parentLevelObject.transform;
|
|
|
|
return newObject;
|
2022-01-29 17:30:08 +00:00
|
|
|
}
|
2022-01-29 13:39:34 +00:00
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
List<GameObject> spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn, GameObject parentLevelObject)
|
2022-01-29 17:30:08 +00:00
|
|
|
{
|
2022-01-29 17:55:05 +00:00
|
|
|
// configuration:
|
2022-01-29 21:04:46 +00:00
|
|
|
float horizontalDistancePerPlatform = (float)Random.Range(0.5f, 2.0f); ;
|
2022-01-29 17:55:05 +00:00
|
|
|
|
2022-01-29 17:30:08 +00:00
|
|
|
List<GameObject> levelBlocksSpawnTemp = new List<GameObject>();
|
2022-01-29 20:46:46 +00:00
|
|
|
// Debug.Log("Building LOOP");
|
2022-01-29 15:58:11 +00:00
|
|
|
|
|
|
|
int pieceCount = 10;
|
2022-01-29 17:30:08 +00:00
|
|
|
float radius = (pieceCount / 2) * 2;
|
2022-01-29 15:58:11 +00:00
|
|
|
float angle = 360f / (float)pieceCount;
|
2022-01-29 19:15:14 +00:00
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
Bounds bounds = this.getPrefabBounds(lastObject);
|
|
|
|
Bounds b = new Bounds(bounds.center, bounds.size);
|
2022-01-29 19:15:14 +00:00
|
|
|
|
2022-01-29 20:07:02 +00:00
|
|
|
Vector3 centerPoint = new Vector3(lastObject.transform.position.x, (lastObject.transform.position.y + radius), lastObject.transform.position.z + b.size.z + 1.0f);
|
2022-01-29 15:58:11 +00:00
|
|
|
|
2022-01-29 18:13:01 +00:00
|
|
|
float heightOffset = radius;
|
|
|
|
|
2022-01-29 20:07:02 +00:00
|
|
|
for (int i = 1; i < pieceCount + 1; i++)
|
2022-01-29 13:39:34 +00:00
|
|
|
{
|
2022-01-29 20:07:02 +00:00
|
|
|
Quaternion rotation = (Quaternion.AngleAxis(i * angle, Vector3.back));
|
2022-01-29 15:58:11 +00:00
|
|
|
Vector3 direction = rotation * Vector3.down;
|
2022-01-29 17:30:08 +00:00
|
|
|
Vector3 position = (lastObject.transform.position + (direction * radius));
|
2022-01-29 13:39:34 +00:00
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
GameObject newObject = Instantiate(objToSpawn, new Vector3(position.x, position.y + heightOffset, position.z + (float)(i * horizontalDistancePerPlatform)), rotation);
|
|
|
|
newObject.transform.parent = parentLevelObject.transform;
|
|
|
|
levelBlocksSpawnTemp.Add(newObject);
|
2022-01-29 13:39:34 +00:00
|
|
|
}
|
2022-01-29 15:58:11 +00:00
|
|
|
|
2022-01-29 17:30:08 +00:00
|
|
|
return levelBlocksSpawnTemp;
|
|
|
|
}
|
2022-01-29 15:58:11 +00:00
|
|
|
|
2022-01-29 19:30:18 +00:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2022-01-29 21:04:46 +00:00
|
|
|
levelParrent = this.gameObject;
|
2022-01-29 19:30:18 +00:00
|
|
|
lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject;
|
|
|
|
lastBlock = this.gameObject.transform.GetChild(0).gameObject;
|
|
|
|
this.spawnedLevelBlocks.Add(lastBlock);
|
2022-01-29 21:04:46 +00:00
|
|
|
|
|
|
|
|
2022-01-29 19:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 13:39:34 +00:00
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2022-01-29 15:58:11 +00:00
|
|
|
Vector3 playerPosition = this.player.transform.position;
|
2022-01-29 20:27:01 +00:00
|
|
|
PlayerController playerControlsSript = this.player.GetComponent<PlayerController>();
|
2022-01-29 17:57:24 +00:00
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
if (playerControlsSript.isFalling)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:08:56 +00:00
|
|
|
for (var i = 0; i < this.spawnedLevelBlocks.Count; i++)
|
2022-01-29 15:58:11 +00:00
|
|
|
{
|
2022-01-29 19:08:56 +00:00
|
|
|
float distance = Vector3.Distance(this.spawnedLevelBlocks[i].transform.position, playerPosition);
|
2022-01-29 19:30:18 +00:00
|
|
|
if (distance > this.maximumDistanceOfPlatformFromPlayer && this.spawnedLevelBlocks.Count >= this.maximumNumberOfPlatformsAtScene)
|
2022-01-29 19:08:56 +00:00
|
|
|
{
|
|
|
|
Destroy(this.spawnedLevelBlocks[i]);
|
|
|
|
this.spawnedLevelBlocks.Remove(this.spawnedLevelBlocks[i]);
|
|
|
|
spavnetobjectIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2022-01-29 15:58:11 +00:00
|
|
|
}
|
2022-01-29 13:39:34 +00:00
|
|
|
|
2022-01-29 19:30:18 +00:00
|
|
|
if (this.spawnedLevelBlocks.Count <= this.maximumNumberOfPlatformsAtScene)
|
2022-01-29 15:58:11 +00:00
|
|
|
{
|
2022-01-29 17:30:08 +00:00
|
|
|
GameObject instantiatedGameObject;
|
|
|
|
GameObject blockObjToSpawn;
|
2022-01-29 15:58:11 +00:00
|
|
|
|
2022-01-29 20:07:02 +00:00
|
|
|
int blockToSpawn = Random.Range(0, (levelBlocks.Count + 1));
|
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
|
|
|
|
if (blockToSpawn > 31 && (blockToSpawn < levelBlocks.Count) && levelBlocks[blockToSpawn].name == lastBlockPrefab.name)
|
2022-01-29 15:58:11 +00:00
|
|
|
{
|
2022-01-29 21:04:46 +00:00
|
|
|
Debug.Log("Same Block");
|
|
|
|
if (blockToSpawn > levelBlocks.Count || blockToSpawn < 0)
|
2022-01-29 17:57:24 +00:00
|
|
|
{
|
2022-01-29 21:04:46 +00:00
|
|
|
blockToSpawn = Random.Range(0, levelBlocks.Count);
|
2022-01-29 17:57:24 +00:00
|
|
|
}
|
2022-01-29 17:30:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 21:04:46 +00:00
|
|
|
|
2022-01-29 20:07:02 +00:00
|
|
|
if (blockToSpawn > -1 && (blockToSpawn < levelBlocks.Count))
|
2022-01-29 17:30:08 +00:00
|
|
|
{
|
2022-01-29 20:07:02 +00:00
|
|
|
blockObjToSpawn = levelBlocks[blockToSpawn];
|
2022-01-29 21:04:46 +00:00
|
|
|
instantiatedGameObject = this.drawPlatform(this.lastBlock, this.levelBlocks[blockToSpawn], this.levelParrent);
|
2022-01-29 17:57:24 +00:00
|
|
|
this.spawnedLevelBlocks.Add(instantiatedGameObject);
|
2022-01-29 17:30:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-29 21:04:46 +00:00
|
|
|
List<GameObject> instantiatedGameObjectLists = this.spawnSpiralOfPlatforms(lastBlock, levelBlocks[0], this.levelParrent);
|
2022-01-29 17:30:08 +00:00
|
|
|
foreach (var spavnedBlock in instantiatedGameObjectLists)
|
|
|
|
{
|
|
|
|
this.spawnedLevelBlocks.Add(spavnedBlock);
|
|
|
|
}
|
|
|
|
instantiatedGameObject = this.spawnedLevelBlocks[this.spawnedLevelBlocks.Count - 1];
|
|
|
|
blockObjToSpawn = levelBlocks[0];
|
2022-01-29 15:58:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:08:56 +00:00
|
|
|
this.lastBlock = instantiatedGameObject;
|
|
|
|
this.lastBlockPrefab = blockObjToSpawn;
|
2022-01-29 15:58:11 +00:00
|
|
|
|
|
|
|
}
|
2022-01-29 13:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-29 17:57:24 +00:00
|
|
|
|