//在roomA和roomB之间创建走廊 private void CreatePassage(CCaveRoom roomA, CCaveRoom roomB, Vector2Int tileA, Vector2Int tileB) { CCaveRoom.ConnectRooms(roomA, roomB); List <Vector2Int> line = GetLine(tileA, tileB); foreach (Vector2Int c in line) { DrawCircle(c, 5); } }
//连接相邻的距离最近的room, 并且在相邻的房子中间创建走路 private void ConnectClosestRooms(List <CCaveRoom> allRooms, bool forceAccessibilityFromMainRoom = false) { m_survivingRooms.Sort(); m_survivingRooms[0].isMainRoom = true; m_survivingRooms[0].isAccessibleFromMainRoom = true; List <CCaveRoom> roomListA = new List <CCaveRoom>(); List <CCaveRoom> roomListB = new List <CCaveRoom>(); if (forceAccessibilityFromMainRoom) { foreach (CCaveRoom room in allRooms) { if (room.isAccessibleFromMainRoom) { roomListB.Add(room); } else { roomListA.Add(room); } } } else { roomListA = allRooms; roomListB = allRooms; } int bestDistance = 0; Vector2Int bestTileA = new Vector2Int(); Vector2Int bestTileB = new Vector2Int(); CCaveRoom bestRoomA = new CCaveRoom(); CCaveRoom bestRoomB = new CCaveRoom(); bool possibleConnectionFound = false; foreach (CCaveRoom roomA in roomListA) { if (!forceAccessibilityFromMainRoom) { possibleConnectionFound = false; if (roomA.connectedRooms.Count > 0) { continue; } } foreach (CCaveRoom roomB in roomListB) { if (roomA == roomB || roomA.IsConnected(roomB)) { continue; } for (int tileIndexA = 0; tileIndexA < roomA.edgeTiles.Count; tileIndexA++) { for (int tileIndexB = 0; tileIndexB < roomB.edgeTiles.Count; tileIndexB++) { Vector2Int tileA = roomA.edgeTiles[tileIndexA]; Vector2Int tileB = roomB.edgeTiles[tileIndexB]; int distanceBetweenRooms = (int)(Mathf.Pow(tileA.x - tileB.x, 2) + Mathf.Pow(tileA.y - tileB.y, 2)); if (distanceBetweenRooms < bestDistance || !possibleConnectionFound) { bestDistance = distanceBetweenRooms; possibleConnectionFound = true; bestTileA = tileA; bestTileB = tileB; bestRoomA = roomA; bestRoomB = roomB; } } } } if (possibleConnectionFound && !forceAccessibilityFromMainRoom) { CreatePassage(bestRoomA, bestRoomB, bestTileA, bestTileB); } } if (possibleConnectionFound && forceAccessibilityFromMainRoom) { CreatePassage(bestRoomA, bestRoomB, bestTileA, bestTileB); ConnectClosestRooms(allRooms, true); } if (!forceAccessibilityFromMainRoom) { ConnectClosestRooms(allRooms, true); } }