Turn.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using JetBrains.Annotations;
  5. using Pools;
  6. using Roads;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using Valve.VR.InteractionSystem;
  10. namespace Routes
  11. {
  12. public enum TriggerState
  13. {
  14. Inside,
  15. Outside,
  16. }
  17. public enum RoadDirection
  18. {
  19. West,
  20. North,
  21. East,
  22. South,
  23. None
  24. }
  25. public class RoadDirectionHelper
  26. {
  27. public static RoadDirection OppositeOf(RoadDirection direction)
  28. {
  29. switch (direction)
  30. {
  31. case RoadDirection.West:
  32. return RoadDirection.East;
  33. case RoadDirection.North:
  34. return RoadDirection.South;
  35. case RoadDirection.East:
  36. return RoadDirection.West;
  37. case RoadDirection.South:
  38. return RoadDirection.North;
  39. case RoadDirection.None:
  40. return RoadDirection.None;
  41. default:
  42. throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
  43. }
  44. }
  45. }
  46. public enum TurnDirection
  47. {
  48. Right,
  49. Left,
  50. Straight
  51. }
  52. public abstract class Turn : MonoBehaviour, IRoad
  53. {
  54. public RoadDirection comingFrom;
  55. public RoadDirection goingTo;
  56. [CanBeNull] private Pool arrowPool;
  57. private bool arrowPoolAvailable;
  58. private TurnDirection turnDirection;
  59. private List<GameObject> usedArrows = new List<GameObject>();
  60. private Collider trigger;
  61. private static readonly RoadDirection[] allDirections =
  62. {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
  63. public Action OnTriggerExitBicycle { get; set; }
  64. public TriggerState BikeTriggerState { get; private set; } = TriggerState.Outside;
  65. public float SlopeDeg { get; private set; }
  66. public float MinY { get; private set; }
  67. private void Awake()
  68. {
  69. arrowPool = FindObjectsOfType<Pool>().FirstOrDefault(o => o.CompareTag("ArrowPool"));
  70. arrowPoolAvailable = arrowPool != null;
  71. trigger = GetComponent<Collider>();
  72. if (!arrowPoolAvailable) Debug.LogWarning("Arrow Pool not found");
  73. }
  74. private void Start()
  75. {
  76. SlopeDeg = transform.localRotation.eulerAngles.z;
  77. MinY = GetComponent<BoxCollider>().bounds.center.y;
  78. UpdateArrows();
  79. }
  80. [CanBeNull]
  81. protected abstract Transform RoadDirectionToTransform(RoadDirection position);
  82. private bool AddArrows(RoadDirection position, GameObject arrows)
  83. {
  84. //Debug.Log($"---{gameObject.name}--- adding arrows at {position}");
  85. var t = RoadDirectionToTransform(position);
  86. if (t == null) return false;
  87. //Debug.Log($" Transform for position => {t.gameObject.name}");
  88. SetRotation(arrows, position);
  89. arrows.transform.position = t.position;
  90. return true;
  91. }
  92. private void SetRotation(GameObject arrows, RoadDirection position)
  93. {
  94. var t = arrows.transform;
  95. //by default, the arrows show to east
  96. switch (position)
  97. {
  98. case RoadDirection.West:
  99. {
  100. if (turnDirection == TurnDirection.Left ||
  101. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  102. {
  103. t.rotation = Quaternion.Euler(0, 90, 0);
  104. //arrows.transform.Rotate(Vector3.up, 90);
  105. }
  106. else if (turnDirection == TurnDirection.Right ||
  107. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  108. {
  109. t.rotation = Quaternion.Euler(0, 270, 0);
  110. //arrows.transform.Rotate(Vector3.up, 90);
  111. }
  112. break;
  113. }
  114. case RoadDirection.North:
  115. if (turnDirection == TurnDirection.Left ||
  116. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  117. {
  118. t.rotation = Quaternion.Euler(0, 180, 0);
  119. //arrows.transform.Rotate(Vector3.up, 180);
  120. }
  121. break;
  122. case RoadDirection.East:
  123. if (turnDirection == TurnDirection.Right ||
  124. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
  125. {
  126. t.rotation = Quaternion.Euler(0, 90, 0);
  127. //arrows.transform.Rotate(Vector3.up, 90);
  128. }
  129. else if (turnDirection == TurnDirection.Left ||
  130. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
  131. {
  132. t.rotation = Quaternion.Euler(0, 270, 0);
  133. //arrows.transform.Rotate(Vector3.up, -90);
  134. }
  135. break;
  136. case RoadDirection.South:
  137. if (turnDirection == TurnDirection.Right ||
  138. turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
  139. {
  140. t.rotation = Quaternion.Euler(0, 180, 0);
  141. //arrows.transform.Rotate(Vector3.up, 180);
  142. }
  143. break;
  144. case RoadDirection.None:
  145. throw new ArgumentException("RoadDirection.None not allowed for adding arrows");
  146. default:
  147. throw new ArgumentOutOfRangeException(nameof(position), position, null);
  148. }
  149. }
  150. public void UpdateArrows()
  151. {
  152. if (!arrowPoolAvailable) return;
  153. if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None)
  154. {
  155. usedArrows.ForEach(o =>
  156. {
  157. // ReSharper disable once PossibleNullReferenceException
  158. arrowPool.ReturnToPool(o);
  159. });
  160. usedArrows.Clear();
  161. return;
  162. }
  163. turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
  164. var usedArrowIndex = 0;
  165. foreach (var position in allDirections.Where(i => i != comingFrom && i != goingTo))
  166. {
  167. GameObject arrows;
  168. if (usedArrowIndex < usedArrows.Count)
  169. {
  170. arrows = usedArrows[usedArrowIndex];
  171. }
  172. else
  173. {
  174. // ReSharper disable once PossibleNullReferenceException
  175. arrows = arrowPool.GetItem();
  176. usedArrows.Add(arrows);
  177. }
  178. var used = AddArrows(position, arrows);
  179. if (!used)
  180. {
  181. // ReSharper disable once PossibleNullReferenceException
  182. arrowPool.ReturnToPool(arrows);
  183. usedArrows.Remove(arrows);
  184. continue;
  185. }
  186. usedArrowIndex++;
  187. }
  188. trigger.enabled = true;
  189. }
  190. private void OnTriggerExit(Collider other)
  191. {
  192. if (other.CompareTag("bike") && OnTriggerExitBicycle != null)
  193. {
  194. OnTriggerExitBicycle();
  195. trigger.enabled = false;
  196. BikeTriggerState = TriggerState.Outside;
  197. }
  198. }
  199. private void OnTriggerEnter(Collider other)
  200. {
  201. if (other.CompareTag("bike"))
  202. {
  203. BikeTriggerState = TriggerState.Inside;
  204. }
  205. }
  206. }
  207. }