GGJ2022/Assets/Scripts/ProceduralGeneration.cs

136 lines
4.9 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
{
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 13:39:34 +00:00
// Start is called before the first frame update
void Start()
{
2022-01-29 15:58:11 +00:00
lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject;
lastBlock = this.gameObject.transform.GetChild(0).gameObject;
2022-01-29 17:30:08 +00:00
this.spawnedLevelBlocks.Add(lastBlock);
}
2022-01-29 13:39:34 +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 18:13:01 +00:00
float horizontalDistancePerPlatform = 0.5f;
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;
Vector3 centerPoint = new Vector3(lastObject.transform.position.x, (lastObject.transform.position.y + radius), lastObject.transform.position.z + 3.0f);
2022-01-29 15:58:11 +00:00
2022-01-29 18:13:01 +00:00
float heightOffset = radius;
2022-01-29 17:30:08 +00:00
for (int i = 1; i < pieceCount + 2; i++)
2022-01-29 13:39:34 +00:00
{
2022-01-29 17:30:08 +00:00
Quaternion rotation = (Quaternion.AngleAxis((i - 1) * 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 13:39:34 +00:00
// Update is called once per frame
void Update()
{
int maxNumberOfBlock = 200;
2022-01-29 18:13:01 +00:00
2022-01-29 15:58:11 +00:00
Vector3 playerPosition = this.player.transform.position;
2022-01-29 17:57:24 +00:00
Debug.Log("Index" + 0);
2022-01-29 15:58:11 +00:00
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 > 10.0f && this.spawnedLevelBlocks.Count >= maxNumberOfBlock)
{
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 18:13:01 +00:00
if (this.spawnedLevelBlocks.Count <= maxNumberOfBlock)
2022-01-29 15:58:11 +00:00
{
2022-01-29 17:30:08 +00:00
int blockToSpawn = Random.Range(0, levelBlocks.Count - 1);
2022-01-29 17:57:24 +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 17:30:08 +00:00
blockObjToSpawn = levelBlocks[blockToSpawn];
2022-01-29 15:58:11 +00:00
if (blockObjToSpawn.name == lastBlockPrefab.name)
{
Debug.Log("Same Block");
if ((blockToSpawn + 1) <= levelBlocks.Count)
{
blockToSpawn++;
}
else if ((blockToSpawn - 1) >= levelBlocks.Count)
{
blockToSpawn--;
}
2022-01-29 17:57:24 +00:00
if ((blockToSpawn + 1) <= levelBlocks.Count)
{
blockToSpawn++;
}
else if ((blockToSpawn - 1) >= levelBlocks.Count)
{
blockToSpawn--;
}
2022-01-29 17:30:08 +00:00
}
if ((blockToSpawn > -1 && (blockToSpawn < (levelBlocks.Count - 1))))
{
MeshFilter meshfilter = this.lastBlock.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 15:58:11 +00:00
blockObjToSpawn = levelBlocks[blockToSpawn];
Vector3 nextBlockLocation = new Vector3(this.lastBlock.transform.position.x, this.lastBlock.transform.position.y, this.lastBlock.transform.position.z + b.size.z + 1.0f);
instantiatedGameObject = Instantiate(blockObjToSpawn, nextBlockLocation, (Quaternion.identity));
2022-01-29 17:57:24 +00:00
this.spawnedLevelBlocks.Add(instantiatedGameObject);
2022-01-29 17:30:08 +00:00
}
else
{
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 17:30:08 +00:00
Debug.Log("Spawn" + blockToSpawn);
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