DynamicReductionSource.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using Controller.Bicycle;
  3. using SicknessReduction.Visual.Vignetting;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. using Wheels;
  7. namespace SicknessReduction
  8. {
  9. public class DynamicReductionSource : MonoBehaviour
  10. {
  11. #region vars
  12. public RbBicycleController bikeController;
  13. public LerpSlopeCollider slopeCollider;
  14. [Header("Configuration")] public bool useSpeed = true;
  15. public bool useSteer = true;
  16. public bool useSlope = true;
  17. public float minSpeedForRestriction = 0.28f;
  18. public float speedMultiplier = 1f;
  19. public float slopeMultiplier = 1f;
  20. public float steerMultiplier = 1f;
  21. [Header("Restriction Data")] public float minValue = 0.3f;
  22. public float maxValue = 0.7f;
  23. public float maxValueChangePerSecond = 0.8f;
  24. [FormerlySerializedAs("threshold")] [Tooltip("Depending on Vignetting source -> deg or deg/s")]
  25. public float steerThreshold = 4f;
  26. public float speedThreshold = 1f;
  27. public float slopeThreshold = 1f;
  28. public float maxValueAtSpeed = 8.33f; //30kmh
  29. public float maxValueAtSteerAngle = 15f;
  30. public float maxValueAtSlope = 8.531f;
  31. private float currentSlopeDeg;
  32. private ValueBasedRestrictionSuggestor steerAngleSuggestor;
  33. private ValueBasedRestrictionSuggestor slopeSuggestor;
  34. private ValueAndTimeBasedRestrictionSuggestor speedSuggestor;
  35. protected float currentValue;
  36. public float CurrentValue => currentValue;
  37. #endregion
  38. protected virtual void Start()
  39. {
  40. if (bikeController == null) Debug.LogError("bike controller = null!");
  41. if (useSteer)
  42. steerAngleSuggestor =
  43. new ValueBasedRestrictionSuggestor(steerThreshold, minValue, maxValue,
  44. maxValueAtSteerAngle); //deg
  45. if (useSpeed)
  46. speedSuggestor = new ValueAndTimeBasedRestrictionSuggestor(speedThreshold, minValue,
  47. maxValue, maxValueAtSpeed, 1.111f /*4kmh*/, 3);
  48. if (useSlope)
  49. {
  50. slopeCollider.OnSlopeChanged += (timestamp, slope) => currentSlopeDeg = slope;
  51. slopeSuggestor =
  52. new ValueBasedRestrictionSuggestor(slopeThreshold, minValue, maxValue, maxValueAtSlope);
  53. }
  54. }
  55. protected virtual void Update()
  56. {
  57. if (bikeController.CurrentSpeed < minSpeedForRestriction)
  58. {
  59. currentValue = 0;
  60. return;
  61. }
  62. if (useSteer) steerAngleSuggestor.Value = bikeController.CurrentSteerAngle;
  63. if (useSpeed) speedSuggestor.UpdateValue(bikeController.CurrentSpeed);
  64. if (useSlope) slopeSuggestor.Value = currentSlopeDeg;
  65. var steerSuggestion = useSteer ? steerAngleSuggestor.Suggestion * steerMultiplier : 0f;
  66. var speedSuggestion = useSpeed ? speedSuggestor.Suggestion * speedMultiplier : 0f;
  67. var slopeSuggestion = useSlope ? slopeSuggestor.Suggestion * slopeMultiplier : 0f;
  68. var maxSuggestion = Mathf.Max(steerSuggestion, speedSuggestion, slopeSuggestion);
  69. var desiredValue = Mathf.Min(maxSuggestion, maxValue);
  70. var desiredChange = desiredValue - currentValue;
  71. var maxChange = Time.deltaTime * maxValueChangePerSecond;
  72. var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange);
  73. currentValue = Mathf.Clamp(currentValue + change, 0, 1);
  74. //Debug.Log($"SuggestionValue = {currentValue}");
  75. }
  76. }
  77. }