Turn.cs 7.5 KB

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