using System.Collections.Generic;
using UnityEngine;
namespace MazeGen
{
///
/// Draws the agent's traveled path (start room -> current room) as a line, one segment
/// per hop. Only the most recently added hop is drawn bright; every earlier hop is
/// dimmed down so the eye is drawn to where the agent is right now, not the whole trail.
///
[RequireComponent(typeof(LineRenderer))]
public class PathVisualizer : MonoBehaviour
{
private static readonly Color BrightColor = new Color(1f, 0.1f, 0.75f);
private static readonly Color DimColor = new Color(0.55f, 0.2f, 0.4f, 0.35f);
private const float LineWidth = 0.15f;
private readonly List _segments = new List();
private Vector3? _lastPoint;
private Material _lineMaterial;
private void Awake()
{
// The LineRenderer required on this GameObject itself is left unused/empty -
// each hop gets its own child LineRenderer so it can be colored independently.
GetComponent().positionCount = 0;
_lineMaterial = new Material(Shader.Find("Sprites/Default"));
}
public void AppendPoint(Vector2 worldPos)
{
Vector3 point = new Vector3(worldPos.x, worldPos.y, -0.5f);
if (_lastPoint.HasValue)
{
DimExistingSegments();
_segments.Add(CreateSegment(_lastPoint.Value, point));
}
_lastPoint = point;
}
private void DimExistingSegments()
{
foreach (var segment in _segments)
{
segment.startColor = DimColor;
segment.endColor = DimColor;
}
}
private LineRenderer CreateSegment(Vector3 a, Vector3 b)
{
var segmentGO = new GameObject("PathSegment");
segmentGO.transform.SetParent(transform, false);
var line = segmentGO.AddComponent();
line.positionCount = 2;
line.SetPositions(new[] { a, b });
line.widthMultiplier = LineWidth;
line.useWorldSpace = true;
line.sortingOrder = 2;
line.material = _lineMaterial;
line.startColor = BrightColor;
line.endColor = BrightColor;
return line;
}
public void Clear()
{
foreach (var segment in _segments)
if (segment != null) Destroy(segment.gameObject);
_segments.Clear();
_lastPoint = null;
}
}
}