IEnumerator CreateCorridor(int roomNumber, int adjacentRoomPosition) { //The RoomGenerator script associated with this room RoomGenerator roomGen = gridPoints[roomNumber].GetComponent <RoomGenerator>(); //If we have already spawned a corridor in this position, do nothing if (roomGen.CheckForCorridor(adjacentRoomPosition)) { yield break; } int doorPosition = 0; int otherRoomDoorPosition = 0; Vector3 spawnPos = Vector3.zero; Vector3 spawnRot = Vector3.zero; //The RoomGenrator script of the room we are connecting to RoomGenerator connectingRoomGen; //The position that this room is in relative to the room we are connecing with int roomPositionRelativeToConnectingRoom = 4; //The grid point which the corridor will be in (if it's a diagonal connection) int corridorPosition = -1; //Which position and rotation to create the corridor in. Also gets the RoomGenerator script of the connecting room switch (adjacentRoomPosition) { //Top Left case 0: if (roomBool[roomNumber - 1] && roomBool[roomNumber - levelGridSize.x]) { yield break; } if (!roomBool[roomNumber - 1] && !roomBool[roomNumber - levelGridSize.x]) { if (Roll(50)) { //From Left spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (29) - Vector3.right * 36); spawnRot = Vector3.up * 180; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 11; otherRoomDoorPosition = 6; corridorPosition = roomNumber - 1; } else { //From Top spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (21) - Vector3.right * 14); spawnRot = Vector3.zero; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 0; otherRoomDoorPosition = 5; corridorPosition = roomNumber - levelGridSize.x; } } if (roomBool[roomNumber - 1] && !roomBool[roomNumber - levelGridSize.x]) { //From Top spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (21) - Vector3.right * 14); spawnRot = Vector3.zero; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 0; otherRoomDoorPosition = 5; corridorPosition = roomNumber - levelGridSize.x; } if (!roomBool[roomNumber - 1] && roomBool[roomNumber - levelGridSize.x]) { //From Left spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (29) - Vector3.right * 36); spawnRot = Vector3.up * 180; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 11; otherRoomDoorPosition = 6; corridorPosition = roomNumber - 1; } connectingRoomGen = gridPoints[roomNumber - levelGridSize.x - 1].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 8; break; //Top Mid case 1: spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (21) - Vector3.right * 4); spawnRot = Vector3.zero; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 1; otherRoomDoorPosition = 7; connectingRoomGen = gridPoints[roomNumber - levelGridSize.x].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 7; break; //Top Right case 2: if (roomBool[roomNumber + 1] && roomBool[roomNumber - levelGridSize.x]) { yield break; } if (!roomBool[roomNumber + 1] && !roomBool[roomNumber - levelGridSize.x]) { if (Roll(50)) { //From Right spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (14) + Vector3.right * 21); spawnRot = Vector3.up * 90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 3; otherRoomDoorPosition = 8; corridorPosition = roomNumber + 1; } else { //From Top spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (36) + Vector3.right * 29); spawnRot = Vector3.up * -90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 2; otherRoomDoorPosition = 9; corridorPosition = roomNumber - levelGridSize.x; } } if (roomBool[roomNumber + 1] && !roomBool[roomNumber - levelGridSize.x]) { //From Top spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (36) + Vector3.right * 29); spawnRot = Vector3.up * -90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 2; otherRoomDoorPosition = 9; corridorPosition = roomNumber - levelGridSize.x; } if (!roomBool[roomNumber + 1] && roomBool[roomNumber - levelGridSize.x]) { //From Right spawnPos = gridPoints[roomNumber].transform.position + (Vector3.forward * (14) + Vector3.right * 21); spawnRot = Vector3.up * 90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 3; otherRoomDoorPosition = 8; corridorPosition = roomNumber + 1; } connectingRoomGen = gridPoints[roomNumber - levelGridSize.x + 1].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 6; break; //Left case 3: spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.right * (21) - Vector3.forward * 4); spawnRot = Vector3.up * -90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 10; otherRoomDoorPosition = 4; connectingRoomGen = gridPoints[roomNumber - 1].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 5; break; //Right case 5: spawnPos = gridPoints[roomNumber].transform.position + (Vector3.right * (21) + Vector3.forward * 4); spawnRot = Vector3.up * 90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 4; otherRoomDoorPosition = 10; connectingRoomGen = gridPoints[roomNumber + 1].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 3; break; case 6: //Bottom Left if (roomBool[roomNumber - 1] && roomBool[roomNumber + levelGridSize.x]) { yield break; } if (!roomBool[roomNumber - 1] && !roomBool[roomNumber + levelGridSize.x]) { if (Roll(50)) { //From Left spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (14) - Vector3.right * 21); spawnRot = Vector3.up * -90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 9; otherRoomDoorPosition = 2; corridorPosition = roomNumber - 1; } else { //From Bottom spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (36) - Vector3.right * 29); spawnRot = Vector3.up * 90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 8; otherRoomDoorPosition = 3; corridorPosition = roomNumber + levelGridSize.x; } } if (roomBool[roomNumber - 1] && !roomBool[roomNumber + levelGridSize.x]) { //From Bottom spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (36) - Vector3.right * 29); spawnRot = Vector3.up * 90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 8; otherRoomDoorPosition = 3; corridorPosition = roomNumber + levelGridSize.x; } if (!roomBool[roomNumber - 1] && roomBool[roomNumber + levelGridSize.x]) { //From Left spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (14) - Vector3.right * 21); spawnRot = Vector3.up * -90; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 9; otherRoomDoorPosition = 2; corridorPosition = roomNumber - 1; } connectingRoomGen = gridPoints[roomNumber + levelGridSize.x - 1].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 2; break; //Bottom Mid case 7: spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (21) + Vector3.right * 4); spawnRot = Vector3.up * 180; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 7; otherRoomDoorPosition = 1; connectingRoomGen = gridPoints[roomNumber + levelGridSize.x].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 1; break; case 8: //Bottom Right if (roomBool[roomNumber + 1] && roomBool[roomNumber + levelGridSize.x]) { yield break; } if (!roomBool[roomNumber + 1] && !roomBool[roomNumber + levelGridSize.x]) { if (Roll(50)) { //From Right spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (29) + Vector3.right * 36f); spawnRot = Vector3.zero; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 5; otherRoomDoorPosition = 0; corridorPosition = roomNumber + 1; } else { //From Bottom spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (21) + Vector3.right * 14); spawnRot = Vector3.up * 180; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 6; otherRoomDoorPosition = 11; corridorPosition = roomNumber + levelGridSize.x; } } if (roomBool[roomNumber + 1] && !roomBool[roomNumber + levelGridSize.x]) { //From Bottom spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (21) + Vector3.right * 14); spawnRot = Vector3.up * 180; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 6; otherRoomDoorPosition = 11; corridorPosition = roomNumber + levelGridSize.x; } if (!roomBool[roomNumber + 1] && roomBool[roomNumber + levelGridSize.x]) { //From Right spawnPos = gridPoints[roomNumber].transform.position + (-Vector3.forward * (29) + Vector3.right * 36f); spawnRot = Vector3.zero; //Int to send PlaceDoorway function so it knows what position to place the doorway doorPosition = 5; otherRoomDoorPosition = 0; corridorPosition = roomNumber + 1; } connectingRoomGen = gridPoints[roomNumber + levelGridSize.x + 1].GetComponent <RoomGenerator>(); roomPositionRelativeToConnectingRoom = 0; break; default: spawnPos = Vector3.zero; spawnRot = Vector3.zero; connectingRoomGen = gridPoints[0].GetComponent <RoomGenerator>(); break; } //Spawning corridor GameObject corridor; if (adjacentRoomPosition == 1 || adjacentRoomPosition == 3 || adjacentRoomPosition == 5 || adjacentRoomPosition == 7) { corridor = Instantiate(roomGen.corridorStraightShort, spawnPos, Quaternion.Euler(spawnRot), gridPoints[roomNumber].transform); } else { corridor = Instantiate(roomGen.corridorCorner, spawnPos, Quaternion.Euler(spawnRot), gridPoints[roomNumber].transform); } //Adding corridor to list corridorList.Add(corridor); //Telling the RoomGenerator script of this room that there now exists a connection to another room and storing GO in array roomGen.SetRoomConnections(adjacentRoomPosition, true); roomGen.SetCorridors(corridor, doorPosition); //Telling the RoomGenerator script of the other room that there now exists a connection to this room and storing GO in array connectingRoomGen.SetRoomConnections(roomPositionRelativeToConnectingRoom, true); connectingRoomGen.SetCorridors(corridor, otherRoomDoorPosition); if (corridorPosition >= 0) { //Setting corridor bool on relevant grid point corridorBool[corridorPosition] = true; } //Calling door generating function doorList.Add(roomGen.PlaceDoorway(doorPosition)); doorList.Add(connectingRoomGen.PlaceDoorway(otherRoomDoorPosition)); }
void RadialCheck() { List <int> toBeRemoved = new List <int>(); List <int> toBeAdded = new List <int>(); foreach (int roomNumber in activeRooms) { int noOfRoomsGenerated = 0; for (int i = 0; i < 9; i++) { //If we have reached desired number of rooms, end if (totalNoOfRoomsGenerated >= totalNoOfRooms) { break; } RoomGenerator roomGen = gridPoints[roomNumber].GetComponent <RoomGenerator>(); bool canGenerateRoom = true; //The address of the other room in the gridPoints array int otherRoom = -1; //The position of this room relative to the newly generated room int j = -1; //If the room generator script says this position isn't valid, end if (roomGen.validRGPositions[i]) { //Setting otherRoom and j variables depending on room position #region switch (i) { case 0: //Making sure a room doesn't get created diagonally if 2 already exist either side of that position if (roomBool[roomNumber - 1] && roomBool[roomNumber - levelGridSize.x]) { canGenerateRoom = false; } if (roomNumber - levelGridSize.x - 1 > 0 && roomNumber % levelGridSize.x != 0) { otherRoom = roomNumber - levelGridSize.x - 1; j = 8; } break; case 1: if (roomNumber - levelGridSize.x > 0) { otherRoom = roomNumber - levelGridSize.x; j = 7; } break; case 2: //Making sure a room doesn't get created diagonally if 2 already exist either side of that position if (roomBool[roomNumber + 1] && roomBool[roomNumber - levelGridSize.x]) { canGenerateRoom = false; } if (roomNumber - levelGridSize.x + 1 > 0 && roomNumber % levelGridSize.x != levelGridSize.x - 1) { otherRoom = roomNumber - levelGridSize.x + 1; j = 6; } break; case 3: if (roomNumber - 1 > 0 && roomNumber % levelGridSize.x != 0) { otherRoom = roomNumber - 1; j = 5; } break; case 5: if (roomNumber + 1 < gridPoints.Length && roomNumber % levelGridSize.x != levelGridSize.x - 1) { otherRoom = roomNumber + 1; j = 3; } break; case 6: //Making sure a room doesn't get created diagonally if 2 already exist either side of that position if (roomBool[roomNumber - 1] && roomBool[roomNumber + levelGridSize.x]) { canGenerateRoom = false; } if (roomNumber + levelGridSize.x - 1 < gridPoints.Length && roomNumber % levelGridSize.x != 0) { otherRoom = roomNumber + levelGridSize.x - 1; j = 2; } break; case 7: if (roomNumber + levelGridSize.x < gridPoints.Length) { otherRoom = roomNumber + levelGridSize.x; j = 1; } break; case 8: //Making sure a room doesn't get created diagonally if 2 already exist either side of that position if (roomBool[roomNumber + 1] && roomBool[roomNumber + levelGridSize.x]) { canGenerateRoom = false; } if (roomNumber + levelGridSize.x + 1 < gridPoints.Length && roomNumber % levelGridSize.x != levelGridSize.x - 1) { otherRoom = roomNumber + levelGridSize.x + 1; j = 0; } break; default: break; } #endregion //If valid room was selected if (otherRoom >= 0 && j >= 0 && otherRoom != 4) { RoomGenerator otherRoomGen = gridPoints[otherRoom].GetComponent <RoomGenerator>(); if (roomBool[otherRoom]) { //Telling this room that there is a room in the i position roomGen.SetAdjacentRoomBool(i, true); //Telling other room that there is a room in the j position otherRoomGen.SetAdjacentRoomBool(j, true); } if (Roll(chanceToCreateRoom) && canGenerateRoom) { bool createdCorridor = false; //If there is no room or corridor occupying this space if (!roomBool[otherRoom] && !corridorBool[otherRoom]) { //Generating room in position i GenerateRoom(otherRoom, j); roomBool[otherRoom] = true; //Telling this room that there is a room in the i position roomGen.SetAdjacentRoomBool(i, true); //Telling other room that there is a room in the j position otherRoomGen.SetAdjacentRoomBool(j, true); //Choosing the shape of the room otherRoomGen.ChooseSmallRoomLayout(j); //Setting distance from start otherRoomGen.SetDistanceFromStart(roomGen.GetDistanceFromStart() + 1); //Create Corridor to new room StartCoroutine(CreateCorridor(roomNumber, i)); createdCorridor = true; //Storing this room number to be added later to active rooms toBeAdded.Add(otherRoom); totalNoOfRoomsGenerated++; //Note room has been generated noOfRoomsGenerated++; } //Generate corridor if room already exists if (!roomGen.CheckForCorridor(i) && roomBool[otherRoom] && !createdCorridor) { if (Roll(chanceToCreateCorridor) && otherRoomGen.validRGPositions[j]) { StartCoroutine(CreateCorridor(roomNumber, i)); //Set the new distances from start if (otherRoomGen.GetDistanceFromStart() < roomGen.GetDistanceFromStart()) { roomGen.SetDistanceFromStart(otherRoomGen.GetDistanceFromStart() + 1); } else { otherRoomGen.SetDistanceFromStart(roomGen.GetDistanceFromStart() + 1); } } } } } } } //If this room has generated another room, replace it on the active list with the new room(s) if (noOfRoomsGenerated > 0) { toBeRemoved.Add(roomNumber); } } //Removing rooms from active list and adding to passive foreach (int room in toBeRemoved.ToArray()) { activeRooms.Remove(room); existingRooms.Add(room); } toBeRemoved.Clear(); //Adding newly generated rooms to active list foreach (int room in toBeAdded.ToArray()) { activeRooms.Add(room); } toBeAdded.Clear(); }