EspController.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using UnityEngine;
  3. namespace SicknessReduction.Haptic
  4. {
  5. public abstract class EspController : MonoBehaviour
  6. {
  7. protected DataBroker Broker;
  8. protected bool DoUpdate { private set; get; }
  9. protected bool PreviousUpdateActive;
  10. private bool brokerAvailable;
  11. private float publishWaitTime = 0.05f; //20Hz
  12. private float lastTs = 0f;
  13. protected virtual async void Start()
  14. {
  15. Broker = DataBroker.Instance;
  16. await Broker.AwaitServerStarted();
  17. brokerAvailable = true;
  18. }
  19. protected virtual void Update()
  20. {
  21. if (!brokerAvailable)
  22. {
  23. DoUpdate = false;
  24. return;
  25. }
  26. var t = Time.time;
  27. if (t - lastTs >= publishWaitTime)
  28. {
  29. lastTs = t;
  30. DoUpdate = true;
  31. }
  32. else
  33. {
  34. DoUpdate = false;
  35. }
  36. }
  37. protected virtual void OnDestroy()
  38. {
  39. DataBroker.DisposeInstance();
  40. }
  41. }
  42. }