1- using System . Linq ;
21using System . Collections . Generic ;
32using UnityEngine ;
43using UnityEngine . Events ;
@@ -8,7 +7,6 @@ public class MainCameraZoomController : MonoBehaviour, IPrimaryWindowElement
87 public UnityEvent < float > cameraSizeWasUpdatedEvent ;
98
109 private float zoomPerScroll ;
11- private float sizeUpperBoundToMapSize ;
1210 private bool zoomCanBeModified = true ;
1311 private bool mapTileIsSelected ;
1412 private bool inputIsActive = true ;
@@ -17,7 +15,7 @@ public class MainCameraZoomController : MonoBehaviour, IPrimaryWindowElement
1715 private MapGenerationManager mapGenerationManager ;
1816 private VisualiserEventsManager visualiserEventsManager ;
1917
20- private readonly float SIZE_GROWTH_PER_ITERATION = 1f ;
18+ private readonly float ADDITIONAL_OFFSET_FROM_MAP_EDGES = 1f ;
2119
2220 public void SetPrimaryWindowElementActive ( bool active )
2321 {
@@ -32,7 +30,6 @@ public void SetZoomPerScroll(float zoomPerScroll)
3230 private void Awake ( )
3331 {
3432 mainCamera = Camera . main ;
35- sizeUpperBoundToMapSize = mainCamera != null ? mainCamera . orthographicSize : 0f ;
3633 userInputController = FindFirstObjectByType < UserInputController > ( ) ;
3734 mapGenerationManager = FindFirstObjectByType < MapGenerationManager > ( ) ;
3835 visualiserEventsManager = FindFirstObjectByType < VisualiserEventsManager > ( ) ;
@@ -89,10 +86,14 @@ private void RegisterToListeners(bool register)
8986
9087 private void OnWheelScrolled ( Vector2 scrollVector )
9188 {
92- if ( inputIsActive && zoomCanBeModified && mainCamera != null && mainCamera . orthographic )
89+ if ( ! inputIsActive || ! zoomCanBeModified || mainCamera == null || ! mainCamera . orthographic )
9390 {
94- mainCamera . orthographicSize = Mathf . Clamp ( mainCamera . orthographicSize - zoomPerScroll * scrollVector . y , MapGenerationManager . MAP_DIMENSION_LOWER_BOUND , sizeUpperBoundToMapSize ) ;
91+ return ;
9592 }
93+
94+ var sizeModifyStep = zoomPerScroll * scrollVector . y ;
95+
96+ mainCamera . orthographicSize = Mathf . Clamp ( mainCamera . orthographicSize - sizeModifyStep , GetMinimumSize ( ) , GetMaximumSize ( ) ) ;
9697 }
9798
9899 private void OnMapTilesWereAdded ( List < MapTile > mapTiles )
@@ -112,53 +113,11 @@ private void UpdateMaximumSize()
112113 return ;
113114 }
114115
115- mainCamera . orthographicSize = MapGenerationManager . MAP_DIMENSION_LOWER_BOUND ;
116-
117- while ( ! EntireMapIsVisibleWithinCamera ( ) && mainCamera . orthographicSize < MapGenerationManager . MAP_DIMENSION_UPPER_BOUND )
118- {
119- mainCamera . orthographicSize += SIZE_GROWTH_PER_ITERATION ;
120- }
121-
122- sizeUpperBoundToMapSize = mainCamera . orthographicSize ;
116+ mainCamera . orthographicSize = GetMaximumSize ( ) ;
123117
124118 cameraSizeWasUpdatedEvent ? . Invoke ( mainCamera . orthographicSize ) ;
125119 }
126120
127- private bool EntireMapIsVisibleWithinCamera ( )
128- {
129- if ( mapGenerationManager == null )
130- {
131- return false ;
132- }
133-
134- var mapSizeWithOffset = mapGenerationManager . GetMapSize ( ) - Vector2 . one * MapTile . GRID_SIZE ;
135- var cameraSize = GetCameraSize ( ) ;
136- var offsetToCenter = ( mapSizeWithOffset - cameraSize ) * 0.5f ;
137- var areaInTheCenterOfMap = new Rect ( offsetToCenter , cameraSize ) ;
138- var mapCornersTiles = mapGenerationManager . GetMapCornersTiles ( ) ;
139-
140- return mapCornersTiles . All ( mapTile =>
141- {
142- var mapTileBounds = mapTile . GetMapTileRenderer ( ) . GetBounds ( ) ;
143- var mapTileBoundsRectangle = new Rect ( mapTileBounds . min , mapTileBounds . size ) ;
144-
145- return areaInTheCenterOfMap . Contains ( mapTileBoundsRectangle . min ) && areaInTheCenterOfMap . Contains ( mapTileBoundsRectangle . max ) ;
146- } ) ;
147- }
148-
149- private Vector2 GetCameraSize ( )
150- {
151- if ( mainCamera == null )
152- {
153- return Vector2 . zero ;
154- }
155-
156- var cameraHeight = mainCamera . orthographicSize * 2f ;
157- var cameraWidth = cameraHeight * mainCamera . aspect ;
158-
159- return new Vector2 ( cameraWidth , cameraHeight ) ;
160- }
161-
162121 private void OnEventReceived ( VisualiserEvent visualiserEvent )
163122 {
164123 if ( visualiserEvent is not MapTileBoolVisualiserEvent mapTileBoolVisualiserEvent )
@@ -182,4 +141,14 @@ private void OnEventReceived(VisualiserEvent visualiserEvent)
182141 break ;
183142 }
184143 }
144+
145+ private float GetMaximumSize ( )
146+ {
147+ var maximumMapDimension = GetSizeBy ( mapGenerationManager != null ? mapGenerationManager . GetMaximumMapDimension ( ) : 0f ) ;
148+
149+ return Mathf . Max ( GetMinimumSize ( ) , maximumMapDimension ) ;
150+ }
151+
152+ private float GetMinimumSize ( ) => GetSizeBy ( MapGenerationManager . MAP_DIMENSION_LOWER_BOUND ) ;
153+ private float GetSizeBy ( float value ) => value * 0.5f + ADDITIONAL_OFFSET_FROM_MAP_EDGES ;
185154}
0 commit comments