SensorBikeController.cs 1.8 KB

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