Turn.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using JetBrains.Annotations;
  5. using Pools;
  6. using Roads;
  7. using UnityEngine;
  8. using Valve.VR.InteractionSystem;
  9. namespace Routes
  10. {
  11. public abstract class Turn : MonoBehaviour
  12. {
  13. public RoadDirection comingFrom;
  14. public RoadDirection goingTo;
  15. [CanBeNull] private Pool arrowPool;
  16. private bool arrowPoolAvailable;
  17. private TurnDirection turnDirection;
  18. private List<GameObject> usedArrows = new List<GameObject>();
  19. public Action OnTriggerExitBicycle { get; set; }
  20. private void Awake()
  21. {
  22. arrowPool = FindObjectsOfType<Pool>().FirstOrDefault(o => o.CompareTag("ArrowPool"));
  23. arrowPoolAvailable = arrowPool != null;
  24. if (!arrowPoolAvailable) Debug.LogWarning("Arrow Pool not found");
  25. }
  26. private void Start()
  27. {
  28. UpdateArrows();
  29. }
  30. protected abstract Transform RoadDirectionToTransform(RoadDirection position);
  31. private void AddArrows(RoadDirection position, GameObject arrows)
  32. {
  33. if (!arrowPoolAvailable) return;
  34. var t = RoadDirectionToTransform(position);
  35. SetRotation(arrows, position);
  36. arrows.transform.position = t.position;
  37. }
  38. private void SetRotation(GameObject arrows, RoadDirection position)
  39. {
  40. var t = arrows.transform;
  41. //by default, the arrows show to east
  42. switch (position)
  43. {
  44. case RoadDirection.West:
  45. {
  46. if (turnDirection == TurnDirection.Left ||
  47. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  48. {
  49. t.rotation = Quaternion.Euler(0, 90, 0);
  50. //arrows.transform.Rotate(Vector3.up, 90);
  51. }
  52. else if (turnDirection == TurnDirection.Right ||
  53. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  54. {
  55. t.rotation = Quaternion.Euler(0, 270, 0);
  56. //arrows.transform.Rotate(Vector3.up, 90);
  57. }
  58. break;
  59. }
  60. case RoadDirection.North:
  61. if (turnDirection == TurnDirection.Left ||
  62. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  63. {
  64. t.rotation = Quaternion.Euler(0, 180, 0);
  65. //arrows.transform.Rotate(Vector3.up, 180);
  66. }
  67. break;
  68. case RoadDirection.East:
  69. if (turnDirection == TurnDirection.Right ||
  70. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  71. {
  72. t.rotation = Quaternion.Euler(0, 90, 0);
  73. //arrows.transform.Rotate(Vector3.up, 90);
  74. }
  75. else if (turnDirection == TurnDirection.Left ||
  76. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  77. {
  78. t.rotation = Quaternion.Euler(0, 270, 0);
  79. //arrows.transform.Rotate(Vector3.up, -90);
  80. }
  81. break;
  82. case RoadDirection.South:
  83. if (turnDirection == TurnDirection.Right ||
  84. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  85. {
  86. t.rotation = Quaternion.Euler(0, 180, 0);
  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. public void UpdateArrows()
  97. {
  98. if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None)
  99. {
  100. if (!arrowPoolAvailable) return;
  101. usedArrows.ForEach(o =>
  102. {
  103. // ReSharper disable once PossibleNullReferenceException
  104. arrowPool.ReturnToPool(o);
  105. });
  106. usedArrows.Clear();
  107. return;
  108. }
  109. turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
  110. var items = new[] {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
  111. var usedArrowIndex = 0;
  112. foreach (var position in items.Where(i => i != comingFrom && i != goingTo))
  113. {
  114. GameObject arrows;
  115. if (usedArrowIndex < usedArrows.Count)
  116. {
  117. arrows = usedArrows[usedArrowIndex];
  118. }
  119. else
  120. {
  121. // ReSharper disable once PossibleNullReferenceException
  122. arrows = arrowPool.GetItem();
  123. usedArrows.Add(arrows);
  124. }
  125. AddArrows(position, arrows);
  126. usedArrowIndex++;
  127. }
  128. }
  129. private void OnTriggerExit(Collider other)
  130. {
  131. if (other.CompareTag("bike"))
  132. {
  133. OnTriggerExitBicycle();
  134. }
  135. }
  136. private void OnCollisionEnter(Collision other)
  137. {
  138. Debug.Log("Colission");
  139. }
  140. private void OnTriggerEnter(Collider other)
  141. {
  142. Debug.Log("Trigger");
  143. }
  144. }
  145. }