StraightRoadExtras.cs 1.5 KB

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