using Controller.Bicycle; using SicknessReduction.Visual.Vignetting; using UnityEngine; namespace SicknessReduction { //TODO: also based on slopes public class DynamicReductionSource : MonoBehaviour { public RbBicycleController bikeController; [Header("Restriction Data")] public float minValue = 0.3f; public float maxValue = 0.7f; public float maxValueChangePerSecond = 0.8f; [Tooltip("Depending on Vignetting source -> deg or deg/s")] public float threshold = 20f; private ValueBasedRestrictionSuggestor steerAngleSuggestor; private ValueAndTimeBasedRestrictionSuggestor speedSuggestor; protected float currentValue; public float CurrentValue => currentValue; protected virtual void Start() { if (bikeController == null) Debug.LogError("bike controller = null!"); steerAngleSuggestor = new ValueBasedRestrictionSuggestor(threshold, minValue, maxValue, bikeController.maxSteeringAngle); //deg speedSuggestor = new ValueAndTimeBasedRestrictionSuggestor(0, minValue, maxValue, 25f, 1.111f /*4kmh*/, 3); } protected virtual void Update() { steerAngleSuggestor.Value = bikeController.CurrentSteerAngle; speedSuggestor.UpdateValue(bikeController.CurrentSpeed); var steerSuggestion = steerAngleSuggestor.Suggestion; var speedSuggestion = speedSuggestor.Suggestion; var desiredValue = Mathf.Max(steerSuggestion, speedSuggestion); var desiredChange = desiredValue - currentValue; var maxChange = Time.deltaTime * maxValueChangePerSecond; var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange); currentValue = Mathf.Clamp(currentValue + change, 0, 1); } } }