Turn.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 enum RoadDirection
  12. {
  13. West,
  14. North,
  15. East,
  16. South,
  17. None
  18. }
  19. public enum TurnDirection
  20. {
  21. Right,
  22. Left,
  23. Straight
  24. }
  25. public abstract class Turn : MonoBehaviour
  26. {
  27. public RoadDirection comingFrom;
  28. public RoadDirection goingTo;
  29. [CanBeNull] private Pool arrowPool;
  30. private bool arrowPoolAvailable;
  31. private TurnDirection turnDirection;
  32. private List<GameObject> usedArrows = new List<GameObject>();
  33. private Collider trigger;
  34. private static readonly RoadDirection[] allDirections = {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
  35. public Action OnTriggerExitBicycle { get; set; }
  36. private void Awake()
  37. {
  38. arrowPool = FindObjectsOfType<Pool>().FirstOrDefault(o => o.CompareTag("ArrowPool"));
  39. arrowPoolAvailable = arrowPool != null;
  40. trigger = GetComponent<Collider>();
  41. if (!arrowPoolAvailable) Debug.LogWarning("Arrow Pool not found");
  42. }
  43. private void Start()
  44. {
  45. UpdateArrows();
  46. }
  47. [CanBeNull] protected abstract Transform RoadDirectionToTransform(RoadDirection position);
  48. private bool AddArrows(RoadDirection position, GameObject arrows)
  49. {
  50. Debug.Log($"---{gameObject.name}--- adding arrows at {position}");
  51. var t = RoadDirectionToTransform(position);
  52. if (t == null) return false;
  53. Debug.Log($" Transform for position => {t.gameObject.name}");
  54. SetRotation(arrows, position);
  55. arrows.transform.position = t.position;
  56. return true;
  57. }
  58. private void SetRotation(GameObject arrows, RoadDirection position)
  59. {
  60. var t = arrows.transform;
  61. //by default, the arrows show to east
  62. switch (position)
  63. {
  64. case RoadDirection.West:
  65. {
  66. if (turnDirection == TurnDirection.Left ||
  67. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  68. {
  69. t.rotation = Quaternion.Euler(0, 90, 0);
  70. //arrows.transform.Rotate(Vector3.up, 90);
  71. }
  72. else if (turnDirection == TurnDirection.Right ||
  73. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  74. {
  75. t.rotation = Quaternion.Euler(0, 270, 0);
  76. //arrows.transform.Rotate(Vector3.up, 90);
  77. }
  78. break;
  79. }
  80. case RoadDirection.North:
  81. if (turnDirection == TurnDirection.Left ||
  82. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  83. {
  84. t.rotation = Quaternion.Euler(0, 180, 0);
  85. //arrows.transform.Rotate(Vector3.up, 180);
  86. }
  87. break;
  88. case RoadDirection.East:
  89. if (turnDirection == TurnDirection.Right ||
  90. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  91. {
  92. t.rotation = Quaternion.Euler(0, 90, 0);
  93. //arrows.transform.Rotate(Vector3.up, 90);
  94. }
  95. else if (turnDirection == TurnDirection.Left ||
  96. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  97. {
  98. t.rotation = Quaternion.Euler(0, 270, 0);
  99. //arrows.transform.Rotate(Vector3.up, -90);
  100. }
  101. break;
  102. case RoadDirection.South:
  103. if (turnDirection == TurnDirection.Right ||
  104. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  105. {
  106. t.rotation = Quaternion.Euler(0, 180, 0);
  107. //arrows.transform.Rotate(Vector3.up, 180);
  108. }
  109. break;
  110. case RoadDirection.None:
  111. throw new ArgumentException("RoadDirection.None not allowed for adding arrows");
  112. default:
  113. throw new ArgumentOutOfRangeException(nameof(position), position, null);
  114. }
  115. }
  116. public void UpdateArrows()
  117. {
  118. if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None)
  119. {
  120. if (!arrowPoolAvailable) return;
  121. usedArrows.ForEach(o =>
  122. {
  123. // ReSharper disable once PossibleNullReferenceException
  124. arrowPool.ReturnToPool(o);
  125. });
  126. usedArrows.Clear();
  127. return;
  128. }
  129. turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
  130. var usedArrowIndex = 0;
  131. foreach (var position in allDirections.Where(i => i != comingFrom && i != goingTo))
  132. {
  133. GameObject arrows;
  134. if (usedArrowIndex < usedArrows.Count)
  135. {
  136. arrows = usedArrows[usedArrowIndex];
  137. }
  138. else
  139. {
  140. // ReSharper disable once PossibleNullReferenceException
  141. arrows = arrowPool.GetItem();
  142. usedArrows.Add(arrows);
  143. }
  144. var used = AddArrows(position, arrows);
  145. if (!used)
  146. {
  147. // ReSharper disable once PossibleNullReferenceException
  148. arrowPool.ReturnToPool(arrows);
  149. usedArrows.Remove(arrows);
  150. continue;
  151. }
  152. usedArrowIndex++;
  153. }
  154. trigger.enabled = true;
  155. }
  156. private void OnTriggerExit(Collider other)
  157. {
  158. if (other.CompareTag("bike"))
  159. {
  160. OnTriggerExitBicycle();
  161. trigger.enabled = false;
  162. }
  163. }
  164. private void OnCollisionEnter(Collision other)
  165. {
  166. Debug.Log("Colission");
  167. }
  168. private void OnTriggerEnter(Collider other)
  169. {
  170. Debug.Log("Trigger");
  171. }
  172. }
  173. }