DynamicReductionSource.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. if (Math.Abs(maxSuggestion - steerSuggestion) < 0.01f)
  70. {
  71. Debug.Log("Suggestion won by Steer");
  72. }else if (Math.Abs(maxSuggestion - speedSuggestion) < 0.01f)
  73. {
  74. Debug.Log("Suggestion won by Speed");
  75. }else if (Math.Abs(maxSuggestion - slopeSuggestion) < 0.01f)
  76. {
  77. Debug.Log("Suggestion won by Slope");
  78. }
  79. var desiredValue = Mathf.Min(maxSuggestion, maxValue);
  80. var desiredChange = desiredValue - currentValue;
  81. var maxChange = Time.deltaTime * maxValueChangePerSecond;
  82. var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange);
  83. currentValue = Mathf.Clamp(currentValue + change, 0, 1);
  84. Debug.Log($"SuggestionValue = {currentValue}");
  85. }
  86. }
  87. }