public bool IsIntersected(PcgRoom room) { PcgRoom r = new PcgRoom(); r.CreateRoom(room.mId, room.mX1 - mInterval, room.mY1 - mInterval, room.mWidth + (mInterval << 1), room.mHeight + (mInterval << 1)); return(mX1 < r.mX2 && mX2 > r.mX1 && mY1 < r.mY2 && mY2 > r.mY1); }
private void CreateOneRoom(PcgRoom room) { for (int i = room.mX1; i <= room.mX2; i++) { for (int j = room.mY1; j <= room.mY2; j++) { mapTerrain[i, j] = GridType.eEmpty; } } }
private void PlaceRooms(int maxTryNumber) { mRoomList.Clear(); int cnt = 0; int numbers = 0; pathNodesManager.transform.position = transform.position; pathNodesMgr.Initialize(pathNodesManager); while (cnt < roomCount) { int w = mRnd.Next(minRoomSize, maxRoomSize); int h = mRnd.Next(minRoomSize, maxRoomSize); // (mapWidth - 1) - w - 1 // max width index // - 1 is for the walls int x = mRnd.Next(mapWidth - 2 - w) + 1; int y = mRnd.Next(mapHeight - 2 - h) + 1; PcgRoom room = new PcgRoom(); room.CreateRoom(cnt, x, y, w, h); bool flag = false; foreach (PcgRoom r in mRoomList) { if (room.IsIntersected(r)) { flag = true; break; } } if (!flag) { // CreateOneRoom(room); int i = pathNodesMgr.AddNode(new Vector3(room.mCenterX, 0, room.mCenterY)); if (mRoomList.Count != 0) { PcgRoom prev = mRoomList[mRoomList.Count - 1]; if (mRnd.Next(2) == 1) { HoriCorridor(prev.mCenterX, room.mCenterX, prev.mCenterY); VertCorridor(prev.mCenterY, room.mCenterY, room.mCenterX); } else { VertCorridor(prev.mCenterY, room.mCenterY, room.mCenterX); HoriCorridor(prev.mCenterX, room.mCenterX, prev.mCenterY); } int j = pathNodesMgr.AddNode(new Vector3(room.mCenterX, 0, prev.mCenterY)); // int former = i - 2; // former = (former >= 0 ? former : 0); // pathNodesMgr.AddNeighbors(i, j); // pathNodesMgr.AddNeighbors(former, j); } mRoomList.Add(room); cnt++; } numbers++; // print(numbers); if (numbers >= maxTryNumber) { break; } } for (int i = 0; i < mRoomList.Count; i++) { CreateOneRoom(mRoomList[i]); } roomCount = cnt; print("Final Rooms Count: " + roomCount + "; Tried Times: " + numbers); }