Skip to content

Commit

Permalink
Added CameraControllingEntity IsKeepingTargetsInView
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Nov 29, 2023
1 parent d8b2403 commit 71ff0ac
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ public enum CameraBehaviorType

public enum TargetApproachStyle
{
/// <summary>
/// The camera moves to the target position immediately, effectively locking on to its position.
/// </summary>
Immediate,
/// <summary>
/// The camera moves to the target position smoothly, but not at a constant speed. The camera will move faster
/// if it is further away from the target, and slower if it is closer.
/// </summary>
Smooth,
/// <summary>
/// The camera moves to the target at a constant speed, regardless of the distance between the camera and the target.
/// </summary>
ConstantSpeed
}

Expand All @@ -36,6 +46,7 @@ public class CameraControllingEntity : PositionedObject
private float minZoomPercent;
private bool isAutoZoomEnabled;
private float furthestZoom;
private AxisAlignedRectangle MaximumViewRectangle = new AxisAlignedRectangle();


/// <summary>
Expand Down Expand Up @@ -123,6 +134,9 @@ public bool LerpSmooth
}
}

/// <summary>
/// The type of approach to use when moving the camera to the target position.
/// </summary>
public TargetApproachStyle TargetApproachStyle { get; set; } = TargetApproachStyle.Smooth;

/// <summary>
Expand Down Expand Up @@ -169,6 +183,7 @@ public float MaxViewableAreaMultiplier
/// </summary>
public float MaxViewableAreaHeight => defaultOrthoHeight * MaxViewableAreaMultiplier;

public bool IsKeepingTargetsInView { get; set; } = false;

/// <summary>
/// The amount of smoothing. The larger the number, faster the Camera moves. This value is ignored if TargetApproachStyle is Immediate.
Expand Down Expand Up @@ -292,12 +307,18 @@ public void Activity()
windowVisualization.Height = ScrollingWindowHeight;
}

if (IsKeepingTargetsInView && hasActivityBeenCalled)
{
KeepTargetsInView();
}

// Zoom should be happening first, and then targeting:
if (isAutoZoomEnabled)
{
ApplyZoom();
}


var target = GetTarget();

var effectiveTargetApproachStyleX =
Expand Down Expand Up @@ -347,6 +368,40 @@ public void Activity()
hasActivityBeenCalled = true;
}

private void KeepTargetsInView()
{
MaximumViewRectangle.Position = this.Position.AtZ(0);
MaximumViewRectangle.Width = MaxViewableAreaWidth;
MaximumViewRectangle.Height = MaxViewableAreaHeight;


for (int i = 0; i < Targets.Count; i++)
{
var target = Targets[i] as PositionedObject;

if(target != null)
{
if(target.Y > MaximumViewRectangle.Y + MaximumViewRectangle.Height / 2)
{
target.Y = MaximumViewRectangle.Y + MaximumViewRectangle.Height / 2;
}
else if(target.Y < MaximumViewRectangle.Y - MaximumViewRectangle.Height / 2)
{
target.Y = MaximumViewRectangle.Y - MaximumViewRectangle.Height / 2;
}

if(target.X > MaximumViewRectangle.X + MaximumViewRectangle.Width / 2)
{
target.X = MaximumViewRectangle.X + MaximumViewRectangle.Width / 2;
}
else if(target.X < MaximumViewRectangle.X - MaximumViewRectangle.Width / 2)
{
target.X = MaximumViewRectangle.X - MaximumViewRectangle.Width / 2;
}
}
}
}

private void ApplyZoom()
{
var separationVector = GetTargetSeparation();
Expand Down

0 comments on commit 71ff0ac

Please sign in to comment.