BG Generation

This commit is contained in:
GamerClassN7 2022-01-30 11:22:18 +01:00
parent 816b51b439
commit 25672d4685

View File

@ -4,17 +4,38 @@ using UnityEngine;
public class ProceduralGeneration : MonoBehaviour public class ProceduralGeneration : MonoBehaviour
{ {
[Header("Level")]
public List<GameObject> levelBlocks = new List<GameObject>(); public List<GameObject> levelBlocks = new List<GameObject>();
private int maximumNumberOfPlatformsAtScene = 100;
public List<GameObject> spawnedLevelBlocks = new List<GameObject>(); public List<GameObject> spawnedLevelBlocks = new List<GameObject>();
private float maximumDistanceOfPlatformFromPlayer = 20.0f;
[Header("Background")]
public List<GameObject> backgroundBlocks = new List<GameObject>();
public List<GameObject> backgroundLevelBlocks = new List<GameObject>();
private float maximumDistanceOfPlatformFromPlayerBg = 600.0f;
public GameObject player = null; public GameObject player = null;
public GameObject lastBlock; public GameObject lastBlock;
private GameObject lastBlockPrefab; private GameObject lastBlockPrefab;
private int spavnetobjectIndex = 0;
private int maximumNumberOfPlatformsAtScene = 100;
private float maximumDistanceOfPlatformFromPlayer = 20.0f;
private GameObject levelParrent = null; private GameObject levelParrent = null;
List<GameObject> spawnBackgroundObjects(Vector3 playerPosition, List<GameObject> backgroundBlocks, GameObject parentLevelObject)
{
List<GameObject> bgBlocksSpawnTemp = new List<GameObject>();
for (var i = 0; i < 200; i++)
{
int bgElement = Random.Range(0, backgroundBlocks.Count);
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)));
newBgObject.transform.parent = parentLevelObject.transform;
bgBlocksSpawnTemp.Add(newBgObject);
}
return bgBlocksSpawnTemp;
}
Bounds getPrefabBounds(GameObject go) Bounds getPrefabBounds(GameObject go)
{ {
Renderer[] renderers = go.GetComponentsInChildren<Renderer>(); Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
@ -43,7 +64,6 @@ public class ProceduralGeneration : MonoBehaviour
newObject.transform.parent = parentLevelObject.transform; newObject.transform.parent = parentLevelObject.transform;
return newObject; return newObject;
} }
List<GameObject> spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn, GameObject parentLevelObject) List<GameObject> spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn, GameObject parentLevelObject)
{ {
// configuration: // configuration:
@ -76,7 +96,6 @@ public class ProceduralGeneration : MonoBehaviour
return levelBlocksSpawnTemp; return levelBlocksSpawnTemp;
} }
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -84,8 +103,6 @@ public class ProceduralGeneration : MonoBehaviour
lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject; lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject;
lastBlock = this.gameObject.transform.GetChild(0).gameObject; lastBlock = this.gameObject.transform.GetChild(0).gameObject;
this.spawnedLevelBlocks.Add(lastBlock); this.spawnedLevelBlocks.Add(lastBlock);
} }
// Update is called once per frame // Update is called once per frame
@ -94,11 +111,30 @@ public class ProceduralGeneration : MonoBehaviour
Vector3 playerPosition = this.player.transform.position; Vector3 playerPosition = this.player.transform.position;
PlayerController playerControlsSript = this.player.GetComponent<PlayerController>(); PlayerController playerControlsSript = this.player.GetComponent<PlayerController>();
if (backgroundLevelBlocks.Count < 200)
{
foreach (var spavnedBgBlock in this.spawnBackgroundObjects(playerPosition, this.backgroundBlocks, this.levelParrent))
{
this.backgroundLevelBlocks.Add(spavnedBgBlock);
}
}
if (playerControlsSript.isFalling) if (playerControlsSript.isFalling)
{ {
return; return;
} }
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++) for (var i = 0; i < this.spawnedLevelBlocks.Count; i++)
{ {
float distance = Vector3.Distance(this.spawnedLevelBlocks[i].transform.position, playerPosition); float distance = Vector3.Distance(this.spawnedLevelBlocks[i].transform.position, playerPosition);
@ -106,7 +142,6 @@ public class ProceduralGeneration : MonoBehaviour
{ {
Destroy(this.spawnedLevelBlocks[i]); Destroy(this.spawnedLevelBlocks[i]);
this.spawnedLevelBlocks.Remove(this.spawnedLevelBlocks[i]); this.spawnedLevelBlocks.Remove(this.spawnedLevelBlocks[i]);
spavnetobjectIndex++;
} }
else else
{ {