Turn.cs 7.2 KB

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