From d8b2403c89bf8cfc6623c3bc7ada2c9070877fd9 Mon Sep 17 00:00:00 2001 From: Victor Chelaru Date: Tue, 28 Nov 2023 07:54:17 -0700 Subject: [PATCH] More work on CameraControllingEntity to expose information about max zoom and viewable area. --- .../Entities/CameraControllingEntity.cs | 59 ++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/Engines/FlatRedBallXNA/FlatRedBall/Entities/CameraControllingEntity.cs b/Engines/FlatRedBallXNA/FlatRedBall/Entities/CameraControllingEntity.cs index c0bb06a79..0c0fc2bcc 100644 --- a/Engines/FlatRedBallXNA/FlatRedBall/Entities/CameraControllingEntity.cs +++ b/Engines/FlatRedBallXNA/FlatRedBall/Entities/CameraControllingEntity.cs @@ -130,7 +130,45 @@ public bool LerpSmooth /// public bool LerpSmoothZoom { get; set; } = true; - public float CurrentZoom { get; set; } = 1; + /// + /// The value used to mulitply the OrthogonalWidth and OrthogonalHeight. A larger value means the camera is more zoomed out, and can see more of the game world. + /// A smaller value means the camera is zoomed in, so it can see less of the game world. + /// + public float ViewableAreaMultiplier { get; private set; } = 1; + + /// + /// Returns the maximum possible value that ViewableAreaMultiplier can be set to. This is based on the size of the presence and size of the map. + /// If Map is null, this returns float.PositiveInfinity. + /// + public float MaxViewableAreaMultiplier + { + get + { + float maxViewableMultiplier = float.PositiveInfinity; + + if (Map != null) + { + var mapHeight = Map.Height; + var mapWidth = Map.Width; + + var maxViewableMultiplierX = mapWidth / defaultOrthoWidth; + var maxViewableMultiplierY = mapHeight / defaultOrthoHeight; + + maxViewableMultiplier = System.Math.Min(maxViewableMultiplierX, maxViewableMultiplierY); + } + return System.Math.Max(1, maxViewableMultiplier); + } + } + + /// + /// Returns the maximum possible viewable width when the camera is zoomed out as far as possible. This is based on the size of the presence and size of the map. + /// + public float MaxViewableAreaWidth => defaultOrthoWidth * MaxViewableAreaMultiplier; + /// + /// Returns the maximum possible viewable height when the camera is zoomed out as far as possible. This is based on the size of the presence and size of the map. + /// + public float MaxViewableAreaHeight => defaultOrthoHeight * MaxViewableAreaMultiplier; + /// /// The amount of smoothing. The larger the number, faster the Camera moves. This value is ignored if TargetApproachStyle is Immediate. @@ -604,17 +642,8 @@ public void ApplySeparationForZoom(Vector2 separationVector) { desiredZoom = System.Math.Min(furthestZoom, currentSeparationDistance / noZoomDistance); - if (Map != null) - { - var mapHeight = Map.Height; - var mapWidth = Map.Width; - - var maxZoomX = mapWidth / defaultOrthoWidth; - var maxZoomY = mapHeight / defaultOrthoHeight; - desiredZoom = System.Math.Min(System.Math.Min(desiredZoom, maxZoomX), maxZoomY); - desiredZoom = System.Math.Max(desiredZoom, 1); - } - + desiredZoom = System.Math.Min(desiredZoom, MaxViewableAreaMultiplier); + desiredZoom = System.Math.Max(desiredZoom, 1); } else { @@ -623,14 +652,14 @@ public void ApplySeparationForZoom(Vector2 separationVector) if (LerpSmoothZoom) { - CurrentZoom = MathHelper.Lerp(CurrentZoom, desiredZoom, .1f); + ViewableAreaMultiplier = MathHelper.Lerp(ViewableAreaMultiplier, desiredZoom, .1f); } else { - CurrentZoom = desiredZoom; + ViewableAreaMultiplier = desiredZoom; } - Camera.OrthogonalHeight = defaultOrthoHeight * CurrentZoom; + Camera.OrthogonalHeight = defaultOrthoHeight * ViewableAreaMultiplier; Camera.FixAspectRatioYConstant(); }