DynamicReductionSource.cs 3.6 KB

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