84 lines
2.5 KiB
C#
84 lines
2.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class LevelGenerator : MonoBehaviour
|
|
{
|
|
public GameObject cube;
|
|
|
|
[Header("Spawn")]
|
|
|
|
public int minBlocks=2;
|
|
public int maxBlocks = 5;
|
|
public Vector3 minScale = Vector3.one;
|
|
public Vector3 maxScale = new Vector3(2,8,2);
|
|
public float rotationRange = 15;
|
|
|
|
public float distanceBetweenBlocks = 10;
|
|
public float rowSpacing = 20;
|
|
|
|
public int curBlock = 1;
|
|
|
|
void Start()
|
|
{
|
|
Application.targetFrameRate= 60;
|
|
}
|
|
|
|
float t =0;
|
|
public List<GameObject> borrowedObjects = new List<GameObject>();
|
|
void Update()
|
|
{
|
|
if(PlayerController.instance.transform.position.z > (curBlock-4) * distanceBetweenBlocks){
|
|
GenerateBlock();
|
|
}
|
|
|
|
if(t < 1){
|
|
t+=Time.deltaTime;
|
|
}else{
|
|
t=0;
|
|
CheckBorrowed();
|
|
}
|
|
}
|
|
|
|
void CheckBorrowed(){
|
|
List<int> itemsToRemove = new List<int>();
|
|
for(int i=0; i < borrowedObjects.Count; i++){
|
|
if(borrowedObjects[i].transform.position.z < PlayerController.instance.transform.position.z){
|
|
ObjectPool.Despawn(borrowedObjects[i]);
|
|
itemsToRemove.Add(i);
|
|
}
|
|
}
|
|
|
|
for(int i = itemsToRemove.Count-1; i > 0;i--){
|
|
borrowedObjects.RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
|
|
void GenerateBlock(){
|
|
int cubesCount = Random.Range(minBlocks, maxBlocks);
|
|
float baseX = PlayerController.instance.transform.position.x -Random.Range(rowSpacing /2f, rowSpacing);
|
|
for(int j =0; j < 5; j++){
|
|
bool flip = false;
|
|
|
|
for(int i =0; i < cubesCount; i++){
|
|
float z = distanceBetweenBlocks * curBlock;
|
|
|
|
Vector3 size = new Vector3(Random.Range(minScale.x,maxScale.x),Random.Range(minScale.y,maxScale.y),Random.Range(minScale.z,maxScale.z));
|
|
float x = baseX + (i * size.x * (flip ? -1 : 1));
|
|
|
|
// GameObject newCube = Instantiate(cube, new Vector3(x,size.y * 0.4f,z), Quaternion.identity);
|
|
GameObject newCube = ObjectPool.Spawn(cube, new Vector3(x,size.y * 0.4f,z));
|
|
borrowedObjects.Add(newCube);
|
|
newCube.transform.localScale = size;
|
|
newCube.transform.Rotate(new Vector3(0,0, rotationRange * ((float)i / (float)cubesCount) * (flip ? 1 : -1)));
|
|
flip = !flip;
|
|
}
|
|
|
|
baseX += Random.Range(rowSpacing / 2f, rowSpacing);
|
|
}
|
|
|
|
curBlock++;
|
|
}
|
|
}
|