SensorBikeController.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using Controller.Bicycle;
  3. using Controller.Lean;
  4. using Sensors;
  5. using Sensors.ANT;
  6. using Sensors.Polar;
  7. using Tracking;
  8. using UnityEngine;
  9. using UnityEngine.Serialization;
  10. namespace Controller
  11. {
  12. [Serializable]
  13. public struct FrontWheelTrackerConfig
  14. {
  15. public FrontWheelTracker frontWheelTracker;
  16. public float multiplicator;
  17. public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
  18. }
  19. [RequireComponent(typeof(IBicycleController))]
  20. public class SensorBikeController : MonoBehaviour
  21. {
  22. public PolarRotationMapping polarRotationMapping;
  23. public SpeedSensorConfig speedSensorConfig;
  24. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  25. public bool steer = true;
  26. public bool accelerate = true;
  27. public bool lean = true;
  28. private IBicycleController bicycleController;
  29. private BikeSensorData sensorData;
  30. private bool isFrontWheelTrackerNotNull;
  31. private void Start()
  32. {
  33. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  34. bicycleController = GetComponent<IBicycleController>();
  35. sensorData = BikeSensorData.Instance;
  36. sensorData.StartListening(speedSensorConfig);
  37. }
  38. private void Update()
  39. {
  40. var speedData = sensorData.SpeedData;
  41. if (speedData != null && accelerate)
  42. {
  43. SetSpeed(speedData.Value);
  44. }
  45. if (isFrontWheelTrackerNotNull && steer)
  46. {
  47. SetSteer();
  48. }
  49. if (lean)
  50. {
  51. SetLeaningAngle();
  52. }
  53. }
  54. private void SetSteer()
  55. {
  56. bicycleController.CurrentSteerAngle =
  57. frontWheelTrackerConfig.AdjustedRotation;
  58. }
  59. private void OnDestroy()
  60. {
  61. sensorData?.Dispose();
  62. }
  63. private void SetLeaningAngle()
  64. {
  65. //bicycleController.CurrentLeaningAngle =
  66. }
  67. private void SetSpeed(SpeedSensorData speedData)
  68. {
  69. bicycleController.CurrentSpeed = speedData.Speed;
  70. }
  71. }
  72. }