using UnityEngine; using System.Collections; using System.Collections.Generic; public class ObstacleManager : MonoBehaviour { public float minSpawnRate = 2.5f; //rate of obstacle spawn public float maxSpawnRate = 5.0f; //rate of obstacle spawn public float maxSpeed = 0.1f; //speed at which objects move to left public float secondsPerPoint = 5.0f; //hard coding each obstacle type in this one mega class because small game public GameObject foodstuffPrefab; //0 public GameObject flyingFoodstuffPrefab; //0 public GameObject ballPrefab; //1 public GameObject verticalCactiPrefab; //2 public GameObject loopPrefab; //3 public GameObject loopFlippedPrefab; //4 public GameObject curvyCavePrefab; //5 public GameObject curvyCaveFlippedPrefab; //6 public GameObject foregroundPrefab; public GameObject backgroundPrefab; public GameObject backgroundBGPrefab; private List obstacles = new List(); //all active obstacles private List foregrounds = new List(); //all active foregrounds private List backgrounds = new List(); //all active backgrounds private List backgroundBGs = new List(); //all active backgroundBGs private float timeTilObstacleSpawn = 0; private int nextObstacle; //corresponding to prefabs as shown above private System.Random rng; private GUIText scoreDisplay; private float timeTilNextPoint; private int points; private bool paused = false; void Start () { rng = new System.Random(); scoreDisplay = GameObject.Find ("Score Display").GetComponent(); scoreDisplay.text = "0"; paused = true; PlanNextObstacle(); timeTilObstacleSpawn = 0; //initialize non-obstacle-y scenery lists CreateSceneryAtPos(new Vector3(-8.0f, 0.0f)); CreateSceneryAtPos(new Vector3(0.0f, 0.0f)); CreateSceneryAtPos(new Vector3(8.0f, 0.0f)); } void CreateSceneryAtPos(Vector3 spawnPosition) { GameObject newObject = Instantiate(foregroundPrefab, spawnPosition, Quaternion.identity) as GameObject; foregrounds.Add(newObject); newObject = Instantiate(backgroundPrefab, spawnPosition, Quaternion.identity) as GameObject; backgrounds.Add(newObject); newObject = Instantiate(backgroundBGPrefab, spawnPosition, Quaternion.identity) as GameObject; backgroundBGs.Add(newObject); } void FixedUpdate () { timeTilObstacleSpawn -= Time.fixedDeltaTime; timeTilNextPoint -= Time.fixedDeltaTime; //spawn obstacle if it's time if (!paused && timeTilObstacleSpawn <= 0) { switch (nextObstacle) { case 0: SpawnFoodstuffs(); break; case 1: SpawnBalls(); break; case 2: SpawnVerticalCacti(); break; case 3: SpawnLoop(false); break; case 4: SpawnLoop(true); break; case 5: SpawnCurvyCave(false); break; case 6: SpawnCurvyCave(true); break; default: break; } PlanNextObstacle(); } if (!paused && timeTilNextPoint <= 0) { GameObject.Find ("EarnPoint").audio.Play(); ++points; scoreDisplay.text = points.ToString(); timeTilNextPoint = secondsPerPoint; } //check first obstacle in list, if offscreen to left, destroy // technically, multiple objects could be off screen at the same time, // if rng dictates it as such, but who cares, this is good enough if (obstacles.Count > 0) { GameObject firstObstacle = obstacles[0] as GameObject; if (firstObstacle.transform.position.x < -10.0f) { Destroy(firstObstacle); obstacles.RemoveAt(0); } } //loop through all objects, moving them to left foreach (GameObject obstacle in obstacles) { Vector3 position = obstacle.transform.position; position.x -= maxSpeed; obstacle.transform.position = position; } //check first foreground in list, if offscreen to left, destroy if (foregrounds.Count > 0) { GameObject firstForeground = foregrounds[0] as GameObject; if (firstForeground.transform.position.x <= -8.0f) { Destroy(firstForeground); foregrounds.RemoveAt(0); Vector3 spawnPosition = foregrounds[0].transform.position; spawnPosition.x += 8.0f; GameObject newForeground = Instantiate(foregroundPrefab, spawnPosition, Quaternion.identity) as GameObject; foregrounds.Add(newForeground); } } //loop through all foregrounds, moving them to left foreach (GameObject foreground in foregrounds) { Vector3 position = foreground.transform.position; position.x -= maxSpeed; foreground.transform.position = position; } //check first background in list, if offscreen to left, destroy if (backgrounds.Count > 0) { GameObject firstBackground = backgrounds[0] as GameObject; if (firstBackground.transform.position.x <= -8.0f) { Destroy(firstBackground); backgrounds.RemoveAt(0); Vector3 spawnPosition = backgrounds[0].transform.position; spawnPosition.x += 8.0f; GameObject newBackground = Instantiate(backgroundPrefab, spawnPosition, Quaternion.identity) as GameObject; backgrounds.Add(newBackground); } } //loop through all backgrounds, moving them to left foreach (GameObject background in backgrounds) { Vector3 position = background.transform.position; position.x -= maxSpeed/2; background.transform.position = position; } //check first background in list, if offscreen to left, destroy if (backgroundBGs.Count > 0) { GameObject firstBackgroundBG = backgroundBGs[0] as GameObject; if (firstBackgroundBG.transform.position.x <= -8.0f) { Destroy(firstBackgroundBG); backgroundBGs.RemoveAt(0); Vector3 spawnPosition = backgroundBGs[0].transform.position; spawnPosition.x += 8.0f; GameObject newBackgroundBG = Instantiate(backgroundBGPrefab, spawnPosition, Quaternion.identity) as GameObject; backgroundBGs.Add(newBackgroundBG); } } //loop through all backgrounds, moving them to left foreach (GameObject backgroundBG in backgroundBGs) { Vector3 position = backgroundBG.transform.position; position.x -= maxSpeed/4; backgroundBG.transform.position = position; } } public void Pause() { paused = true; GameObject.Find ("Score Text").GetComponent().text = points.ToString(); } public void Reset() { foreach (GameObject obj in obstacles) { GameObject.Destroy(obj); } obstacles.Clear(); PlanNextObstacle(); timeTilObstacleSpawn = 0; scoreDisplay.text = "0"; timeTilNextPoint = secondsPerPoint; points = 0; paused = false; } void PlanNextObstacle() { double result = (rng.NextDouble()*100.0f); //hard code (again!) probabilities because small game if (result <= 35) { //ball course nextObstacle = 1; } else if (result <= 70) { //cacti course nextObstacle = 2; } else if (result <= 77.5) { //loop 1 nextObstacle = 3; } else if (result <= 85) { //loop 2 nextObstacle = 4; } else if (result <= 92.5) { //curvy cave 1 nextObstacle = 5; } else {//if (result <= 100) { //curvy cave 2 nextObstacle = 6; } //nextObstacle = 5; } void SpawnFoodstuffs() { //spawn at same x coordinate, but varied y coordinate Vector3 spawnPosition = new Vector3(4.5f, (float)rng.NextDouble()*(3.0f-2.22f) - 3.0f); GameObject newFoodstuff = Instantiate(foodstuffPrefab, spawnPosition, Quaternion.identity) as GameObject; obstacles.Add (newFoodstuff); timeTilObstacleSpawn = 3f; } //haha this function is a mess void SpawnBalls() { //randomize number of columns of balls, from 2 to 10 int numBallColumns = rng.Next (5,11); bool lastColHadFood = false; for (int i=0; i