private ExplorableSection PlaceRoom(ExplorableSection.RoomType connectingType, GameObject exitToConnectTo) { GameObject connectableRoom = null; ExplorableSection connectableRoomExplorable = null; List <int> roomIdxToAttempt = GetRandomRoomIdx(connectingType); for (int i = 0; i < roomIdxToAttempt.Count; i++) { connectableRoom = instantiatedRooms[connectingType][roomIdxToAttempt[i]]; connectableRoomExplorable = connectableRoom.GetComponent <ExplorableSection>(); connectableRoomExplorable.AttachToExit(exitToConnectTo); ReplenishRoomPool(connectableRoom, connectingType, roomIdxToAttempt[i]); if (DoesRoomOverlap(connectableRoom)) { Destroy(connectableRoom); connectableRoom = null; connectableRoomExplorable = null; } else { createdRooms.Add(connectableRoom); break; } } return(connectableRoomExplorable); }
private void ReplenishRoomPool(GameObject usedRoom, ExplorableSection.RoomType connectingType, int connectingTypeIdx) { GameObject replacementRoom = Instantiate(rooms[createdRoomToRoomIdx[usedRoom]], transform.position, Quaternion.identity); createdRoomToRoomIdx[replacementRoom] = createdRoomToRoomIdx[usedRoom]; createdRoomToRoomIdx.Remove(usedRoom); instantiatedRooms[connectingType][connectingTypeIdx] = replacementRoom; }
public List <GameObject> BuildRooms() { // playerStartPoint is the side of the room that the player starts in, currently it should only support // east or west (as that is what the hallway is by default). Later this should be changed to allow the starting // room type to be rotated as the game call for it ExplorableSection currentRoomExplorable = startingRoom.GetComponent <ExplorableSection>(); List <GameObject> pendingExits = new List <GameObject>(); for (int i = 0; i < currentRoomExplorable.Exits.Length; i++) { if (currentRoomExplorable.Exits[i] != playerStartPoint) { pendingExits.Add(currentRoomExplorable.Exits[i]); } } createdRooms.Add(startingRoom); for (int i = 0; i < buildIterations; i++) { List <GameObject> newExits = new List <GameObject>(); for (int j = 0; j < pendingExits.Count; j++) { ExplorableSectionExit sectionExit = pendingExits[j].GetComponent <ExplorableSectionExit>(); ExplorableSection.RoomType connectingType = sectionExit.ConnectableRoomTypes[Random.Range(0, sectionExit.ConnectableRoomTypes.Length)]; currentRoomExplorable = PlaceRoom(connectingType, pendingExits[j]); if (currentRoomExplorable != null) { for (int k = 0; k < currentRoomExplorable.Exits.Length; k++) { if (currentRoomExplorable.RoomAttachedExitLocation != currentRoomExplorable.Exits[k]) { newExits.Add(currentRoomExplorable.Exits[k]); } } } } pendingExits = newExits; } RemoveRoomBuildingPool(); return(createdRooms); }
private List <int> GetRandomRoomIdx(ExplorableSection.RoomType roomType) { List <int> randomizedRoomIdx = new List <int>(); for (int i = 0; i < instantiatedRooms[roomType].Count; i++) { randomizedRoomIdx.Add(i); } int roomIdxCount = randomizedRoomIdx.Count; while (roomIdxCount > 1) { roomIdxCount--; int i = Random.Range(0, roomIdxCount); int val = randomizedRoomIdx[i]; randomizedRoomIdx[i] = randomizedRoomIdx[roomIdxCount]; randomizedRoomIdx[roomIdxCount] = val; } return(randomizedRoomIdx); }