SensorBikeController.cs 2.4 KB

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