diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index ec94e6f..cb49380 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -2960,6 +2960,9 @@ MonoBehaviour: player: {fileID: 1002905792} gameBoardSpriteRenderer: {fileID: 297740151} wallPrefab: {fileID: 2746972694419659216, guid: 3fbc857440f3c649b9d49d40955db450, type: 3} + fallbackBarThicknessWorldUnits: 0.08 + geometryEpsilon: 0.0001 + wallPaddingPixelsPerSide: 2 --- !u!1001 &2090615130 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Game/WorldFiller.cs b/Assets/Scripts/Game/WorldFiller.cs index 9d63a00..3a33cc4 100644 --- a/Assets/Scripts/Game/WorldFiller.cs +++ b/Assets/Scripts/Game/WorldFiller.cs @@ -15,6 +15,9 @@ public class WorldFiller : MonoBehaviour [SerializeField] private float geometryEpsilon = 0.0001f; + [SerializeField] + private float wallPaddingPixelsPerSide = 1.0f; + private Bounds gameBoardBounds; private Vector2?[] wallPositions; @@ -162,10 +165,20 @@ public class WorldFiller : MonoBehaviour return; } - ApplyWallTransform(wall.transform, wallRenderer, clampedWallRect); + Rect boardRect = Rect.MinMaxRect( + gameBoardBounds.min.x, + gameBoardBounds.min.y, + gameBoardBounds.max.x, + gameBoardBounds.max.y + ); + + float wallPaddingWorldUnits = PixelsToWorldUnits(wallPaddingPixelsPerSide, wallRenderer); + Rect paddedWallRect = ExpandRectInsideBounds(clampedWallRect, wallPaddingWorldUnits, boardRect); + + ApplyWallTransform(wall.transform, wallRenderer, paddedWallRect); var wallBounds = wallRenderer.bounds; - UpdateFreeAreas(clampedWallRect); + UpdateFreeAreas(paddedWallRect); // Check if there are yummies, multipliers or the shark intersecting the spawned wall. // Yummies and multipliers are awarded to the player if they are intersected by the wall. @@ -226,7 +239,7 @@ public class WorldFiller : MonoBehaviour // Let the player know that the wall has been spawned. int areaPercentage = boardArea > geometryEpsilon - ? Mathf.RoundToInt((clampedWallRect.width * clampedWallRect.height / boardArea) * 100.0f) + ? Mathf.RoundToInt((paddedWallRect.width * paddedWallRect.height / boardArea) * 100.0f) : 0; player.OnWallSpawned(areaPercentage); @@ -491,4 +504,42 @@ public class WorldFiller : MonoBehaviour && point.y >= rect.yMin - geometryEpsilon && point.y <= rect.yMax + geometryEpsilon; } + + private float PixelsToWorldUnits(float pixels, SpriteRenderer referenceRenderer) + { + float pixelsPerUnit = 100.0f; + + if (referenceRenderer != null && referenceRenderer.sprite != null && referenceRenderer.sprite.pixelsPerUnit > 0.0f) + { + pixelsPerUnit = referenceRenderer.sprite.pixelsPerUnit; + } + else if (gameBoardSpriteRenderer != null && gameBoardSpriteRenderer.sprite != null && gameBoardSpriteRenderer.sprite.pixelsPerUnit > 0.0f) + { + pixelsPerUnit = gameBoardSpriteRenderer.sprite.pixelsPerUnit; + } + + return pixels / pixelsPerUnit; + } + + private Rect ExpandRectInsideBounds(Rect rect, float padding, Rect boundsRect) + { + if (padding <= geometryEpsilon) + { + return rect; + } + + Rect expanded = Rect.MinMaxRect( + rect.xMin - padding, + rect.yMin - padding, + rect.xMax + padding, + rect.yMax + padding + ); + + if (TryGetRectIntersection(expanded, boundsRect, out Rect clampedExpanded)) + { + return clampedExpanded; + } + + return rect; + } }