SensorBikeController.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 enum SteeringMode { frontWheel, Leaning, HMD };
  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 SteeringMode steeringSelection;
  27. private IBicycleController bicycleController;
  28. private bool isFrontWheelTrackerNotNull;
  29. private BikeSensorData sensorData;
  30. private GameObject player;
  31. private void Start()
  32. {
  33. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  34. bicycleController = GetComponent<IBicycleController>();
  35. sensorData = BikeSensorData.Instance;
  36. player = GameObject.FindGameObjectWithTag("HMDTracker");
  37. // Dummy assignment
  38. steeringSelection = SteeringMode.HMD;
  39. }
  40. private void Update()
  41. {
  42. var speedData = sensorData.SpeedData;
  43. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  44. if (steer) SetSteer();
  45. if (lean) SetLeaningAngle();
  46. }
  47. private void SetSteer()
  48. {
  49. Debug.Log("Updating Steering");
  50. switch (steeringSelection) {
  51. case SteeringMode.frontWheel:
  52. if (isFrontWheelTrackerNotNull) {
  53. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation *2f;
  54. }
  55. break;
  56. case SteeringMode.Leaning:
  57. // TBD
  58. bicycleController.CurrentSteerAngle = 0;
  59. break;
  60. case SteeringMode.HMD:
  61. // TODO: Convert steering relative to bike
  62. bicycleController.CurrentSteerAngle = player.transform.rotation.y;
  63. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  64. break;
  65. }
  66. }
  67. private void SetLeaningAngle()
  68. {
  69. //bicycleController.CurrentLeaningAngle =
  70. }
  71. private void SetSpeed(SpeedSensorData speedData)
  72. {
  73. bicycleController.CurrentSpeed = speedData.Speed;
  74. }
  75. }
  76. }