Skip to content

Commit

Permalink
Fixed possible crash assigning Target on CameraControllingEntity.
Browse files Browse the repository at this point in the history
Renamed ApproachCoefficient -> TargetApproachCoefficient.
  • Loading branch information
vchelaru committed Nov 25, 2023
1 parent 1f6504a commit 086900b
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ public PositionedObject Target
{
set
{
if (Targets == null)
if (Targets == null ||
// This is a little inefficient but the reason we need this is a user
// may use Targets initially and then switch to using a single Target.
// If Targets are used, the user may assign the Targets to a list that is
// not compatible with the assigned value. For example, Targets could be assigned
// to a List<Enemy>, but then the Target is set to a Player. This would result in an
// invalid cast operation when the Player is added to the List<Enemy>.
Targets is PositionedObjectList<PositionedObject> == false)
{
Targets = new PositionedObjectList<PositionedObject>();
}
Expand Down Expand Up @@ -133,14 +140,14 @@ public bool LerpSmooth
/// then the velocity of the camera will be 20*5 = 100.
/// If TargetApproachStyle is ConstantSpeed, this is the speed of the camera in pixels per second. regardless of the distance to the target.
/// </remarks>
[Obsolete("Use ApproachCoefficient instead, since this value is confusingly named.")]
[Obsolete("Use TargetApproachCoefficient instead, since this value is confusingly named.")]
public float LerpCoefficient
{
get => ApproachCoefficient;
set => ApproachCoefficient = value;
get => TargetApproachCoefficient;
set => TargetApproachCoefficient = value;
}

public float ApproachCoefficient { get; set; } = 5;
public float TargetApproachCoefficient { get; set; } = 5;

/// <summary>
/// Whether to snap the camera position to the screen pixel. This value can be used to prevent half-pixels from being drawn.
Expand Down Expand Up @@ -539,11 +546,11 @@ public void ApplyTarget(Vector2 target, TargetApproachStyle approachStyleX, Targ
switch (approachStyleX)
{
case TargetApproachStyle.Smooth:
effectiveThis.Velocity.X = (target.X - effectiveThis.Position.X) * ApproachCoefficient;
effectiveThis.Velocity.X = (target.X - effectiveThis.Position.X) * TargetApproachCoefficient;
break;
case TargetApproachStyle.ConstantSpeed:
// todo - need to have a test here to see if we're within a range so we don't overshoot/jitter
effectiveThis.Velocity.X = System.Math.Sign(target.X - effectiveThis.Position.X) * ApproachCoefficient;
effectiveThis.Velocity.X = System.Math.Sign(target.X - effectiveThis.Position.X) * TargetApproachCoefficient;
break;
case TargetApproachStyle.Immediate:
effectiveThis.Position.X = target.X;
Expand All @@ -553,11 +560,11 @@ public void ApplyTarget(Vector2 target, TargetApproachStyle approachStyleX, Targ
switch (approachStyleY)
{
case TargetApproachStyle.Smooth:
effectiveThis.Velocity.Y = (target.Y - effectiveThis.Position.Y) * ApproachCoefficient;
effectiveThis.Velocity.Y = (target.Y - effectiveThis.Position.Y) * TargetApproachCoefficient;
break;
case TargetApproachStyle.ConstantSpeed:
// todo - need to have a test here to see if we're within a range so we don't overshoot/jitter
effectiveThis.Velocity.Y = System.Math.Sign(target.Y - effectiveThis.Position.Y) * ApproachCoefficient;
effectiveThis.Velocity.Y = System.Math.Sign(target.Y - effectiveThis.Position.Y) * TargetApproachCoefficient;
break;
case TargetApproachStyle.Immediate:
effectiveThis.Position.Y = target.Y;
Expand Down

0 comments on commit 086900b

Please sign in to comment.