123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using Controller.Bicycle;
- using SicknessReduction.Visual.Vignetting;
- using UnityEngine;
- using UnityEngine.Serialization;
- using Wheels;
- namespace SicknessReduction
- {
- public class DynamicReductionSource : MonoBehaviour
- {
- #region vars
- private const float MAX_SLOPE_ANGLE = 8.531f;
- public RbBicycleController bikeController;
- public LerpSlopeCollider slopeCollider;
- [Header("Configuration")] public bool useSpeed = true;
- public bool useSteer = true;
- public bool useSlope = true;
- public float minSpeedForRestriction = 0.28f;
- public float speedMultiplier = 1f;
- public float slopeMultiplier = 1f;
- public float steerMultiplier = 1f;
- [Header("Restriction Data")] public float minValue = 0.3f;
- public float maxValue = 0.7f;
- public float maxValueChangePerSecond = 0.8f;
- [FormerlySerializedAs("threshold")] [Tooltip("Depending on Vignetting source -> deg or deg/s")]
- public float steerThreshold = 20f;
- public float speedThreshold = 1f;
- public float slopeThreshold = 1f;
- private float currentSlopeDeg;
- private ValueBasedRestrictionSuggestor steerAngleSuggestor;
- private ValueBasedRestrictionSuggestor slopeSuggestor;
- private ValueAndTimeBasedRestrictionSuggestor speedSuggestor;
- protected float currentValue;
- public float CurrentValue => currentValue;
- #endregion
-
- protected virtual void Start()
- {
- if (bikeController == null) Debug.LogError("bike controller = null!");
- if (useSteer)
- steerAngleSuggestor =
- new ValueBasedRestrictionSuggestor(steerThreshold, minValue, maxValue,
- bikeController.maxSteeringAngle);
- if (useSpeed)
- speedSuggestor = new ValueAndTimeBasedRestrictionSuggestor(speedThreshold, minValue,
- maxValue, 25f, 1.111f , 3);
- if (useSlope)
- {
- slopeCollider.OnSlopeChanged += (timestamp, slope) => currentSlopeDeg = slope;
- slopeSuggestor =
- new ValueBasedRestrictionSuggestor(slopeThreshold, minValue, maxValue, MAX_SLOPE_ANGLE);
- }
- }
- protected virtual void Update()
- {
- if (bikeController.CurrentSpeed < minSpeedForRestriction)
- {
- currentValue = 0;
- return;
- }
-
- if (useSteer) steerAngleSuggestor.Value = bikeController.CurrentSteerAngle;
- if (useSpeed) speedSuggestor.UpdateValue(bikeController.CurrentSpeed);
- if (useSlope) slopeSuggestor.Value = currentSlopeDeg;
- var steerSuggestion = useSteer ? steerAngleSuggestor.Suggestion * steerMultiplier : 0f;
- var speedSuggestion = useSpeed ? speedSuggestor.Suggestion * speedMultiplier : 0f;
- var slopeSuggestion = useSlope ? slopeSuggestor.Suggestion * slopeMultiplier : 0f;
- var maxSuggestion = Mathf.Max(steerSuggestion, speedSuggestion, slopeSuggestion);
- if (Math.Abs(maxSuggestion - steerSuggestion) < 0.01f)
- {
- Debug.Log("Suggestion won by Steer");
- }else if (Math.Abs(maxSuggestion - speedSuggestion) < 0.01f)
- {
- Debug.Log("Suggestion won by Speed");
- }else if (Math.Abs(maxSuggestion - slopeSuggestion) < 0.01f)
- {
- Debug.Log("Suggestion won by Slope");
- }
- var desiredValue = Mathf.Min(maxSuggestion, maxValue);
- var desiredChange = desiredValue - currentValue;
- var maxChange = Time.deltaTime * maxValueChangePerSecond;
- var change = Mathf.Sign(desiredChange) * Mathf.Min(Mathf.Abs(desiredChange), maxChange);
- currentValue = Mathf.Clamp(currentValue + change, 0, 1);
- Debug.Log($"SuggestionValue = {currentValue}");
- }
- }
- }
|