FMSCountdown.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Routes;
  2. using UnityEngine;
  3. namespace Logging
  4. {
  5. public class FMSCountdown : MonoBehaviour
  6. {
  7. public RouteManager routeManager;
  8. public int fmsInterval = 30;
  9. private float startTime = -1f;
  10. private float timestampLastFMS;
  11. private GUIStyle bigRedTextStyle;
  12. private void Start()
  13. {
  14. bigRedTextStyle = new GUIStyle {fontSize = 72, normal = new GUIStyleState {textColor = Color.red}};
  15. var activeRoute = routeManager.routes[routeManager.selectedRoute];
  16. activeRoute.OnStartEntered += () =>
  17. {
  18. startTime = Time.time;
  19. timestampLastFMS = startTime;
  20. };
  21. activeRoute.OnFinishPassed += () => gameObject.SetActive(false);
  22. }
  23. private void OnGUI()
  24. {
  25. if (startTime < 0) return;
  26. var time = Time.time - startTime;
  27. var timeFromLastFms = Time.time - timestampLastFMS;
  28. GUI.TextField(new Rect(20, 85, 200, 20), $"Seconds Passed: {time:N2}");
  29. var distToInterval = fmsInterval - timeFromLastFms;
  30. if (distToInterval <= 1 || distToInterval >= fmsInterval - 2 && time > fmsInterval) //show a second before and 2 after
  31. {
  32. GUI.TextField(new Rect(225, 65, 120, 100), "FMS!", bigRedTextStyle);
  33. if (distToInterval <= 0) // set timestamp for next interval
  34. {
  35. timestampLastFMS = Time.time;
  36. }
  37. }
  38. }
  39. }
  40. }