Turn.cs 5.9 KB

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