Skip to content

Commit 9cd5d53

Browse files
author
JasonNumberThirteen
committed
Optimise adjusting zoom of the main camera after changing dimensions of the map
1 parent b197840 commit 9cd5d53

5 files changed

Lines changed: 19 additions & 82 deletions

File tree

Assets/Project/Prefabs/Game Objects/MapTile.prefab

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scripts/Game Objects/Camera/Main/MainCameraZoomController.cs

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Linq;
21
using System.Collections.Generic;
32
using UnityEngine;
43
using 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
}

Assets/Project/Scripts/Game Objects/Map Tile/MapTile.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using UnityEngine;
22
using UnityEngine.Events;
33

4-
[RequireComponent(typeof(MapTileNode), typeof(MapTileRenderer))]
4+
[RequireComponent(typeof(MapTileNode))]
55
public class MapTile : MonoBehaviour
66
{
77
public static readonly float GRID_SIZE = 1f;
@@ -14,14 +14,12 @@ public class MapTile : MonoBehaviour
1414
private MapTileType tileType;
1515
private int weight;
1616
private MapTileNode mapTileNode;
17-
private MapTileRenderer mapTileRenderer;
1817
private VisualiserEventsManager visualiserEventsManager;
1918

2019
public Vector2 GetPosition() => transform.position;
2120
public MapTileType GetTileType() => tileType;
2221
public int GetWeight() => weight;
2322
public MapTileNode GetMapTileNode() => mapTileNode;
24-
public MapTileRenderer GetMapTileRenderer() => mapTileRenderer;
2523

2624
public void ResetTile()
2725
{
@@ -73,7 +71,6 @@ public void SetWeightTo(int weight)
7371
private void Awake()
7472
{
7573
mapTileNode = GetComponent<MapTileNode>();
76-
mapTileRenderer = GetComponent<MapTileRenderer>();
7774
visualiserEventsManager = FindFirstObjectByType<VisualiserEventsManager>();
7875
}
7976
}

Assets/Project/Scripts/Game Objects/Map Tile/MapTileRenderer.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

Assets/Project/Scripts/Game Objects/Map Tile/MapTileRenderer.cs.meta

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)