123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using Controller.Bicycle;
- using SicknessReduction.Visual.Vignetting;
- using UnityEngine;
- namespace SicknessReduction
- {
- public enum ReductionValueSource
- {
- AngularVelocity,
- SteerAngle,
- SteerAngleAndSpeed
- }
- //TODO: also based on slopes
- public class DynamicReductionSource : MonoBehaviour
- {
- public RbBicycleController bikeController;
- [Header("Restriction Data")] public ReductionValueSource reductionSource;
- 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 QueueBasedRestrictionSuggestorVector3 angularVelocitySuggestor;
- private ValueBasedRestrictionSuggestor speedSuggestor;
-
- protected float currentValue;
- public float CurrentValue => currentValue;
- protected virtual void Start()
- {
- if (reductionSource == ReductionValueSource.AngularVelocity)
- {
- angularVelocitySuggestor =
- new QueueBasedRestrictionSuggestorVector3(threshold, minValue, maxValue, 180f); //deg/s
- }
- else if (reductionSource == ReductionValueSource.SteerAngle || reductionSource == ReductionValueSource.SteerAngleAndSpeed)
- {
- steerAngleSuggestor =
- new ValueBasedRestrictionSuggestor(threshold, minValue, maxValue,
- bikeController.maxSteeringAngle); //deg
- if (reductionSource == ReductionValueSource.SteerAngleAndSpeed)
- {
- speedSuggestor = new ValueBasedRestrictionSuggestor(0, minValue,
- maxValue, 25f);
- }
- }
- }
- protected virtual void Update()
- {
- float desiredValue;
- if (reductionSource == ReductionValueSource.AngularVelocity)
- {
- angularVelocitySuggestor.AddValue(bikeController.rigidBody.transform.localEulerAngles);
- desiredValue = angularVelocitySuggestor.Suggestion;
- }
- else
- {
- if(bikeController == null) Debug.LogError("bike controller = null!");
- steerAngleSuggestor.Value = bikeController.CurrentSteerAngle;
- if (reductionSource == ReductionValueSource.SteerAngle)
- {
- desiredValue = steerAngleSuggestor.Suggestion;
- }
- else
- {
- speedSuggestor.Value = bikeController.CurrentSpeed;
- var steerSuggestion = steerAngleSuggestor.Suggestion;
- var speedSuggestion = speedSuggestor.Suggestion;
- if (steerSuggestion == 0)
- {
- desiredValue = 0f;
- }
- else
- {
- desiredValue = (2 * steerSuggestion + speedSuggestion) / 3;
- }
- }
-
- }
- 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);
- }
- }
- }
|