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 13:39:34 +00:00
|
|
|
|
2022-01-29 19:30:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
GameObject drawPlatform(GameObject lastObject, GameObject objToSpawn)
|
2022-01-29 13:39:34 +00:00
|
|
|
{
|
2022-01-29 19:30:18 +00:00
|
|
|
MeshFilter meshfilter = lastObject.GetComponent<MeshFilter>();
|
|
|
|
Bounds bounds = meshfilter.mesh.bounds;
|
|
|
|
|
|
|
|
float scale = meshfilter.transform.localScale.x;
|
|
|
|
Bounds b = new Bounds(bounds.center * scale, bounds.size * scale);
|
|
|
|
|
|
|
|
Vector3 nextBlockLocation = new Vector3(lastObject.transform.position.x, lastObject.transform.position.y, lastObject.transform.position.z + b.size.z + 1.0f);
|
|
|
|
|
2022-01-29 19:37:49 +00:00
|
|
|
return Instantiate(objToSpawn, nextBlockLocation, (Quaternion.identity));
|
2022-01-29 17:30:08 +00:00
|
|
|
}
|
2022-01-29 13:39:34 +00:00
|
|
|
|
2022-01-29 19:28:42 +00:00
|
|
|
List<GameObject> spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn)
|
2022-01-29 17:30:08 +00:00
|
|
|
{
|
2022-01-29 17:55:05 +00:00
|
|
|
// configuration:
|
2022-01-29 20:07:02 +00:00
|
|
|
float horizontalDistancePerPlatform = (float)Random.Range(1.0f, levelBlocks.Count); ;
|
2022-01-29 17:55:05 +00:00
|
|
|
|
2022-01-29 17:30:08 +00:00
|
|
|
List<GameObject> levelBlocksSpawnTemp = new List<GameObject>();
|
|
|
|
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
|
|
|
|
|
|
|
MeshFilter meshfilter = lastObject.GetComponent<MeshFilter>();
|
|
|
|
Bounds bounds = meshfilter.mesh.bounds;
|
|
|
|
|
|
|
|
float scale = meshfilter.transform.localScale.x;
|
|
|
|
Bounds b = new Bounds(bounds.center * scale, bounds.size * scale);
|
|
|
|
|
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 18:07:18 +00:00
|
|
|
levelBlocksSpawnTemp.Add(Instantiate(objToSpawn, new Vector3(position.x, position.y + heightOffset, position.z + (float)(i * horizontalDistancePerPlatform)), rotation));
|
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()
|
|
|
|
{
|
|
|
|
lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject;
|
|
|
|
lastBlock = this.gameObject.transform.GetChild(0).gameObject;
|
|
|
|
this.spawnedLevelBlocks.Add(lastBlock);
|
|
|
|
}
|
|
|
|
|
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 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 20:27:01 +00:00
|
|
|
if (playerControlsSript.isFalling == false) ;
|
2022-01-29 15:58:11 +00:00
|
|
|
{
|
2022-01-29 20:27:01 +00:00
|
|
|
if (blockToSpawn > 31 && (blockToSpawn < levelBlocks.Count) && levelBlocks[blockToSpawn].name == lastBlockPrefab.name)
|
2022-01-29 17:57:24 +00:00
|
|
|
{
|
2022-01-29 20:27:01 +00:00
|
|
|
Debug.Log("Same Block");
|
|
|
|
if (blockToSpawn > levelBlocks.Count || blockToSpawn < 0)
|
|
|
|
{
|
|
|
|
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 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 19:30:18 +00:00
|
|
|
instantiatedGameObject = this.drawPlatform(this.lastBlock, this.levelBlocks[blockToSpawn]);
|
2022-01-29 17:57:24 +00:00
|
|
|
this.spawnedLevelBlocks.Add(instantiatedGameObject);
|
2022-01-29 17:30:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-29 19:28:42 +00:00
|
|
|
List<GameObject> instantiatedGameObjectLists = this.spawnSpiralOfPlatforms(lastBlock, levelBlocks[0]);
|
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
|
|
|
|