EspController.cs 1.1 KB

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