DynamicReductionSource.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Controller.Bicycle;
  2. using SicknessReduction.Visual.Vignetting;
  3. using UnityEngine;
  4. using UnityEngine.Serialization;
  5. namespace SicknessReduction
  6. {
  7. //TODO: also based on slopes
  8. public class DynamicReductionSource : MonoBehaviour
  9. {
  10. private const float MAX_SLOPE_ANGLE = 8.531f;
  11. public RbBicycleController bikeController;
  12. [Header("Configuration")] public bool useSpeed = true;
  13. public bool useSteer = true;
  14. public bool useSlope = true;
  15. public float speedMultiplier = 1f;
  16. public float slopeMultiplier = 1f;
  17. public float steerMultiplier = 1f;
  18. [Header("Restriction Data")] public float minValue = 0.3f;
  19. public float maxValue = 0.7f;
  20. public float maxValueChangePerSecond = 0.8f;
  21. [FormerlySerializedAs("threshold")] [Tooltip("Depending on Vignetting source -> deg or deg/s")]
  22. public float steerThreshold = 20f;
  23. public float speedThreshold = 1f;
  24. public float slopeThreshold = 1f;
  25. private ValueBasedRestrictionSuggestor steerAngleSuggestor;
  26. private ValueBasedRestrictionSuggestor slopeSuggestor;
  27. private ValueAndTimeBasedRestrictionSuggestor speedSuggestor;
  28. protected float currentValue;
  29. public float CurrentValue => currentValue;
  30. protected virtual void Start()
  31. {
  32. if (bikeController == null) Debug.LogError("bike controller = null!");
  33. if (useSteer)
  34. steerAngleSuggestor =
  35. new ValueBasedRestrictionSuggestor(steerThreshold, minValue, maxValue,
  36. bikeController.maxSteeringAngle); //deg
  37. if (useSpeed)
  38. speedSuggestor = new ValueAndTimeBasedRestrictionSuggestor(speedThreshold, minValue,
  39. maxValue, 25f, 1.111f /*4kmh*/, 3);
  40. if (useSlope)
  41. slopeSuggestor =
  42. new ValueBasedRestrictionSuggestor(slopeThreshold, minValue, maxValue, MAX_SLOPE_ANGLE);
  43. }
  44. protected virtual void Update()
  45. {
  46. if (useSteer) steerAngleSuggestor.Value = bikeController.CurrentSteerAngle;
  47. if (useSpeed) speedSuggestor.UpdateValue(bikeController.CurrentSpeed);
  48. if (useSlope) slopeSuggestor.Value = bikeController.CurrentGradientDeg;
  49. var steerSuggestion = useSteer ? steerAngleSuggestor.Suggestion * steerMultiplier : 0f;
  50. var speedSuggestion = useSpeed ? speedSuggestor.Suggestion * speedMultiplier : 0f;
  51. var slopeSuggestion = useSlope ? slopeSuggestor.Suggestion * slopeMultiplier : 0f;
  52. var desiredValue = Mathf.Min(Mathf.Max(steerSuggestion, speedSuggestion, slopeSuggestion), maxValue);
  53. var desiredChange = desiredValue - currentValue;
  54. var maxChange = Time.deltaTime * maxValueChangePerSecond;
  55. var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange);
  56. currentValue = Mathf.Clamp(currentValue + change, 0, 1);
  57. Debug.Log($"SuggestionValue = {currentValue}");
  58. }
  59. }
  60. }