BoardPathRenderer.cs 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using DevilLink2.Core;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace DevilLink2.Presentation
  6. {
  7. /// <summary>Non-interactive overlay outside the board viewport. Stores only algorithm-returned paths.</summary>
  8. [RequireComponent(typeof(CanvasRenderer))]
  9. public sealed class BoardPathRenderer : MaskableGraphic
  10. {
  11. private sealed class Stroke
  12. {
  13. public GridPosition[] Points; public RectTransform Board; public BoardLayout Layout; public float Age;
  14. }
  15. private readonly List<Stroke> strokes = new List<Stroke>();
  16. public int ActivePathCount => strokes.Count;
  17. public const float DrawSeconds = .180f, FadeStartsAt = .220f, FadeSeconds = .260f, LineWidth = 3.5f;
  18. protected override void Awake() { base.Awake(); raycastTarget = false; maskable = false; }
  19. public void Show(IReadOnlyList<GridPosition> path, RectTransform board, BoardLayout layout)
  20. {
  21. if (path == null || path.Count < 2 || !board) return;
  22. var copy = new GridPosition[path.Count]; for (int i = 0; i < copy.Length; i++) copy[i] = path[i];
  23. strokes.Add(new Stroke { Points = copy, Board = board, Layout = layout }); SetVerticesDirty();
  24. }
  25. public void Clear() { strokes.Clear(); SetVerticesDirty(); }
  26. private void Update() { AdvanceVisuals(Time.unscaledDeltaTime); }
  27. protected override void OnDisable() { Clear(); base.OnDisable(); }
  28. public void AdvanceVisuals(float seconds)
  29. {
  30. if (float.IsNaN(seconds) || float.IsInfinity(seconds) || seconds < 0) return;
  31. if (strokes.Count == 0) return;
  32. for (int i = strokes.Count - 1; i >= 0; i--)
  33. { strokes[i].Age += seconds; if (!strokes[i].Board || strokes[i].Age >= FadeStartsAt + FadeSeconds) strokes.RemoveAt(i); }
  34. SetVerticesDirty();
  35. }
  36. protected override void OnPopulateMesh(VertexHelper vh)
  37. {
  38. vh.Clear();
  39. foreach (var stroke in strokes)
  40. {
  41. if (!stroke.Board) continue;
  42. var points = new Vector2[stroke.Points.Length]; float total = 0;
  43. for (int i = 0; i < points.Length; i++)
  44. {
  45. var p = stroke.Points[i]; var offset = stroke.Layout.TilePosition(p.Row, p.Column);
  46. var local = new Vector3(-stroke.Board.rect.width / 2 + offset.x + stroke.Layout.TileSize / 2,
  47. stroke.Board.rect.height / 2 + offset.y - stroke.Layout.TileSize / 2, 0);
  48. points[i] = rectTransform.InverseTransformPoint(stroke.Board.TransformPoint(local));
  49. }
  50. // End at the cell edges, leaving the accepted tiles' artwork unobscured.
  51. float inset = stroke.Layout.TileSize * .5f * stroke.Board.lossyScale.x / Mathf.Max(.0001f, rectTransform.lossyScale.x);
  52. points[0] = Vector2.MoveTowards(points[0], points[1], inset);
  53. int last = points.Length - 1;
  54. points[last] = Vector2.MoveTowards(points[last], points[last - 1], inset);
  55. for (int i = 1; i < points.Length; i++) total += Vector2.Distance(points[i - 1], points[i]);
  56. float remaining = total * Mathf.Clamp01(stroke.Age / DrawSeconds);
  57. var tint = new Color(239f / 255, 214f / 255, 157f / 255,
  58. 1 - Mathf.Clamp01((stroke.Age - FadeStartsAt) / FadeSeconds));
  59. for (int i = 1; i < points.Length && remaining > 0; i++)
  60. {
  61. float length = Vector2.Distance(points[i - 1], points[i]);
  62. if (length <= 0) continue;
  63. var end = Vector2.Lerp(points[i - 1], points[i], Mathf.Min(1, remaining / length));
  64. AddSegment(vh, points[i - 1], end, tint); remaining -= length;
  65. if (i < points.Length - 1 && remaining > 0)
  66. {
  67. // Fill the tiny corner seam without changing the algorithm's path points.
  68. float half = LineWidth * .5f;
  69. AddSegment(vh, points[i] - Vector2.right * half, points[i] + Vector2.right * half, tint);
  70. }
  71. }
  72. }
  73. }
  74. private static void AddSegment(VertexHelper vh, Vector2 a, Vector2 b, Color tint)
  75. {
  76. var normal = new Vector2(-(b - a).y, (b - a).x).normalized * (LineWidth / 2);
  77. int first = vh.currentVertCount;
  78. vh.AddVert(a - normal, tint, Vector2.zero); vh.AddVert(a + normal, tint, Vector2.zero);
  79. vh.AddVert(b + normal, tint, Vector2.zero); vh.AddVert(b - normal, tint, Vector2.zero);
  80. vh.AddTriangle(first, first + 1, first + 2); vh.AddTriangle(first, first + 2, first + 3);
  81. }
  82. }
  83. }