LabyrinthRoom BuildRoom(Vector3 location, float diameter) { LabyrinthRoom newRoom = new LabyrinthRoom(location, diameter); unfinishedRoomList.Add(newRoom); GameObject newRoomBoundry = Instantiate(roomBoundryPrefab, location, Quaternion.identity); newRoomBoundry.transform.localScale = new Vector3(diameter, diameter, 1f); newRoomBoundry.transform.parent = this.transform; return(newRoom); }
//Build a labyrinth room by room. Pick a random room, and explore a random direction from that room. //Once every direction has been explored from a room (and either been extended, or discarded), remove //that room from the pool of unfinished rooms. void BuildLabyrinth() { //Build a starting room BuildRoom(new Vector3(1f, 0f, 0f), roomDiameter * 2 + Random.Range(0f, diamaterVariation)); //While there are unfinished rooms while (unfinishedRoomList.Count != 0 && finishedRoomList.Count < 200) { int nextRoomIndex = Random.Range(0, unfinishedRoomList.Count); LabyrinthRoom testingRoom = unfinishedRoomList [nextRoomIndex]; // print ("Testing room number " + nextRoomIndex.ToString()); //If this room has remaining directions, pick a random direction to try and extend if (testingRoom.HasUnexploredDirection()) { MovementDirection exploringDirection = testingRoom.PopRandomUnexploredDirection(); // print ("Testing direction " + exploringDirection.ToString ()); Vector3 exploringVector = testingRoom.DirectionToVector(exploringDirection); float proposedDiameter = roomDiameter + Random.Range(0f, diamaterVariation); //Ensure that the separation radius is always sufficient to prevent the new room overlapping the current room. float randomRoomSeparation = (roomSeparation + Random.Range(0f, separationVariation)); if (randomRoomSeparation < (proposedDiameter / 2f + testingRoom.GetRadius())) { randomRoomSeparation = (proposedDiameter / 2f + testingRoom.GetRadius() + 2f); } Vector3 proposedLocation = testingRoom.GetPosition() + exploringVector * randomRoomSeparation; if (TestProposedLocation(proposedLocation, proposedDiameter)) { LabyrinthRoom newRoom = BuildRoom(proposedLocation, proposedDiameter); BuildCorridor(newRoom.GetIncomingConnectionPoint(exploringDirection), testingRoom.GetOutgoingConnectionPoint(exploringDirection)); } } else //Otherwise, add to finished room list, and remove from unfinished room list. { finishedRoomList.Add(testingRoom); unfinishedRoomList.RemoveAt(nextRoomIndex); } } }
/// <summary> /// Absorbs the cells of another room. /// </summary> /// <param name="otherRoom">Other room</param> public void AbsorbRoom(LabyrinthRoom otherRoom) { for (int i = 0; i < otherRoom.cells.Count; i++) this.Add(otherRoom.cells[i]); }
/// <summary> /// Initializes the cell with a specific room. /// </summary> /// <param name="r">Room</param> public void InitializeCell(LabyrinthRoom r) { r.Add(this); }