GGJ2022/Assets/Scripts/ProceduralGeneration.cs

220 lines
8.5 KiB
C#
Raw Permalink 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 11:12:24 +00:00
private 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)
{
2022-01-30 13:17:35 +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);
Vector3 nextBlockLocation = new Vector3(objToSpawn.transform.position.x, objToSpawn.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-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);
//newObject.transform.parent = parentLevelObject.transform;
2022-01-29 21:04:46 +00:00
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;
2022-01-30 11:12:24 +00:00
if (bgParrent == null)
{
bgParrent = GameObject.Find("Background");
}
2022-01-29 19:30:18 +00:00
this.spawnedLevelBlocks.Add(lastBlock);
}
2022-01-29 13:39:34 +00:00
// Update is called once per frame
void Update()
{
2022-01-30 10:57:52 +00:00
// if (playerControlsSript.isFalling)
// {
// return;
// }
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);
}
}
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 + 3f)
{
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-30 13:31:07 +00:00
int blockToSpawn = Random.Range(1, (levelBlocks.Count + 1));
2022-01-29 21:04:46 +00:00
2022-01-30 11:12:24 +00:00
if (
(blockToSpawn > -1 && (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-30 13:31:07 +00:00
blockToSpawn = Random.Range(1, 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-30 13:31:07 +00:00
if (this.spawnedLevelBlocks.Count < 3)
2022-01-30 13:17:35 +00:00
{
2022-01-30 13:46:59 +00:00
blockToSpawn = 9;
}
else
{
if (lastBlockPrefab.name == "platform_rrange" && levelBlocks[blockToSpawn].name != "platform_empty")
{
blockToSpawn = 3;
}
2022-01-30 13:17:35 +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-30 13:46:59 +00:00
if (lastBlockPrefab.name == "platform_rrange")
{
blockObjToSpawn = levelBlocks[3];
instantiatedGameObject = this.drawPlatform(this.lastBlock, this.levelBlocks[3], this.levelParrent);
this.spawnedLevelBlocks.Add(instantiatedGameObject);
}
else
2022-01-29 17:30:08 +00:00
{
2022-01-30 13:46:59 +00:00
List<GameObject> instantiatedGameObjectLists = this.spawnSpiralOfPlatforms(lastBlock, levelBlocks[0], this.levelParrent);
foreach (var spavnedBlock in instantiatedGameObjectLists)
{
this.spawnedLevelBlocks.Add(spavnedBlock);
}
instantiatedGameObject = this.spawnedLevelBlocks[this.spawnedLevelBlocks.Count - 1];
blockObjToSpawn = levelBlocks[0];
2022-01-29 17:30:08 +00:00
}
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