RouteManager.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Linq;
  2. using Controller.Bicycle;
  3. using UnityEngine;
  4. namespace Routes
  5. {
  6. public class RouteManager : MonoBehaviour
  7. {
  8. public Route[] routes;
  9. public int selectedRoute;
  10. public int visibleLength = 3;
  11. public RbBicycleController bicycle;
  12. public Logging.Base.Logging logging;
  13. public GameObject showOnFinish;
  14. private GameObject bicycleGameObject;
  15. private void Start()
  16. {
  17. for (var i = 0; i < routes.Length; i++)
  18. {
  19. routes[i].gameObject.SetActive(i == selectedRoute);
  20. }
  21. routes[selectedRoute].OnStartEntered += OnOnStartEntered;
  22. routes[selectedRoute].OnFinishPassed += OnOnFinishPassed;
  23. PlaceBike();
  24. }
  25. private void PlaceBike()
  26. {
  27. var firstTurnPos = routes[selectedRoute].items.First().turn.transform.position;
  28. var startTransform = routes[selectedRoute].start.gameObject.transform;
  29. var startRotationEuler = startTransform.rotation.eulerAngles;
  30. var difStartFirstTurn = firstTurnPos - startTransform.position;
  31. Vector3 bikeDirection = Vector3.zero;
  32. Vector3 bikeRotation = Vector3.zero;
  33. if (difStartFirstTurn.x > 1 || difStartFirstTurn.z > 1)
  34. {
  35. bikeDirection = startTransform.right;
  36. bikeRotation = new Vector3(0, -90, 0);
  37. }
  38. /*else if (difStartFirstTurn.z > 1)
  39. {
  40. bikeDirection = startTransform.forward;
  41. bikeRotation = new Vector3(0, -180, 0);
  42. }*/
  43. else if (difStartFirstTurn.x < -1 || difStartFirstTurn.z < -1)
  44. {
  45. bikeDirection = -startTransform.right;
  46. bikeRotation = new Vector3(0, 90, 0);
  47. }
  48. /*else if (difStartFirstTurn.z < -1)
  49. {
  50. bikeDirection = -startTransform.forward;
  51. bikeRotation = new Vector3(0, 180, 0);
  52. }*/
  53. bicycleGameObject = bicycle.gameObject;
  54. bicycleGameObject.transform.SetPositionAndRotation(
  55. startTransform.position + bikeDirection * 12,
  56. Quaternion.Euler(startRotationEuler + bikeRotation));
  57. }
  58. private void OnOnFinishPassed()
  59. {
  60. //bicycle.enabled = false;
  61. Instantiate(showOnFinish, bicycle.transform);
  62. Destroy(logging.gameObject);
  63. }
  64. private void OnOnStartEntered()
  65. {
  66. logging.gameObject.SetActive(true);
  67. }
  68. }
  69. }