SensorBikeController.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.frontWheel;
  39. }
  40. private void Update()
  41. {
  42. Debug.Log("Bike Sensor Controller called");
  43. var speedData = sensorData.SpeedData;
  44. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  45. if (steer) SetSteer();
  46. if (lean) SetLeaningAngle();
  47. }
  48. private void SetSteer()
  49. {
  50. Debug.Log("Updating Steering");
  51. switch (steeringSelection) {
  52. case SteeringMode.frontWheel:
  53. if (isFrontWheelTrackerNotNull) {
  54. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation;
  55. }
  56. break;
  57. case SteeringMode.Leaning:
  58. // TBD
  59. bicycleController.CurrentSteerAngle = 0;
  60. break;
  61. case SteeringMode.HMD:
  62. // TODO: Convert steering relative to bike
  63. Debug.Log("HMD Rotation " + player.transform.eulerAngles);
  64. bicycleController.CurrentSteerAngle = player.transform.eulerAngles.y;
  65. break;
  66. }
  67. //Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  68. }
  69. private void SetLeaningAngle()
  70. {
  71. //bicycleController.CurrentLeaningAngle =
  72. }
  73. private void SetSpeed(SpeedSensorData speedData)
  74. {
  75. bicycleController.CurrentSpeed = speedData.Speed;
  76. }
  77. }
  78. }