Route.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using Roads;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. namespace Routes
  8. {
  9. [Serializable]
  10. public struct RouteItem
  11. {
  12. public Turn turn;
  13. public RoadDirection from;
  14. public RoadDirection to;
  15. public void Apply()
  16. {
  17. if (turn.comingFrom != from || turn.goingTo != to)
  18. {
  19. turn.comingFrom = from;
  20. turn.goingTo = to;
  21. turn.UpdateArrows();
  22. }
  23. }
  24. public void Clear()
  25. {
  26. if (turn.comingFrom != RoadDirection.None || turn.goingTo != RoadDirection.None)
  27. {
  28. turn.comingFrom = RoadDirection.None;
  29. turn.goingTo = RoadDirection.None;
  30. turn.UpdateArrows();
  31. }
  32. }
  33. }
  34. public class Route : MonoBehaviour
  35. {
  36. public List<RouteItem> items;
  37. public StraightRoadExtras start;
  38. public StraightRoadExtras finish;
  39. private int visibleLength;
  40. private int visibleStart;
  41. private int VisibleStart => Math.Max(0, visibleStart - 1);
  42. private int VisibleEnd => Math.Min(items.Count, VisibleStart + visibleLength);
  43. private void Start()
  44. {
  45. start.ShowArc();
  46. var routeManager = GetComponentInParent<RouteManager>();
  47. visibleStart = 0;
  48. visibleLength = routeManager.visibleLength;
  49. items.ForEach((item) => item.turn.OnTriggerExitBicycle = NextTurn);
  50. UpdateRouteItems();
  51. }
  52. private void NextTurn()
  53. {
  54. Debug.Log("Route: left intersection - show next turn");
  55. visibleStart++;
  56. UpdateRouteItems();
  57. }
  58. private void UpdateRouteItems()
  59. {
  60. if (VisibleEnd == items.Count - 1)
  61. {
  62. finish.ShowArc();
  63. }
  64. for (var i = 0; i < VisibleEnd; i++)
  65. {
  66. var item = items[i];
  67. if (i >= VisibleStart)
  68. {
  69. item.Apply();
  70. }
  71. else
  72. {
  73. item.Clear();
  74. }
  75. }
  76. }
  77. }
  78. }