Skip to content

Commit

Permalink
Not stopping until at least 3 line segments drawn is now handled by C…
Browse files Browse the repository at this point in the history
…loseFieldLine
  • Loading branch information
FlorianGlawogger committed Feb 19, 2025
1 parent 94a15c6 commit c980d6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,46 @@ 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 int currentLineSegmentIndex;

private void Awake()
{
fieldLine = GetComponent<FieldLine>();
if (fieldLine)
{
fieldLine.StartDrawing.AddListener(OnStartDrawing);
fieldLine.stopDrawingCheck = StopDrawing;
}
}

private void OnStartDrawing()
{
currentLineSegmentIndex = -1;
}

/// <summary>
/// Checks if the FieldLine should stop drawing
/// (e.g. because it is close to its start and would otherwise just loop unnecessarily)
/// </summary>
/// <param name="lineSegmentPosition">The position of the current line segment of the FieldLine</param>
/// <returns>True if the FieldLine should stop drawing</returns>
private bool StopDrawing(Vector3 lineSegmentPosition)
{
var emObj = transform.parent.gameObject;
currentLineSegmentIndex++;

var dist = transform.position;
if (currentLineSegmentIndex < minLineSegmentCount)
{
// Don't stop drawing until minLineSegmentCount has been reached
return false;
}

Vector3 fieldLineOffsetStart = fieldLine.transform.TransformPoint(Vector3.zero - fieldLine.originOffset);
float lineDistanceToStart = Vector3.Distance(lineSegmentPosition, fieldLineOffsetStart);
Vector3 fieldLineStartPosition = fieldLine.transform.TransformPoint(Vector3.zero - fieldLine.originOffset);
float lineDistanceToStart = Vector3.Distance(lineSegmentPosition, fieldLineStartPosition);
bool closeToStart = lineDistanceToStart <= (fieldLine.minLineSegmentLength * 2);

return closeToStart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Events;

#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif
Expand Down Expand Up @@ -62,6 +64,10 @@ public class FieldLine : MonoBehaviour, IResetObject
/// </summary>
private AdvancedLineRenderer _lineRenderer;

/// <summary>
/// Invoked when the FieldLine starts the drawing process.
/// </summary>
public UnityEvent StartDrawing = new UnityEvent();
public delegate bool StopDrawingCheck(Vector3 position);
public StopDrawingCheck stopDrawingCheck;

Expand Down Expand Up @@ -134,6 +140,8 @@ public void Draw()
if (!visible || Mathf.Abs(GetFieldStrengthFromEmObj()) * fieldStrengthFactor < 0.05)
return;

StartDrawing?.Invoke();

// Start drawing at originOffset
int positionIndex = 0;
Vector3 previousDirection = Vector3.zero;
Expand All @@ -152,8 +160,9 @@ public void Draw()
if (positionIndex > 3)
{
// Set line segment length based on how steep the angle to previous direction is
// Except for the first positions (because previousDirection is still Vector3.zero at the beginning,
// and then we also get more accurate result with less jumping around of field lines when moving objects)
// Except for the first positions, because:
// 1. previousDirection is still Vector3.zero at the beginning
// 2. and then we also get more accurate result with less jumping around of field lines when moving objects)
float difference = Vector3.Angle(previousDirection, direction);
float lerpFactor = 1f - Mathf.Clamp01(difference / maxLineSegmentAngleChangeForLengthAdjustment);
lerpFactor = Mathf.Pow(lerpFactor, lineSegmentLengthAdjustmentExponent);
Expand All @@ -168,7 +177,7 @@ public void Draw()
previousDirection = direction;

// Check if we should stop drawing
if (positionIndex > 3 && stopDrawingCheck != null && stopDrawingCheck(position))
if (stopDrawingCheck != null && stopDrawingCheck(position))
break;
}

Expand Down

0 comments on commit c980d6d

Please sign in to comment.