Turn.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. public Action OnTriggerExitBicycle { get; set; }
  35. private void Awake()
  36. {
  37. arrowPool = FindObjectsOfType<Pool>().FirstOrDefault(o => o.CompareTag("ArrowPool"));
  38. arrowPoolAvailable = arrowPool != null;
  39. trigger = GetComponent<Collider>();
  40. if (!arrowPoolAvailable) Debug.LogWarning("Arrow Pool not found");
  41. }
  42. private void Start()
  43. {
  44. UpdateArrows();
  45. }
  46. [CanBeNull] protected abstract Transform RoadDirectionToTransform(RoadDirection position);
  47. private bool AddArrows(RoadDirection position, GameObject arrows)
  48. {
  49. Debug.Log($"---{gameObject.name}--- adding arrows at {position}");
  50. var t = RoadDirectionToTransform(position);
  51. if (t == null) return false;
  52. Debug.Log($" Transform for position => {t.gameObject.name}");
  53. SetRotation(arrows, position);
  54. arrows.transform.position = t.position;
  55. return true;
  56. }
  57. private void SetRotation(GameObject arrows, RoadDirection position)
  58. {
  59. var t = arrows.transform;
  60. //by default, the arrows show to east
  61. switch (position)
  62. {
  63. case RoadDirection.West:
  64. {
  65. if (turnDirection == TurnDirection.Left ||
  66. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  67. {
  68. t.rotation = Quaternion.Euler(0, 90, 0);
  69. //arrows.transform.Rotate(Vector3.up, 90);
  70. }
  71. else if (turnDirection == TurnDirection.Right ||
  72. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  73. {
  74. t.rotation = Quaternion.Euler(0, 270, 0);
  75. //arrows.transform.Rotate(Vector3.up, 90);
  76. }
  77. break;
  78. }
  79. case RoadDirection.North:
  80. if (turnDirection == TurnDirection.Left ||
  81. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  82. {
  83. t.rotation = Quaternion.Euler(0, 180, 0);
  84. //arrows.transform.Rotate(Vector3.up, 180);
  85. }
  86. break;
  87. case RoadDirection.East:
  88. if (turnDirection == TurnDirection.Right ||
  89. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  90. {
  91. t.rotation = Quaternion.Euler(0, 90, 0);
  92. //arrows.transform.Rotate(Vector3.up, 90);
  93. }
  94. else if (turnDirection == TurnDirection.Left ||
  95. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  96. {
  97. t.rotation = Quaternion.Euler(0, 270, 0);
  98. //arrows.transform.Rotate(Vector3.up, -90);
  99. }
  100. break;
  101. case RoadDirection.South:
  102. if (turnDirection == TurnDirection.Right ||
  103. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  104. {
  105. t.rotation = Quaternion.Euler(0, 180, 0);
  106. //arrows.transform.Rotate(Vector3.up, 180);
  107. }
  108. break;
  109. case RoadDirection.None:
  110. throw new ArgumentException("RoadDirection.None not allowed for adding arrows");
  111. default:
  112. throw new ArgumentOutOfRangeException(nameof(position), position, null);
  113. }
  114. }
  115. public void UpdateArrows()
  116. {
  117. if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None)
  118. {
  119. if (!arrowPoolAvailable) return;
  120. usedArrows.ForEach(o =>
  121. {
  122. // ReSharper disable once PossibleNullReferenceException
  123. arrowPool.ReturnToPool(o);
  124. });
  125. usedArrows.Clear();
  126. return;
  127. }
  128. turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
  129. var items = new[] {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
  130. var usedArrowIndex = 0;
  131. foreach (var position in items.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. }