private void Start() { Subdungeon rootSubDungeon = new Subdungeon(new Rect(0, 0, rows, columns)); CreateBSP(rootSubDungeon); rootSubDungeon.CreateRooms(); boardPosFloor = new GameObject[rows, columns]; DrawRooms(rootSubDungeon); DrawCorridors(rootSubDungeon); DrawWalls(); DrawBase(); Vector3 playerPos = new Vector3(rootSubDungeon.GetRoom().xMax / 2, rootSubDungeon.GetRoom().yMax / 2, 0f); //put the player in the middle of the first room player.transform.position = playerPos; //set player position Vector3 center = new Vector3(rows / 2, columns / 2, -1f); //have the minimap render out a top down view of the center of the map minimap.transform.position = center; minimap.orthographicSize = rows / 2f; //keep size large enough that it can see the entire dungeon output playerPos.z = -9f; Darkness.transform.position = playerPos; }
public void DrawCorridors(Subdungeon subdungeon) { //Draws the corridors onto the map based on the construction given from the CreateCorridors() if (subdungeon == null) { return; } DrawCorridors(subdungeon.left); DrawCorridors(subdungeon.right); foreach (Rect corridor in subdungeon.corridors) { for (int i = (int)corridor.x; i < corridor.xMax; i++) { for (int j = (int)corridor.y; j < corridor.yMax; j++) { if (boardPosFloor[i, j] == null) { Vector3 pos = new Vector3(i, j, 0f); //this made it work I dont know 8 and 4 int rand = (int)Random.Range(0, floorTiles.Length); GameObject newInstance = Instantiate(floorTiles[rand], pos, Quaternion.identity) as GameObject; newInstance.transform.SetParent(transform); boardPosFloor[i, j] = newInstance; } } } } }
public void DrawRooms(Subdungeon subdungeon) { //Instantiates the tiles on the tiles into the game based on the construction of the rooms from CreateRooms() if (subdungeon == null) { return; } if (subdungeon.isLeaf()) { for (int i = (int)subdungeon.room.x; i < subdungeon.room.xMax; i++) { for (int j = (int)subdungeon.room.y; j < subdungeon.room.yMax; j++) { int randIndex = Random.Range(0, floorTiles.Length); //pick a random tile from the array Vector3 pos = new Vector3(i, j, 0f); //the position where the tile will be placed GameObject newInstance = Instantiate(floorTiles[randIndex], pos, Quaternion.identity) as GameObject; newInstance.transform.SetParent(transform); boardPosFloor[i, j] = newInstance; } } } else { DrawRooms(subdungeon.left); DrawRooms(subdungeon.right); } }
public void CreateBSP(Subdungeon subdungeon) //Create a BinarySpacePartition from a given subdungeon { if (subdungeon.isLeaf()) { if (subdungeon.rect.width > maxRoomSize || subdungeon.rect.height > maxRoomSize || Random.Range(0.0f, 1.0f) > 0.25) { if (subdungeon.split(minRoomSize, maxRoomSize)) { CreateBSP(subdungeon.left); CreateBSP(subdungeon.right); } } } }
public bool split(int minRoomSize, int maxRoomsize) { if (!isLeaf()) { return(false); } bool splitT; if (rect.width / rect.height >= 1.25) { splitT = false; } else if (rect.height / rect.width >= 1.25) { splitT = true; } else { splitT = Random.Range(0.0f, 1.0f) > 0.5; } if (Mathf.Min(rect.height, rect.width) / 2 < minRoomSize) { return(false); } if (splitT) { int split = Random.Range(minRoomSize, (int)(rect.width - minRoomSize)); left = new Subdungeon(new Rect(rect.x, rect.y, rect.width, split)); right = new Subdungeon(new Rect(rect.x, rect.y + split, rect.width, rect.height - split)); } else { int split = Random.Range(minRoomSize, (int)(rect.height - minRoomSize)); left = new Subdungeon(new Rect(rect.x, rect.y, split, rect.height)); right = new Subdungeon(new Rect(rect.x + split, rect.y, rect.width - split, rect.height)); } return(true); }
public void CreateCorridor(Subdungeon left, Subdungeon right) { //picks a random position in each room and connect those points Rect rroom = right.GetRoom(); Rect lroom = left.GetRoom(); Vector2 leftPt = new Vector2((int)Random.Range(lroom.x + 1, lroom.xMax - 1), (int)Random.Range(lroom.y + 1, lroom.yMax - 1)); Vector2 rightPt = new Vector2((int)Random.Range(rroom.x + 1, rroom.xMax - 1), (int)Random.Range(rroom.y + 1, rroom.yMax - 1)); if (leftPt.x > rightPt.x) { Vector2 temp = leftPt; leftPt = rightPt; rightPt = temp; } int w = (int)(leftPt.x - rightPt.x); //do those random points have the same width? int h = (int)(leftPt.y - rightPt.y); //do those random points have the same height? if (w != 0) { if (Random.Range(0, 1) > 2) { corridors.Add(new Rect(leftPt.x, leftPt.y, Mathf.Abs(w) + 1, 1)); if (h < 0) { corridors.Add(new Rect(rightPt.x, leftPt.y, 1, Mathf.Abs(h))); } else { corridors.Add(new Rect(rightPt.x, leftPt.y, 1, -Mathf.Abs(h))); } } else { if (h < 0) { corridors.Add(new Rect(leftPt.x, leftPt.y, 1, Mathf.Abs(h))); } else { corridors.Add(new Rect(leftPt.x, rightPt.y, 1, Mathf.Abs(h))); } corridors.Add(new Rect(leftPt.x, rightPt.y, Mathf.Abs(w) + 1, 1)); } } else { if (h < 0) { corridors.Add(new Rect((int)leftPt.x, (int)leftPt.y, 1, Mathf.Abs(h))); } else { corridors.Add(new Rect((int)rightPt.x, (int)rightPt.y, 1, Mathf.Abs(h))); } } }