Skip to content

Commit

Permalink
Improve stop drawing check to also check distance to start once furth…
Browse files Browse the repository at this point in the history
…est distance is not updating anymore
  • Loading branch information
FlorianGlawogger committed Feb 19, 2025
1 parent c980d6d commit 6aaf693
Showing 1 changed file with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
[RequireComponent(typeof(FieldLine))]
public class CloseFieldLine : MonoBehaviour
{
private FieldLine fieldLine;

[Tooltip("Don't stop drawing until at least this many line segments have been drawn (i.e. the number of StopDrawing calls)")]
[SerializeField] private int minLineSegmentCount = 3;

private FieldLine fieldLine;
private int currentLineSegmentIndex;
private float furthestDistanceToStart;
private float distanceToStartWhenDrawingShouldStop;

private void Awake()
{
Expand All @@ -23,7 +24,10 @@ private void Awake()

private void OnStartDrawing()
{
// Reset field line's segment-related variables
currentLineSegmentIndex = -1;
furthestDistanceToStart = 0f;
distanceToStartWhenDrawingShouldStop = ((fieldLine.minLineSegmentLength + fieldLine.maxLineSegmentLength) / 2f);
}

/// <summary>
Expand All @@ -36,16 +40,31 @@ private bool StopDrawing(Vector3 lineSegmentPosition)
{
currentLineSegmentIndex++;

if (currentLineSegmentIndex < minLineSegmentCount)
// Check if distance to start pos got larger
Vector3 fieldLineStartPosition = fieldLine.transform.TransformPoint(Vector3.zero - fieldLine.originOffset);
float lineDistanceToStart = Vector3.Distance(lineSegmentPosition, fieldLineStartPosition);
if (lineDistanceToStart > furthestDistanceToStart)
{
// Don't stop drawing until minLineSegmentCount has been reached
return false;
// distance to start position got larger
furthestDistanceToStart = lineDistanceToStart;
}

Vector3 fieldLineStartPosition = fieldLine.transform.TransformPoint(Vector3.zero - fieldLine.originOffset);
float lineDistanceToStart = Vector3.Distance(lineSegmentPosition, fieldLineStartPosition);
bool closeToStart = lineDistanceToStart <= (fieldLine.minLineSegmentLength * 2);
if (currentLineSegmentIndex < minLineSegmentCount)
// Don't stop drawing until at least minLineSegmentCount has been reached
return false;

if (lineDistanceToStart < furthestDistanceToStart)
{
// furthestDistanceToStart did not update
// i.e. we might be already getting close to start pos
if (lineDistanceToStart < distanceToStartWhenDrawingShouldStop)
{
return true;
}
}

return closeToStart;
// If very small field line loop, we also should stop when very close to Start even when we have not yet reached a smaller furthestDistanceToStart
bool VeryCloseToStartPos = lineDistanceToStart <= (fieldLine.minLineSegmentLength * 2);
return VeryCloseToStartPos;
}
}

0 comments on commit 6aaf693

Please sign in to comment.