public void SliceRoom(int curIterationTimes, RoomNode curRoomToSlice, List <RoomNode> roomsToReturn, Queue <RoomNode> queue, int minRoomWidth, int minRoomHeight, int lineWidth, int corridorWidth) { PartitionLine line = GetPartitionLine(curRoomToSlice, minRoomWidth, minRoomHeight, lineWidth); RoomNode left, right; borders.Add(line); if (line != null) { if (line.direction == Direction.Horizontal) { left = new RoomNode(curRoomToSlice, new Vector2Int(curRoomToSlice.bottomLeft.x, line.coordinates.y + lineWidth), curRoomToSlice.topRight); right = new RoomNode(curRoomToSlice, curRoomToSlice.bottomLeft, new Vector2Int(curRoomToSlice.topRight.x, line.coordinates.y - lineWidth)); } else /*if (line.direction == Direciton.Vertical)*/ { left = new RoomNode(curRoomToSlice, curRoomToSlice.bottomLeft, new Vector2Int(line.coordinates.x - lineWidth, curRoomToSlice.topRight.y) /*,curRoomToSlice.nodeIndex + 1*/); right = new RoomNode(curRoomToSlice, new Vector2Int(line.coordinates.x + lineWidth, curRoomToSlice.bottomLeft.y), curRoomToSlice.topRight /*, curRoomToSlice.nodeIndex + 2*/); } curRoomToSlice.leftChild = left; curRoomToSlice.rightChild = right; AddRoomToCollections(left, queue, roomsToReturn); AddRoomToCollections(right, queue, roomsToReturn); ConnectNeighborRooms(left, right, line, corridorWidth, minRoomWidth, minRoomHeight, lineWidth); } else { leafNodes.Add(curRoomToSlice); } if (curIterationTimes == iterationTimes - 1) { leafNodes.AddRange(roomsToSlice); } }
//private void ClearCollections() //{ // roomsToSlice.Clear(); // leafNodes.Clear(); // allNodes.Clear(); // borders.Clear(); //} //连接相邻房间 private void ConnectNeighborRooms(RoomNode left, RoomNode right, PartitionLine passage, int corridorWidth, int minRoomWidth, int minRoomHeight, int lineWidth) { Direction direction = passage.direction; List <Vector2Int> temp = new List <Vector2Int>(); if (direction == Direction.Horizontal) { int x; if (left.Width < 2 * minRoomWidth + 2 * lineWidth - 1) { x = seed.Next(left.bottomLeft.x + 1 + corridorWidth, right.topRight.x - corridorWidth); } else { x = (seed.Next(0, 2) == 0) ? seed.Next(left.bottomLeft.x + corridorWidth, left.bottomLeft.x + minRoomWidth - corridorWidth) : seed.Next(right.topRight.x - minRoomWidth + corridorWidth + 2, right.topRight.x - corridorWidth); } for (int y = right.topRight.y + 1; y < left.bottomLeft.y; y++) { for (int xOffset = x - corridorWidth; xOffset <= x + corridorWidth; xOffset++) { temp.Add(new Vector2Int(xOffset, y)); } } //gates.Add(new Vector2Int(x, left.bottomLeft.y)); //gates.Add(new Vector2Int(x, right.topRight.y)); } else if (direction == Direction.Vertical) { int y; if (left.Height < 2 * minRoomHeight + 2 * lineWidth - 1) { y = seed.Next(left.bottomLeft.y + 1 + corridorWidth, right.topRight.y - corridorWidth); } else { y = (seed.Next(0, 2) == 0) ? seed.Next(left.bottomLeft.y + 1 + corridorWidth, left.bottomLeft.y + minRoomHeight - corridorWidth) : seed.Next(right.topRight.y - minRoomHeight + corridorWidth + 1, right.topRight.y - corridorWidth); } for (int x = left.topRight.x + 1; x < right.bottomLeft.x; x++) { for (int yOffset = y - corridorWidth; yOffset <= y + corridorWidth; yOffset++) { temp.Add(new Vector2Int(x, yOffset)); } } //gates.Add(new Vector2Int(left.topRight.x, y)); //gates.Add(new Vector2Int(right.bottomLeft.x, y)); } gates.Add(temp[0]); gates.Add(temp[temp.Count - 1]); //temp.RemoveAt(0); //temp.RemoveAt(temp.Count - 1); Corridor corridor = new Corridor(temp, (direction == Direction.Horizontal)?Direction.Vertical:Direction.Horizontal); corridors.Add(corridor); }