StraightRoadExtras.cs 1.5 KB

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