RouteManager.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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++) routes[i].gameObject.SetActive(i == selectedRoute);
  18. routes[selectedRoute].OnStartEntered += OnOnStartEntered;
  19. routes[selectedRoute].OnFinishPassed += OnOnFinishPassed;
  20. PlaceBike();
  21. }
  22. private void PlaceBike()
  23. {
  24. var firstTurnPos = routes[selectedRoute].items.First().turn.transform.position;
  25. var startTransform = routes[selectedRoute].start.gameObject.transform;
  26. var startRotationEuler = startTransform.rotation.eulerAngles;
  27. var difStartFirstTurn = firstTurnPos - startTransform.position;
  28. var bikeDirection = Vector3.zero;
  29. if (difStartFirstTurn.x > 1)
  30. bikeDirection = new Vector3(-1, 0, 0);
  31. else if (difStartFirstTurn.z > 1)
  32. bikeDirection = new Vector3(0, 0, -1);
  33. else if (difStartFirstTurn.x < -1)
  34. bikeDirection = new Vector3(1, 0, 0);
  35. else if (difStartFirstTurn.z < -1) bikeDirection = new Vector3(0, 0, 1);
  36. bicycleGameObject = bicycle.gameObject;
  37. bicycleGameObject.transform.position =
  38. startTransform.position + bikeDirection * 12;
  39. bicycleGameObject.transform.LookAt(startTransform);
  40. }
  41. private void OnOnFinishPassed()
  42. {
  43. //bicycle.enabled = false;
  44. Instantiate(showOnFinish, bicycle.transform);
  45. Destroy(logging.gameObject);
  46. }
  47. private void OnOnStartEntered()
  48. {
  49. logging.gameObject.SetActive(true);
  50. }
  51. }
  52. }