GGJ2022/Assets/Scripts/ProceduralGeneration.cs

195 lines
7.8 KiB
C#
Raw Normal View History

2022-01-29 13:39:34 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProceduralGeneration : MonoBehaviour
{
2022-01-30 10:22:18 +00:00
[Header("Level")]
2022-01-29 13:39:34 +00:00
public List<GameObject> levelBlocks = new List<GameObject>();
2022-01-30 10:22:18 +00:00
private int maximumNumberOfPlatformsAtScene = 100;
2022-01-29 17:57:24 +00:00
public List<GameObject> spawnedLevelBlocks = new List<GameObject>();
2022-01-30 10:22:18 +00:00
private float maximumDistanceOfPlatformFromPlayer = 20.0f;
2022-01-30 10:43:14 +00:00
private GameObject levelParrent = null;
2022-01-30 10:22:18 +00:00
[Header("Background")]
public List<GameObject> backgroundBlocks = new List<GameObject>();
public List<GameObject> backgroundLevelBlocks = new List<GameObject>();
private float maximumDistanceOfPlatformFromPlayerBg = 600.0f;
2022-01-30 10:43:14 +00:00
public GameObject bgParrent = null;
2022-01-30 10:22:18 +00:00
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 13:39:34 +00:00
2022-01-30 10:22:18 +00:00
List<GameObject> spawnBackgroundObjects(Vector3 playerPosition, List<GameObject> backgroundBlocks, GameObject parentLevelObject)
{
List<GameObject> bgBlocksSpawnTemp = new List<GameObject>();
for (var i = 0; i < 200; i++)
{
2022-01-30 10:43:14 +00:00
int bgElement = Random.Range(0, backgroundBlocks.Count - 1);
2022-01-30 10:22:18 +00:00
GameObject newBgObject = Instantiate(backgroundBlocks[bgElement], (Random.insideUnitSphere * Random.Range(300, maximumDistanceOfPlatformFromPlayerBg) + playerPosition + new Vector3(0, 0, 50)), (Quaternion.identity));
newBgObject.transform.Rotate(new Vector3(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)));
2022-01-30 10:43:14 +00:00
newBgObject.transform.parent = bgParrent.transform;
2022-01-30 10:22:18 +00:00
bgBlocksSpawnTemp.Add(newBgObject);
}
2022-01-29 19:30:18 +00:00
2022-01-30 10:22:18 +00:00
return bgBlocksSpawnTemp;
}
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;
2022-01-30 00:41:59 +00:00
for (int i = 1; i < renderers.Length; i++)
2022-01-29 21:04:46 +00:00
{
bounds.Encapsulate(renderers[i].bounds);
}
return bounds;
}
else
{
return new Bounds();
}
2022-01-29 21:37:40 +00:00
2022-01-29 21:04:46 +00:00
}
GameObject drawPlatform(GameObject lastObject, GameObject objToSpawn, GameObject parentLevelObject)
{
Bounds bounds = this.getPrefabBounds(lastObject);
2022-01-30 00:41:59 +00:00
Bounds b = this.getPrefabBounds(objToSpawn);
Vector3 nextBlockLocation = new Vector3(lastObject.transform.position.x, lastObject.transform.position.y, lastObject.transform.position.z + bounds.extents.z + b.extents.z);
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 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-30 02:30:37 +00:00
float horizontalDistancePerPlatform = (float)Random.Range(2.0f, 3.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-30 10:34:49 +00:00
float radius = (pieceCount / 2) + 1.9f;
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);
2022-01-30 00:41:59 +00:00
Bounds b = this.getPrefabBounds(objToSpawn);
2022-01-29 19:15:14 +00:00
2022-01-30 00:41:59 +00:00
Vector3 centerPoint = new Vector3(lastObject.transform.position.x, (lastObject.transform.position.y + radius), lastObject.transform.position.z + (lastObject.name.Contains("chunk") ? bounds.size.z : bounds.extents.z) + b.extents.z);
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-30 00:41:59 +00:00
GameObject newObject = Instantiate(objToSpawn, new Vector3(position.x, position.y + heightOffset, position.z + (float)(i * horizontalDistancePerPlatform) + (lastObject.name.Contains("chunk") ? bounds.extents.z : 0f)), rotation);
2022-01-29 21:04:46 +00:00
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 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 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;
PlayerController playerControlsSript = this.player.GetComponent<PlayerController>();
2022-01-29 17:57:24 +00:00
2022-01-30 10:22:18 +00:00
if (backgroundLevelBlocks.Count < 200)
{
foreach (var spavnedBgBlock in this.spawnBackgroundObjects(playerPosition, this.backgroundBlocks, this.levelParrent))
{
this.backgroundLevelBlocks.Add(spavnedBgBlock);
}
}
2022-01-29 21:04:46 +00:00
if (playerControlsSript.isFalling)
{
return;
}
2022-01-30 10:22:18 +00:00
for (var i = 0; i < this.backgroundLevelBlocks.Count; i++)
{
float distance = Vector3.Distance(this.backgroundLevelBlocks[i].transform.position, playerPosition);
if (distance > this.maximumDistanceOfPlatformFromPlayer + maximumDistanceOfPlatformFromPlayerBg)
{
Destroy(this.backgroundLevelBlocks[i]);
this.backgroundLevelBlocks.Remove(this.backgroundLevelBlocks[i]);
}
}
for (var i = 0; i < this.spawnedLevelBlocks.Count; i++)
2022-01-29 15:58:11 +00:00
{
float distance = Vector3.Distance(this.spawnedLevelBlocks[i].transform.position, playerPosition);
if (distance > this.maximumDistanceOfPlatformFromPlayer && this.spawnedLevelBlocks.Count >= this.maximumNumberOfPlatformsAtScene && playerPosition.z > this.spawnedLevelBlocks[i].transform.position.z)
{
Destroy(this.spawnedLevelBlocks[i]);
this.spawnedLevelBlocks.Remove(this.spawnedLevelBlocks[i]);
}
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
}
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