public void ConnectRoom(AP_Room r) { Vector2 connectDir = r.GetUnitPos() - GetUnitPos(); if (connectDir.magnitude != 1) { print("Rooms not orthogonally adjacent, connect failed"); return; } ConnectRoom(connectDir); r.ConnectRoom(-connectDir); }
AP_Room ExpandRoom() { if (ExpandingDoors.Count > 0) { Door nextDoor = ExpandingDoors[0]; // get most recent door from expanding doors list ExpandingDoors.RemoveAt(0); // removing door from list makes sure it's not used again AP_Room newRoom = CreateRoom(nextDoor.newPos); // place room based on door retrieved AP_Room originRoom = GetRoomAtPos(nextDoor.origin); // get origin of nextDoor, then find that origin room in dungeon rooms originRoom.ConnectRoom(newRoom); // join both rooms with a door edge return(newRoom); } else { print("NO DOORS TO EXPAND FROM"); return(null); } }
void BuildBigRoom(bool isEndRoom) { Vector2 origin = DungeonRooms[DungeonRooms.Count - 1].GetUnitPos(); // get unit position of last room in dungeon room list to know the previous room to big room Vector2[] dirs = GetRandomDirections(); Vector2[] sectionOffsets = { Vector2.zero, Vector2.right, new Vector2(1, 1), Vector2.up }; // offsets of four rooms composing the big room with 0,0 as the bottom left room section Vector2[] sectionPositions = new Vector2[4]; bool isValid = true; int curDirIndex = 0; int offset = 0; // stores random offset of big room from origin room offset = (Random.value > 0.5) ? 0 : -1; // for now as big rooms are 2x2, either no offset, or -1 offset are possible int attempts = 0; // used to track attempt iterations to place big room, to use both possible offsets do { isValid = true; Vector2 curDir = dirs[curDirIndex]; if (dirs[curDirIndex] == mNorth) { sectionPositions[0] = origin + curDir + new Vector2(offset, 0); } else if (dirs[curDirIndex] == mEast) { sectionPositions[0] = origin + curDir + new Vector2(0, offset); } else if (dirs[curDirIndex] == mSouth) { sectionPositions[0] = origin + curDir + new Vector2(offset, -1); } else // dir[curDirIndex] must equal mWest { sectionPositions[0] = origin + curDir + new Vector2(-1, offset); } for (int i = 1; i < 4; i++) // using 0,0 as bottom left, setup other section positions { sectionPositions[i] = sectionPositions[0] + sectionOffsets[i]; } // iterate through vector2 array. if all positions are free space, continue for (int r = 0; r < 4; r++) { if (!isSpaceAvailable(sectionPositions[r])) { isValid = false; curDirIndex++; if (attempts == 0) // swap the offset and try again { curDirIndex = 0; attempts = 1; offset = (offset == 0) ? -1 : 0; } } } } while (!isValid && curDirIndex < 4); // repeat trying different positioning of big room until valid position is found if (curDirIndex > 3) //reload level if big room fails to generate in current conditions { SceneManager.LoadScene(SceneManager.GetActiveScene().name); // Application.LoadLevel(Application.loadedLevelName); print("RELOAD LEVEL " + ((isEndRoom) ? "END ROOM" : "MID ROOM") + " FAILED"); // do { // } while(Application.isLoadingLevel); return; // return from this function so no other lines in this function run, this should theoretically be unnecessary? } for (int i = 0; i < 4; i++) // iterate through array again, generating a room at each chosen unit position { AP_Room r = CreateRoom(sectionPositions[i]); r.SetRoomType(((isEndRoom) ? AP_Room.RoomType.end : AP_Room.RoomType.mid)); // consider expanding rooms of big room using add door to end, to enable potential branching from big room } // merge the four created rooms AP_Room[] otherRooms = new AP_Room[3]; for (int i = 0; i < 3; i++) { otherRooms [i] = GetRoomAtPos(sectionPositions [i + 1]); } GetRoomAtPos(sectionPositions [0]).MergeRoom(otherRooms); //GetRoomAtPos (sectionPositions [0]).MergeRoom ( GetRoomAtPos (sectionPositions [1]), // GetRoomAtPos (sectionPositions [2]), // GetRoomAtPos (sectionPositions [3])); /*GetRoomAtPos(sectionPositions[0]).MergeRoom(GetRoomAtPos(sectionPositions[1])); * GetRoomAtPos(sectionPositions[0]).MergeRoom(GetRoomAtPos(sectionPositions[3])); * GetRoomAtPos(sectionPositions[2]).MergeRoom(GetRoomAtPos(sectionPositions[1])); * GetRoomAtPos(sectionPositions[2]).MergeRoom(GetRoomAtPos(sectionPositions[3])); */ AP_Room entryRoom = GetRoomAtPos(origin); AP_Room midRoomNode = GetRoomAtPos(origin + dirs[curDirIndex]); entryRoom.ConnectRoom(midRoomNode); // connect this big room with the last main path room generated if (!isEndRoom) { // choose a valid direction and room for the main path to continue on from int entryDoor = 0; int exitDoor = 0; for (int i = 0; i < 4; i++) { if (Vector2.Distance(origin, sectionPositions[i]) == 1) { entryDoor = i; } } exitDoor = (entryDoor + 2 > 3) ? (entryDoor + 1) % 3 : entryDoor + 2; // print("entry door = " + entryDoor + " exit door = " + exitDoor); for (int i = 0; i < 4; i++) { if (i != exitDoor) { ExpandMainPathDoors(GetRoomAtPos(sectionPositions[i])); } } ExpandMainPathDoors(GetRoomAtPos(sectionPositions[exitDoor])); } }