DynamicReductionSource.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #region vars
  11. private const float MAX_SLOPE_ANGLE = 8.531f;
  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 speedMultiplier = 1f;
  18. public float slopeMultiplier = 1f;
  19. public float steerMultiplier = 1f;
  20. [Header("Restriction Data")] public float minValue = 0.3f;
  21. public float maxValue = 0.7f;
  22. public float maxValueChangePerSecond = 0.8f;
  23. [FormerlySerializedAs("threshold")] [Tooltip("Depending on Vignetting source -> deg or deg/s")]
  24. public float steerThreshold = 20f;
  25. public float speedThreshold = 1f;
  26. public float slopeThreshold = 1f;
  27. private float currentSlopeDeg;
  28. private ValueBasedRestrictionSuggestor steerAngleSuggestor;
  29. private ValueBasedRestrictionSuggestor slopeSuggestor;
  30. private ValueAndTimeBasedRestrictionSuggestor speedSuggestor;
  31. protected float currentValue;
  32. public float CurrentValue => currentValue;
  33. #endregion
  34. protected virtual void Start()
  35. {
  36. if (bikeController == null) Debug.LogError("bike controller = null!");
  37. if (useSteer)
  38. steerAngleSuggestor =
  39. new ValueBasedRestrictionSuggestor(steerThreshold, minValue, maxValue,
  40. bikeController.maxSteeringAngle); //deg
  41. if (useSpeed)
  42. speedSuggestor = new ValueAndTimeBasedRestrictionSuggestor(speedThreshold, minValue,
  43. maxValue, 25f, 1.111f /*4kmh*/, 3);
  44. if (useSlope)
  45. {
  46. slopeCollider.OnSlopeChanged += (timestamp, slope) => currentSlopeDeg = slope;
  47. slopeSuggestor =
  48. new ValueBasedRestrictionSuggestor(slopeThreshold, minValue, maxValue, MAX_SLOPE_ANGLE);
  49. }
  50. }
  51. protected virtual void Update()
  52. {
  53. if (useSteer) steerAngleSuggestor.Value = bikeController.CurrentSteerAngle;
  54. if (useSpeed) speedSuggestor.UpdateValue(bikeController.CurrentSpeed);
  55. if (useSlope) slopeSuggestor.Value = currentSlopeDeg;
  56. var steerSuggestion = useSteer ? steerAngleSuggestor.Suggestion * steerMultiplier : 0f;
  57. var speedSuggestion = useSpeed ? speedSuggestor.Suggestion * speedMultiplier : 0f;
  58. var slopeSuggestion = useSlope ? slopeSuggestor.Suggestion * slopeMultiplier : 0f;
  59. var desiredValue = Mathf.Min(Mathf.Max(steerSuggestion, speedSuggestion, slopeSuggestion), maxValue);
  60. var desiredChange = desiredValue - currentValue;
  61. var maxChange = Time.deltaTime * maxValueChangePerSecond;
  62. var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange);
  63. currentValue = Mathf.Clamp(currentValue + change, 0, 1);
  64. //Debug.Log($"SuggestionValue = {currentValue}");
  65. }
  66. }
  67. }