/// <summary> /// Gives the user a sneak peak at the level, then begins following the player. /// </summary> private IEnumerator WaitCamera(CameraController camController) { yield return(new WaitForSecondsRealtime(Constants.CameraOverviewTime)); camController.Following = true; }
private void _generateLevel() { Random rgen = new Random(); Vector3[] workArea = Squarify(Camera.GetFustrumBounds(Camera.transform.position.y), Camera); int tilesPerRow = Mathf.CeilToInt(Mathf.Sqrt(Constants.TileCountPerScreen)); TileDimension = (workArea[0].x - workArea[1].x) / tilesPerRow; TileScale = new Vector3(TileDimension, TileDimension, 1); GameObject[,] tiles = new GameObject[tilesPerRow, tilesPerRow]; int currentExitPossibility = 0; int[] xExitPossibilities = new int[tilesPerRow * 4 - 8]; int[] yExitPossibilities = new int[tilesPerRow * 4 - 8]; List <int[]> entityPlacementPossibilities = new List <int[]>(); for (int x = 0; x < tilesPerRow; x++) { for (int z = 0; z < tilesPerRow; z++) { bool isBlock = rgen.NextDouble() < Constants.BlockChance; GameObject toInstantiate = isBlock ? BlockPrefab : FloorPrefab; if (z == 0 || x == 0 || x == tilesPerRow - 1 || z == tilesPerRow - 1) { toInstantiate = BlockPrefab; isBlock = true; if (!(x == 0 && z == 0) && !(x == 0 && z == tilesPerRow - 1) && !(x == tilesPerRow - 1 && z == 0) && !(x == tilesPerRow - 1 && z == tilesPerRow - 1)) { xExitPossibilities[currentExitPossibility] = x; yExitPossibilities[currentExitPossibility] = z; currentExitPossibility++; } } if (!isBlock) { entityPlacementPossibilities.Add(new[] { x, z }); } tiles[x, z] = Instantiate(toInstantiate, GetTransform(workArea, TileDimension, x, z), FloorPrefab.transform.rotation); tiles[x, z].transform.localScale = TileScale; } } int exit = rgen.Next(0, xExitPossibilities.Length); int xExit = xExitPossibilities[exit]; int yExit = yExitPossibilities[exit]; GameObject inTheWay = tiles[xExit, yExit]; Transform oldTransform = inTheWay.transform; Destroy(inTheWay); GameObject doorGo = Instantiate(DoorPrefab, oldTransform.position, oldTransform.rotation); doorGo.transform.localScale = TileScale; tiles[xExit, yExit] = doorGo; int xDestroy = -1; int yDestroy = -1; if (yExit == 0) { if (tiles[xExit, yExit + 1].GetComponent <Block>()) { yDestroy = yExit + 1; xDestroy = xExit; } } else if (yExit == tilesPerRow - 1) { if (tiles[xExit, yExit - 1].GetComponent <Block>()) { yDestroy = yExit - 1; xDestroy = xExit; } } else if (xExit == 0) { if (tiles[xExit + 1, yExit].GetComponent <Block>()) { yDestroy = yExit; xDestroy = xExit + 1; } } else if (xExit == tilesPerRow - 1) { if (tiles[xExit - 1, yExit].GetComponent <Block>()) { yDestroy = yExit; xDestroy = xExit - 1; } } if (!(yDestroy == -1 && xDestroy == -1)) { GameObject alsoInTheWay = tiles[xDestroy, yDestroy]; Transform anotherOldTransform = alsoInTheWay.transform; Destroy(alsoInTheWay); GameObject floor = Instantiate(FloorPrefab, anotherOldTransform.position, anotherOldTransform.rotation); floor.transform.localScale = TileScale; tiles[xDestroy, yDestroy] = floor; } _camController = GameObject.Find("Cameras").GetComponent <CameraController>(); int index = rgen.Next(0, entityPlacementPossibilities.Count); GameObject go = tiles[entityPlacementPossibilities[index][0], entityPlacementPossibilities[index][1]]; Player = Instantiate(PlayerPrefab, go.transform.position, PlayerPrefab.transform.rotation); Player.transform.localScale = TileScale; _camController.player = Player; Player.GetComponent <Player>().camController = _camController; entityPlacementPossibilities.RemoveAt(index); for (int i = 0; i < Constants.EnemyLimit; i++) { index = rgen.Next(0, entityPlacementPossibilities.Count); go = tiles[entityPlacementPossibilities[index][0], entityPlacementPossibilities[index][1]]; GameObject newEnemy = Instantiate(SkullPrefab, go.transform.position, SkullPrefab.transform.rotation); newEnemy.transform.localScale = TileScale; newEnemy.GetComponent <Skull>().OnCollisionEnter(); Enemy.EnemyList.Add(newEnemy); entityPlacementPossibilities.RemoveAt(index); } }