StraightRoadExtras.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using Controller.Bicycle;
  3. using Pools;
  4. using Routes;
  5. using UnityEngine;
  6. namespace Roads
  7. {
  8. [RequireComponent(typeof(BoxCollider))]
  9. public class StraightRoadExtras : MonoBehaviour, IRoad
  10. {
  11. public GameObject arcPrefab;
  12. private GameObject arc;
  13. private bool hasArc;
  14. public TriggerState ArcState { get; private set; } = TriggerState.Outside;
  15. public float SlopeDeg { get; private set; }
  16. public float MinY { get; private set; }
  17. public delegate void OnArcEnteredEvent();
  18. public delegate void OnArcPassedEvent();
  19. public event OnArcEnteredEvent OnArcEntered;
  20. public event OnArcPassedEvent OnArcPassed;
  21. private void Start()
  22. {
  23. SlopeDeg = transform.localRotation.eulerAngles.z;
  24. MinY = GetComponent<BoxCollider>().bounds.center.y;
  25. }
  26. public void ShowArc()
  27. {
  28. arc = Instantiate(arcPrefab, transform);
  29. hasArc = arc != null;
  30. }
  31. private void OnTriggerEnter(Collider other)
  32. {
  33. if (other.CompareTag("bike"))
  34. {
  35. if (hasArc)
  36. {
  37. OnArcEntered?.Invoke();
  38. ArcState = TriggerState.Inside;
  39. }
  40. }
  41. }
  42. private void OnTriggerExit(Collider other)
  43. {
  44. if (hasArc && other.CompareTag("bike"))
  45. {
  46. Destroy(arc);
  47. ArcState = TriggerState.Outside;
  48. OnArcPassed?.Invoke();
  49. }
  50. }
  51. }
  52. }