/// <summary> /// deletes chunks as the move off the left side of the screen /// </summary> private void RemoveOffscreenChunks() { float limitX; limitX = FindScreenLeft(); //set where the left edge of the screen is for (int i = chunks.Count - 1; i >= 0; i--) //goes through the list of all chunks { if (chunks[i].rightedge.position.x < limitX) //compares the right edge of a chunk to left edge of screen { //if chunk if off left side of screen BreuChunks chunk = chunks[i]; BreuPlatform[] deadPlatforms = chunk.GetComponentsInChildren <BreuPlatform>(); //collects list of all platforms in chunk foreach (BreuPlatform platform in deadPlatforms) //deletes all platforms in chunk { platforms.Remove(platform.GetComponent <BreuAABB>()); } BreuSpring[] deadSprings = chunk.GetComponentsInChildren <BreuSpring>(); //collects list of all spring in chunk foreach (BreuSpring spring in deadSprings) //deletes all springs in chunk { springs.Remove(spring.GetComponentInChildren <BreuAABB>()); } BreuSpring[] deadHazards = chunk.GetComponentsInChildren <BreuSpring>(); //collects list of all spring in chunk foreach (BreuSpring hazard in deadHazards) //deletes all springs in chunk { hazards.Remove(hazard.GetComponentInChildren <BreuAABB>()); } BreuSpring[] deadOneWays = chunk.GetComponentsInChildren <BreuSpring>(); //collects list of all spring in chunk foreach (BreuSpring oneway in deadOneWays) //deletes all springs in chunk { Oneways.Remove(oneway.GetComponentInChildren <BreuAABB>()); } BreuSpring[] deadPickup = chunk.GetComponentsInChildren <BreuSpring>(); //collects list of all spring in chunk foreach (BreuSpring pickup in deadPickup) //deletes all springs in chunk { PickUps.Remove(pickup.GetComponentInChildren <BreuAABB>()); } chunks.RemoveAt(i); //remove chunk from list of chunk Destroy(chunk.gameObject); //deletes chunk object, which should be empty } } }
/// <summary> /// Spawns new chunk prefabs and add AABBs present in prefabs to lists /// </summary> /// <param name="isStarting"></param> private void SpawnChunk() { //spawn new chunk: float GapSize = Random.Range(XGapSizeMin, XGapSizeMax);//set distance between chunks Vector3 pos = new Vector3(); if (chunks.Count > 0) { pos.x = chunks[chunks.Count - 1].rightedge.position.x + GapSize; pos.y = chunks[chunks.Count - 1].rightedge.position.y; } int index = Random.Range(0, prefabChunk.Length); //selects chunk at random from array BreuChunks chunk = Instantiate(prefabChunk[index], pos, Quaternion.identity); //spawns chunk chunks.Add(chunk); //adds ne chunk to chunk list ///<summary> ///adds platforms from new chunk to list of platforms ///</summary> BreuPlatform[] newplatforms = chunk.GetComponentsInChildren <BreuPlatform>(); foreach (BreuPlatform p in newplatforms) { platforms.Add(p.GetComponent <BreuAABB>());//add AABB for platforms into a list of platform AABBs } ///<summary> ///adds springs from new chunk to list of springs ///</summary> BreuSpring[] newsprings = chunk.GetComponentsInChildren <BreuSpring>(); foreach (BreuSpring s in newsprings) { springs.Add(s.GetComponent <BreuAABB>());//add AABB for springs into a list of spring AABBs } ///<summary> ///adds hazards from new chunk to list of springs ///</summary> BreuHazard[] newHazards = chunk.GetComponentsInChildren <BreuHazard>(); foreach (BreuHazard h in newHazards) { hazards.Add(h.GetComponent <BreuAABB>()); } ///<summary> ///adds OneWays from new chunk to list of springs ///</summary> BreuOneWay[] newOneWays = chunk.GetComponentsInChildren <BreuOneWay>(); foreach (BreuOneWay o in newOneWays) { Oneways.Add(o.GetComponent <BreuAABB>()); } ///<summary> ///adds hazards from new chunk to list of springs ///</summary> BreuPickUp[] newPickUps = chunk.GetComponentsInChildren <BreuPickUp>(); foreach (BreuPickUp p in newPickUps) { PickUps.Add(p.GetComponent <BreuAABB>()); } }