Turn.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Linq;
  3. using JetBrains.Annotations;
  4. using Pools;
  5. using Roads;
  6. using UnityEngine;
  7. namespace Routes
  8. {
  9. public class Turn : MonoBehaviour
  10. {
  11. public RoadDirection comingFrom;
  12. public RoadDirection goingTo;
  13. [CanBeNull] private Pool arrowPool;
  14. private bool arrowPoolAvailable;
  15. private TurnDirection turnDirection;
  16. private void Awake()
  17. {
  18. arrowPool = FindObjectsOfType<Pool>().FirstOrDefault(o => o.CompareTag("ArrowPool"));
  19. arrowPoolAvailable = arrowPool != null;
  20. if (!arrowPoolAvailable) Debug.LogWarning("Arrow Pool not found");
  21. }
  22. private void Start()
  23. {
  24. if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None) return;
  25. turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
  26. var items = new[] {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
  27. foreach (var position in items.Where(i => i != comingFrom && i != goingTo))
  28. {
  29. AddArrows(position);
  30. }
  31. }
  32. protected virtual Transform RoadDirectionToTransform(RoadDirection position)
  33. {
  34. throw new NotImplementedException();
  35. }
  36. private void AddArrows(RoadDirection position)
  37. {
  38. if (!arrowPoolAvailable) return;
  39. Transform t = RoadDirectionToTransform(position);
  40. System.Diagnostics.Debug.Assert(arrowPool != null, nameof(arrowPool) + " != null");
  41. var arrows = arrowPool.GetItem();
  42. SetRotation(arrows, position);
  43. arrows.transform.position = t.position;
  44. }
  45. private void SetRotation(GameObject arrows, RoadDirection position)
  46. {
  47. //by default, the arrow shows to north
  48. switch (position)
  49. {
  50. case RoadDirection.West:
  51. {
  52. if (turnDirection == TurnDirection.Left ||
  53. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  54. {
  55. arrows.transform.Rotate(Vector3.up, 90);
  56. }
  57. else if (turnDirection == TurnDirection.Right ||
  58. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  59. {
  60. arrows.transform.Rotate(Vector3.up, 90);
  61. }
  62. break;
  63. }
  64. case RoadDirection.North:
  65. if (turnDirection == TurnDirection.Left ||
  66. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.East)
  67. {
  68. arrows.transform.Rotate(Vector3.up, 180);
  69. }
  70. break;
  71. case RoadDirection.East:
  72. if (turnDirection == TurnDirection.Right ||
  73. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  74. {
  75. arrows.transform.Rotate(Vector3.up, 90);
  76. }
  77. else if (turnDirection == TurnDirection.Left ||
  78. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  79. {
  80. arrows.transform.Rotate(Vector3.up, -90);
  81. }
  82. break;
  83. case RoadDirection.South:
  84. if (turnDirection == TurnDirection.Right ||
  85. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  86. {
  87. arrows.transform.Rotate(Vector3.up, 180);
  88. }
  89. break;
  90. case RoadDirection.None:
  91. throw new ArgumentException("RoadDirection.None not allowed for adding arrows");
  92. default:
  93. throw new ArgumentOutOfRangeException(nameof(position), position, null);
  94. }
  95. }
  96. }
  97. }