1
0

SensorBikeController.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 FrontWheelTrackerConfig frontWheelTrackerConfig;
  24. public bool steer = true;
  25. public bool accelerate = true;
  26. public bool lean = true;
  27. private IBicycleController bicycleController;
  28. private BikeSensorData sensorData;
  29. private bool isFrontWheelTrackerNotNull;
  30. private void Start()
  31. {
  32. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  33. bicycleController = GetComponent<IBicycleController>();
  34. sensorData = BikeSensorData.Instance;
  35. }
  36. private void Update()
  37. {
  38. var speedData = sensorData.SpeedData;
  39. if (speedData != null && accelerate)
  40. {
  41. SetSpeed(speedData.Value);
  42. }
  43. if (isFrontWheelTrackerNotNull && steer)
  44. {
  45. SetSteer();
  46. }
  47. if (lean)
  48. {
  49. SetLeaningAngle();
  50. }
  51. }
  52. private void SetSteer()
  53. {
  54. bicycleController.CurrentSteerAngle =
  55. frontWheelTrackerConfig.AdjustedRotation;
  56. }
  57. private void SetLeaningAngle()
  58. {
  59. //bicycleController.CurrentLeaningAngle =
  60. }
  61. private void SetSpeed(SpeedSensorData speedData)
  62. {
  63. bicycleController.CurrentSpeed = speedData.Speed;
  64. }
  65. }
  66. }