public void ChooseLevel() { bool validLevel = false; while (!validLevel) { GenerateLevel(); searchSpace.UpdateSearchSpace(); for (int i = 0; i < numberOfLanes; i++) { searchSpace.startRow.Add(searchSpace.tiles[i]); } for (int i = numberOfRows * numberOfLanes - numberOfLanes; i < numberOfRows * numberOfLanes; i++) { bool found = UsePathfinderOnce(searchSpace.tiles[i]); if (found) { validLevel = true; break; } } } if (theme.extras.Count > 0) { InstantiateExtras(); } if (validLevel) { for (int i = 0; i < searchSpace.tiles.Count; i++) { Tile tile = searchSpace.tiles[i]; if (tile.hasObstacle && tile.obstacle.obstaclePrefab != null) { ObstacleInstance obstacle = tile.obstacle; Transform transform = tile.gameObject.transform; InstantiateObstacle(obstacle, transform, levelObstacles.transform); } } } }
/// <summary> /// Checks if there's an obstacle on tile. /// Also checkes if the tile in front of this one has an obstacle that covers more than one tile, /// and in that case sets variable hasObstacle to true and the tilesCovered of the obstacle on this tile. /// </summary> /// <returns>Wheter there's an obstacle on this tile.</returns> public bool CheckForObstacle(int counter) { counter++; if (hasObstacle && counter <= 1) { return(true); } Vector3 pos = gameObject.transform.position; pos += Vector3.back; Tile tileInFront = searchSpace.tiles.Find(x => x.gameObject.transform.position == pos); //there's no tile in front, therefor there's no obstacle in front if (tileInFront == null) { return(false); } //there's no obstacle in front if (!tileInFront.hasObstacle) { return(false); } //if there is an obstacle in front and it's the first of the tiles it's covering if (tileInFront.obstacle.obstaclePrefab != null) { bool obstacle = false; if (tileInFront.obstacle.obstaclePrefab.tilesCovered > 1 && tileInFront.obstacle.obstaclePrefab.tilesCovered > counter) { obstacle = true; } return(obstacle); } else //if there's an obstacle in front and it's not the first of the tiles it's covering { return(tileInFront.CheckForObstacle(counter)); } }
/// <summary> /// Instantiates an obstacle. /// If spawnFrequently is true for the obstacle, it will spawn in even intervals according to the spacing value. /// If spawnFrequently is false for the obstacle, it will be spawn according to the spawnProbablility value. /// </summary> /// <param name="obstacle">The obstacle to be instatiated.</param> /// <param name="transformParent">Transform of the tile the obstacle will be placed upon.</param> /// <param name="parentObject">Transform of the game object in the scene which will be set as parent.</param> private bool InstantiateObstacle(ObstacleInstance obstacle, Transform transformParent, Transform parentObject) { Vector3 tempPos = new Vector3(transformParent.transform.position.x, transformParent.transform.position.y + 1, transformParent.transform.position.z); if (obstacle.obstaclePrefab.prefab == null) { return(false); } GameObject newObstacle = Instantiate(obstacle.obstaclePrefab.prefab, tempPos, Quaternion.identity, parentObject); newObstacle.name = obstacle.obstaclePrefab.name; if (obstacle.spawnFrequently) { obstacle.lastInstance = newObstacle.transform; } return(true); }