private GameObject FindRandomRoomWithExitAt(Dictionary <Direction, bool> directions, int x, int y) { //if there is no exit the new room can be an exit bool canBeExit = !exitSet; // return all possible Rooms at this point GameObject[] rooms = Array.FindAll(roomPrefabs, it => { RoomProperties roomProperties = it.GetComponent <RoomProperties>(); bool isEntrance = roomProperties.isEntrance; //bool hasCorrectExit = directions.All(direction => roomProperties.GetExitDirections ()[direction]); bool hasCorrectExit = true; foreach (var entry in directions) { if (roomProperties.GetExitDirections().Contains(entry) == false) { hasCorrectExit = false; } } bool isExit = roomProperties.isExit; return(!isEntrance && hasCorrectExit && (canBeExit || !isExit)); }); //the rooms are sorted by number of exits GameObject[] sortedRooms = sortRoomsByNumeberofExits(rooms); int numberOfRolls = Math.Max(1, (Mathf.Abs(x) + Mathf.Abs(y)) / levelSize); int lowestRoomIndex = int.MaxValue; for (int i = 0; i < numberOfRolls; i++) { lowestRoomIndex = Math.Min(lowestRoomIndex, UnityEngine.Random.Range(0, rooms.Length)); } if (doneRooms.Count > levelSize * 2) { lowestRoomIndex = 0; } if (sortedRooms[lowestRoomIndex].GetComponent <RoomProperties>().isExit) { exitSet = true; } var chosenRoomPrefab = sortedRooms[lowestRoomIndex]; var chosenRoom = Instantiate(chosenRoomPrefab); return(chosenRoom); }
private GameObject doRoom(GameObject room, int x, int y) { RoomProperties roomProperties = room.GetComponent <RoomProperties>(); RoomBehaviour roomBehaviour = room.GetComponent <RoomBehaviour>(); foreach (var exit in roomProperties.GetExitDirections()) { if (exit.Value) { Dictionary <Direction, bool> directions = new Dictionary <Direction, bool>(); //change var currentX = x; var currentY = y; if (exit.Key == Direction.NORTH) { currentY++; } else if (exit.Key == Direction.SOUTH) { currentY--; } else if (exit.Key == Direction.EAST) { currentX++; } else if (exit.Key == Direction.WEST) { currentX--; } if (IsRoom(currentX, currentY)) { continue; } if (IsRoom(currentX - 1, currentY)) { if (HasExit(currentX - 1, currentY, Direction.EAST)) { directions.Add(Direction.WEST, true); } else { directions.Add(Direction.WEST, false); } } if (IsRoom(currentX + 1, currentY)) { if (HasExit(currentX + 1, currentY, Direction.WEST)) { directions.Add(Direction.EAST, true); } else { directions.Add(Direction.EAST, false); } } if (IsRoom(currentX, currentY - 1)) { if (HasExit(currentX, currentY - 1, Direction.NORTH)) { directions.Add(Direction.SOUTH, true); } else { directions.Add(Direction.SOUTH, false); } } if (IsRoom(currentX, currentY + 1)) { if (HasExit(currentX, currentY + 1, Direction.SOUTH)) { directions.Add(Direction.NORTH, true); } else { directions.Add(Direction.NORTH, false); } } var connectingRoom = FindRandomRoomWithExitAt(directions, currentX, currentY); todoRooms.Add(connectingRoom); AddRoom(connectingRoom, currentX, currentY); } } todoRooms.Remove(room); return(room); }