Pool. closes #1

This commit is contained in:
2023-07-15 21:40:01 +05:30
parent 660a3bb021
commit 3ede35447e
9 changed files with 448 additions and 117 deletions

View File

@@ -21,15 +21,37 @@ public class LevelGenerator : MonoBehaviour
void Start()
{
Application.targetFrameRate= 60;
}
float t =0;
// Update is called once per frame
public List<GameObject> borrowedObjects = new List<GameObject>();
void Update()
{
if(PlayerController.instance.transform.position.z > (curBlock-3) * distanceBetweenBlocks){
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);
}
}
@@ -45,7 +67,9 @@ public class LevelGenerator : MonoBehaviour
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 = 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;

View File

@@ -5,40 +5,50 @@ using UnityEngine;
public class ObjectPool : MonoBehaviour
{
public static ObjectPool instance;
public static Dictionary<GameObject, List<Transform>> pool;
public static Dictionary<string, List<GameObject>> pool = new Dictionary<string, List<GameObject>>();
void Awake(){
instance= this;
}
public static GameObject Spawn(GameObject obj, Vector3 position){
if(pool.ContainsKey(obj)){
if(pool.ContainsKey(obj.name)){
// Debug.Log($"Has key : {pool[obj.name].Count}");
//use from pool
if(pool[obj].Count <=0){
if(pool[obj.name].Count <=0){
GameObject go = Instantiate(obj, position, Quaternion.identity);
pool.Add(obj, new List<Transform>());
pool[obj].Add(go.transform);
// Debug.Log("Empty Reserves");
// pool[obj.name] new List<GameObject>();
pool[obj.name].Add(go);
return go;
}else{
GameObject chosen = pool[obj][0].gameObject;
pool[obj].RemoveAt(0);
GameObject chosen = pool[obj.name][0].gameObject;
chosen.gameObject.SetActive(true);
chosen.transform.position = position;
pool[obj.name].RemoveAt(0);
// Debug.Log("Reusing");
return chosen;
}
}else{
GameObject go = Instantiate(obj, position, Quaternion.identity);
pool.Add(obj, new List<Transform>());
pool[obj].Add(go.transform);
return go;
// Debug.Log("First");
GameObject go = Instantiate(obj, position, Quaternion.identity);
pool.Add(obj.name, new List<GameObject>());
pool[obj.name].Add(go);
return go;
}
}
public static void Despawn(GameObject obj){
// if()
string objName = obj.name.Replace("(Clone)","");
if(!pool.ContainsKey(objName)){
pool.Add(obj.name, new List<GameObject>());
}
pool[objName].Add(obj);
obj.SetActive(false);
// Debug.Log($"Adding {objName} back to pool, " + pool[objName].Count);
}
}
public class PoolItems{
}

View File

@@ -33,6 +33,13 @@ public class PlayerController : MonoBehaviour
transform.localEulerAngles = new Vector3(0,0, turningForce * turningRotation);
}
void OnTriggerEnter(Collider other){
Debug.Log("Hit with " + other.name);
Application.LoadLevel(0);
}
float input = 0;
public void OnLeftPanel(){
input = -1;