DynamicReductionSource.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Controller.Bicycle;
  2. using SicknessReduction.Visual.Vignetting;
  3. using UnityEngine;
  4. namespace SicknessReduction
  5. {
  6. public enum ReductionValueSource
  7. {
  8. AngularVelocity,
  9. SteerAngle,
  10. SteerAngleAndSpeed
  11. }
  12. //TODO: also based on slopes
  13. public class DynamicReductionSource : MonoBehaviour
  14. {
  15. public RbBicycleController bikeController;
  16. [Header("Restriction Data")] public ReductionValueSource reductionSource;
  17. public float minValue = 0.3f;
  18. public float maxValue = 0.7f;
  19. public float maxValueChangePerSecond = 0.8f;
  20. [Tooltip("Depending on Vignetting source -> deg or deg/s")]
  21. public float threshold = 20f;
  22. private ValueBasedRestrictionSuggestor steerAngleSuggestor;
  23. private QueueBasedRestrictionSuggestorVector3 angularVelocitySuggestor;
  24. private ValueBasedRestrictionSuggestor speedSuggestor;
  25. protected float currentValue;
  26. public float CurrentValue => currentValue;
  27. protected virtual void Start()
  28. {
  29. if (reductionSource == ReductionValueSource.AngularVelocity)
  30. {
  31. angularVelocitySuggestor =
  32. new QueueBasedRestrictionSuggestorVector3(threshold, minValue, maxValue, 180f); //deg/s
  33. }
  34. else if (reductionSource == ReductionValueSource.SteerAngle || reductionSource == ReductionValueSource.SteerAngleAndSpeed)
  35. {
  36. steerAngleSuggestor =
  37. new ValueBasedRestrictionSuggestor(threshold, minValue, maxValue,
  38. bikeController.maxSteeringAngle); //deg
  39. if (reductionSource == ReductionValueSource.SteerAngleAndSpeed)
  40. {
  41. speedSuggestor = new ValueBasedRestrictionSuggestor(0, minValue,
  42. maxValue, 25f);
  43. }
  44. }
  45. }
  46. protected virtual void Update()
  47. {
  48. float desiredValue;
  49. if (reductionSource == ReductionValueSource.AngularVelocity)
  50. {
  51. angularVelocitySuggestor.AddValue(bikeController.rigidBody.transform.localEulerAngles);
  52. desiredValue = angularVelocitySuggestor.Suggestion;
  53. }
  54. else
  55. {
  56. if(bikeController == null) Debug.LogError("bike controller = null!");
  57. steerAngleSuggestor.Value = bikeController.CurrentSteerAngle;
  58. if (reductionSource == ReductionValueSource.SteerAngle)
  59. {
  60. desiredValue = steerAngleSuggestor.Suggestion;
  61. }
  62. else
  63. {
  64. speedSuggestor.Value = bikeController.CurrentSpeed;
  65. var steerSuggestion = steerAngleSuggestor.Suggestion;
  66. var speedSuggestion = speedSuggestor.Suggestion;
  67. if (steerSuggestion == 0)
  68. {
  69. desiredValue = 0f;
  70. }
  71. else
  72. {
  73. desiredValue = (2 * steerSuggestion + speedSuggestion) / 3;
  74. }
  75. }
  76. }
  77. var desiredChange = desiredValue - currentValue;
  78. var maxChange = Time.deltaTime * maxValueChangePerSecond;
  79. var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange);
  80. currentValue = Mathf.Clamp(currentValue + change, 0, 1);
  81. }
  82. }
  83. }