public void FindAndConnectClosestEdges(List<Dictionary<int, List<Partition>>> allRoomPartitions, float hallwayWidth, DungeonPartitionData data) { List<Edge> connectedEdges = new List<Edge>(); // List to track e foreach (var roomDict in allRoomPartitions) { foreach (var roomEntry in roomDict) { foreach (var partition in roomEntry.Value) { Vector2 centroid = partition.GetCentroid(); foreach (var edge in partition.Edges) { if (edge.Length < hallwayWidth) continue; // Skip edges that are too short to handle a hallway Vector2 normal = edge.GetOutwardNormal(centroid); DrawEdgeWithNormal(edge, centroid, normal); Edge closestEdge = null; float minDistance = float.MaxValue; Vector2 bestConnectionPoint = Vector2.zero; foreach (var otherRoomEntry in roomDict) { if (otherRoomEntry.Key == roomEntry.Key) continue; foreach (var otherPartition in otherRoomEntry.Value) { foreach (var otherEdge in otherPartition.Edges) { if (AreEdgesFacing(normal, otherEdge.GetOutwardNormal(otherPartition.GetCentroid())) && edge != otherEdge && !connectedEdges.Contains(otherEdge)) { Vector2 dir = (otherEdge.Midpoint - edge.Midpoint).normalized; if (Vector2.Dot(dir, normal) > 0) // Ensures the direction is not behind the normal { if (IsLineOfSightClear(edge.Midpoint, otherEdge.Midpoint, allRoomPartitions, edge)) { float distance = Vector2.Distance(edge.Midpoint, otherEdge.Midpoint); if (distance < minDistance) { minDistance = distance; closestEdge = otherEdge; bestConnectionPoint = otherEdge.Midpoint; } } } } } } } if (closestEdge != null) { float slideDistance = hallwayWidth / 2; connectedEdges.Add(edge); connectedEdges.Add(closestEdge); Vector2 rayEndPoint; Vector2 intersectionPoint; bool hitMainTarget, hitStartTarget, hitEndTarget; Vector2 midpoint = edge.Midpoint; Vector2 adjustedSourceQuarterToEndPoint = Vector2.zero; Vector2 adjustedSourceQuarterToStartPoint = Vector2.zero; Vector2 adjustedTargetQuarterToEndPoint = Vector2.zero; Vector2 adjustedTargetQuarterToStartPoint = Vector2.zero; do { // Midpoint raycast rayEndPoint = midpoint + normal * 100; hitMainTarget = LineIntersectsEdge(midpoint, rayEndPoint, closestEdge, out intersectionPoint) && IsPointOnEdge(intersectionPoint, closestEdge); DrawXAtPosition(intersectionPoint, hitMainTarget ? Color.blue : Color.red); // Slide towards the start and raycast Vector2 slideStartPoint = Vector2.Lerp(midpoint, edge.Start, slideDistance / edge.Length); rayEndPoint = slideStartPoint + normal * 100; hitStartTarget = LineIntersectsEdge(slideStartPoint, rayEndPoint, closestEdge, out intersectionPoint) && IsPointOnEdge(intersectionPoint, closestEdge); DrawXAtPosition(intersectionPoint, hitStartTarget ? Color.cyan : Color.red); if (hitStartTarget) adjustedTargetQuarterToStartPoint = intersectionPoint; // Assign start to quarter point if (hitStartTarget) adjustedSourceQuarterToStartPoint = slideStartPoint; // Assign start to quarter point // Slide towards the end and raycast Vector2 slideEndPoint = Vector2.Lerp(midpoint, edge.End, slideDistance / edge.Length); rayEndPoint = slideEndPoint + normal * 100; hitEndTarget = LineIntersectsEdge(slideEndPoint, rayEndPoint, closestEdge, out intersectionPoint) && IsPointOnEdge(intersectionPoint, closestEdge); DrawXAtPosition(intersectionPoint, hitEndTarget ? Color.magenta : Color.red); if (hitEndTarget) adjustedTargetQuarterToEndPoint = intersectionPoint; // Assign end to quarter point if (hitEndTarget) adjustedSourceQuarterToEndPoint = slideEndPoint; // Assign end to quarter point // Step towards target midpoint if any raycasts missed if (!hitMainTarget || !hitStartTarget || !hitEndTarget) { midpoint = Vector2.Lerp(midpoint, bestConnectionPoint, 0.5f); Debug.Log("Stepping towards new midpoint: " + midpoint); } } while ((!hitMainTarget || !hitStartTarget || !hitEndTarget) && Vector2.Distance(midpoint, bestConnectionPoint) > 0.01); // Error handling if alignment fails if (!hitMainTarget || !hitStartTarget || !hitEndTarget) { Debug.LogError("Failed to align raycasts satisfactorily at midpoint: " + midpoint); } else { List<Vector2> vertices = new List<Vector2>
adjustedSourceQuarterToStartPoint, adjustedSourceQuarterToEndPoint, adjustedTargetQuarterToEndPoint, adjustedTargetQuarterToStartPoint
EnsureWindingOrder(vertices, true); // true for CW, false for CCW based on your system's requirements Polygon hallwayShape = new Polygon(vertices); Partition hallwayPartition = new Partition(hallwayShape); data.Leaves.Add(hallwayPartition); // Assuming dungeonData is accessible Debug.Log("All raycasts successfully aligned at midpoint: " + midpoint); } // Draw the primary connection line Debug.DrawLine(new Vector3(midpoint.x, midpoint.y, 0), new Vector3(bestConnectionPoint.x, bestConnectionPoint.y, 0), Color.red, 5000); } } } } } }