TO SQUASH: Fixed issues.
This commit is contained in:
@@ -59,6 +59,7 @@ public class WorldFiller : MonoBehaviour
|
||||
}
|
||||
|
||||
spawnedWalls.Clear();
|
||||
InitializeFreeAreas();
|
||||
}
|
||||
|
||||
public void SetWallPosition(Vector2 position)
|
||||
@@ -280,6 +281,80 @@ public class WorldFiller : MonoBehaviour
|
||||
boardArea = boardRect.width * boardRect.height;
|
||||
}
|
||||
|
||||
public bool IsPositionInFreeArea(Vector2 position)
|
||||
{
|
||||
if (freeAreas.Count == 0 && gameBoardSpriteRenderer != null)
|
||||
{
|
||||
InitializeFreeAreas();
|
||||
}
|
||||
|
||||
for (int i = 0; i < freeAreas.Count; i++)
|
||||
{
|
||||
if (RectContainsWithTolerance(freeAreas[i], position))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Vector2 GetRandomPositionInFreeAreas(Bounds? clearingArea = null)
|
||||
{
|
||||
if (freeAreas.Count == 0 && gameBoardSpriteRenderer != null)
|
||||
{
|
||||
InitializeFreeAreas();
|
||||
}
|
||||
|
||||
List<Rect> candidateAreas = new List<Rect>(freeAreas);
|
||||
|
||||
if (clearingArea.HasValue)
|
||||
{
|
||||
Bounds bounds = clearingArea.Value;
|
||||
Rect clearingRect = Rect.MinMaxRect(bounds.min.x, bounds.min.y, bounds.max.x, bounds.max.y);
|
||||
candidateAreas = GetAreasExcludingRect(candidateAreas, clearingRect);
|
||||
}
|
||||
|
||||
float totalArea = 0.0f;
|
||||
|
||||
for (int i = 0; i < candidateAreas.Count; i++)
|
||||
{
|
||||
totalArea += candidateAreas[i].width * candidateAreas[i].height;
|
||||
}
|
||||
|
||||
if (totalArea <= geometryEpsilon)
|
||||
{
|
||||
Debug.LogWarning("No free area available to generate a random position.");
|
||||
|
||||
if (gameBoardSpriteRenderer != null)
|
||||
{
|
||||
return gameBoardSpriteRenderer.bounds.center;
|
||||
}
|
||||
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
float targetArea = Random.Range(0.0f, totalArea);
|
||||
float accumulatedArea = 0.0f;
|
||||
|
||||
for (int i = 0; i < candidateAreas.Count; i++)
|
||||
{
|
||||
Rect area = candidateAreas[i];
|
||||
accumulatedArea += area.width * area.height;
|
||||
|
||||
if (targetArea <= accumulatedArea)
|
||||
{
|
||||
return new Vector2(
|
||||
Random.Range(area.xMin, area.xMax),
|
||||
Random.Range(area.yMin, area.yMax)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Rect fallbackArea = candidateAreas[candidateAreas.Count - 1];
|
||||
return fallbackArea.center;
|
||||
}
|
||||
|
||||
private float GetCurrentBarThickness(bool isVerticalLine)
|
||||
{
|
||||
var barSegments = GameObject.FindGameObjectsWithTag("Bar");
|
||||
@@ -486,6 +561,30 @@ public class WorldFiller : MonoBehaviour
|
||||
freeAreas = updatedAreas;
|
||||
}
|
||||
|
||||
private List<Rect> GetAreasExcludingRect(List<Rect> sourceAreas, Rect excludedArea)
|
||||
{
|
||||
List<Rect> remainingAreas = new List<Rect>();
|
||||
|
||||
for (int i = 0; i < sourceAreas.Count; i++)
|
||||
{
|
||||
Rect sourceArea = sourceAreas[i];
|
||||
|
||||
if (!TryGetRectIntersection(sourceArea, excludedArea, out Rect overlap))
|
||||
{
|
||||
AddValidRect(remainingAreas, sourceArea);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Split the source area around the overlap and keep only non-overlapping parts.
|
||||
AddValidRect(remainingAreas, Rect.MinMaxRect(sourceArea.xMin, sourceArea.yMin, overlap.xMin, sourceArea.yMax));
|
||||
AddValidRect(remainingAreas, Rect.MinMaxRect(overlap.xMax, sourceArea.yMin, sourceArea.xMax, sourceArea.yMax));
|
||||
AddValidRect(remainingAreas, Rect.MinMaxRect(overlap.xMin, sourceArea.yMin, overlap.xMax, overlap.yMin));
|
||||
AddValidRect(remainingAreas, Rect.MinMaxRect(overlap.xMin, overlap.yMax, overlap.xMax, sourceArea.yMax));
|
||||
}
|
||||
|
||||
return remainingAreas;
|
||||
}
|
||||
|
||||
private void AddValidRect(List<Rect> target, Rect candidate)
|
||||
{
|
||||
if (candidate.width <= geometryEpsilon || candidate.height <= geometryEpsilon)
|
||||
|
||||
Reference in New Issue
Block a user