12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using Controller.Bicycle;
- using Controller.Lean;
- using Sensors;
- using Sensors.ANT;
- using Sensors.Polar;
- using Tracking;
- using UnityEngine;
- using UnityEngine.Serialization;
- namespace Controller
- {
- [Serializable]
- public struct FrontWheelTrackerConfig
- {
- public FrontWheelTracker frontWheelTracker;
- public float multiplicator;
- public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
- }
- [RequireComponent(typeof(IBicycleController))]
- [RequireComponent(typeof(ILeanController))]
- public class SensorBikeController : MonoBehaviour
- {
- public PolarRotationMapping polarRotationMapping;
- public SpeedSensorConfig speedSensorConfig;
- private ILeanController leanController;
- public FrontWheelTrackerConfig frontWheelTrackerConfig;
- public bool steer = true;
- public bool accelerate = true;
- public bool lean = true;
- private IBicycleController bicycleController;
- private BikeSensorData sensorData;
- private bool isFrontWheelTrackerNotNull;
- private void Start()
- {
- leanController = GetComponent<ILeanController>();
- isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
- bicycleController = GetComponent<IBicycleController>();
- sensorData = BikeSensorData.Instance;
- sensorData.StartListening(speedSensorConfig);
- }
- private void Update()
- {
- var speedData = sensorData.SpeedData;
- if (speedData != null && accelerate)
- {
- SetSpeed(speedData.Value);
- }
- if (isFrontWheelTrackerNotNull && steer)
- {
- SetSteer();
- }
- if (lean)
- {
- SetLeaningAngle();
- }
- }
- private void SetSteer()
- {
- bicycleController.CurrentSteerAngle =
- frontWheelTrackerConfig.AdjustedRotation;
- }
- private void OnDestroy()
- {
- sensorData?.Dispose();
- }
- private void SetLeaningAngle()
- {
- //bicycleController.CurrentLeaningAngle =
- bicycleController.CurrentLeaningAngle = leanController.LeanAngle;
- }
- private void SetSpeed(SpeedSensorData speedData)
- {
- bicycleController.CurrentSpeed = speedData.Speed;
- }
- }
- }
|