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;
|
|
|
|
private Vector3 lastBlockSpawnPoint;
|
2022-01-29 17:57:24 +00:00
|
|
|
public GameObject lastBlock;
|
2022-01-29 15:58:11 +00:00
|
|
|
private GameObject lastBlockPrefab;
|
|
|
|
private int blockIndex = 0;
|
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 18:13:01 +00:00
|
|
|
lastBlockSpawnPoint = this.gameObject.transform.GetChild(0).gameObject.transform.position;
|
2022-01-29 17:30:08 +00:00
|
|
|
this.spawnedLevelBlocks.Add(lastBlock);
|
|
|
|
}
|
2022-01-29 13:39:34 +00:00
|
|
|
|
2022-01-29 17:30:08 +00:00
|
|
|
List<GameObject> drawLoop(GameObject lastObject, GameObject objToSpawn)
|
|
|
|
{
|
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;
|
2022-01-29 17:57:24 +00:00
|
|
|
Vector3 centerPoint = new Vector3(lastObject.transform.position.x, (lastObject.transform.position.y + radius), lastObject.transform.position.z);
|
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()
|
|
|
|
{
|
2022-01-29 18:13:01 +00:00
|
|
|
int maxNumberOfBlock = 20;
|
|
|
|
|
2022-01-29 15:58:11 +00:00
|
|
|
Vector3 playerPosition = this.player.transform.position;
|
2022-01-29 17:57:24 +00:00
|
|
|
float distance = Vector3.Distance(this.spawnedLevelBlocks[0].transform.position, playerPosition);
|
|
|
|
|
|
|
|
Debug.Log("Index" + 0);
|
2022-01-29 15:58:11 +00:00
|
|
|
|
2022-01-29 18:13:01 +00:00
|
|
|
if (distance > 20.0f && this.spawnedLevelBlocks.Count > maxNumberOfBlock)
|
2022-01-29 15:58:11 +00:00
|
|
|
{
|
2022-01-29 17:57:24 +00:00
|
|
|
Destroy(this.spawnedLevelBlocks[0]);
|
|
|
|
this.spawnedLevelBlocks.Remove(this.spawnedLevelBlocks[0]);
|
2022-01-29 17:30:08 +00:00
|
|
|
spavnetobjectIndex++;
|
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
|
|
|
{
|
|
|
|
MeshFilter meshfilter = 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 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))))
|
|
|
|
{
|
2022-01-29 15:58:11 +00:00
|
|
|
blockObjToSpawn = levelBlocks[blockToSpawn];
|
2022-01-29 17:30:08 +00:00
|
|
|
instantiatedGameObject = Instantiate(blockObjToSpawn, new Vector3(0, 0, blockIndex * (b.size.z + 1.0f)), (Quaternion.identity));
|
2022-01-29 17:57:24 +00:00
|
|
|
this.spawnedLevelBlocks.Add(instantiatedGameObject);
|
2022-01-29 17:30:08 +00:00
|
|
|
blockIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
List<GameObject> instantiatedGameObjectLists = this.drawLoop(lastBlock, levelBlocks[0]);
|
|
|
|
foreach (var spavnedBlock in instantiatedGameObjectLists)
|
|
|
|
{
|
|
|
|
this.spawnedLevelBlocks.Add(spavnedBlock);
|
|
|
|
blockIndex++;
|
|
|
|
}
|
|
|
|
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);
|
2022-01-29 15:58:11 +00:00
|
|
|
|
|
|
|
lastBlock = instantiatedGameObject;
|
|
|
|
lastBlockSpawnPoint = instantiatedGameObject.transform.position;
|
2022-01-29 17:30:08 +00:00
|
|
|
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
|
|
|
|