Files
project-reset/Assets/Scripts/Player/SettingValue.cs

80 lines
2.9 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Serialization;
public interface IResettableSettingValue{
public abstract void SmoothAndEase();
}
[Serializable]
public struct SettingValue<T> : IResettableSettingValue{
public T targetValue;
public T currentValue;
public T refVel;
public float targetSmoothing; // Smoothing changes how fast the value is changed.
public float targetEasing; // Easing changes how fast smoothing is changed, when given a new value.
public T Value{
get => currentValue;
set => targetValue = value;
}
private float currentSmoothing;
public T defaultValue;
public float defaultSmoothing;
public float defaultEasing;
private float refVelFloat; // For use with SmoothDamp
private Vector3 refVelV3; // For use with SmoothDamp
private Vector2 refVelV2; // For use with SmoothDamp
public SettingValue(T initValue, float defaultEasing = 2f, float defaultSmoothing = 1f){
targetValue = initValue;
this.defaultSmoothing = defaultSmoothing;
this.defaultEasing = defaultEasing;
targetEasing = defaultEasing;
targetSmoothing = defaultSmoothing;
currentSmoothing = targetSmoothing;
currentValue = targetValue;
refVelFloat = 0;
refVelV3 = default;
refVelV2 = default;
refVel = default;
defaultValue = initValue;
}
public void SmoothAndEase(){
Debug.Log("Worked!");
return;
currentSmoothing = Mathf.MoveTowards(currentSmoothing, targetSmoothing, targetEasing * 1f * Time.deltaTime);
if (typeof(T) == typeof(float)) {
currentValue = (T)(object)Mathf.SmoothDamp((float)(object)currentValue, (float)(object)targetValue, ref refVelFloat, currentSmoothing * Time.deltaTime);
}
if (typeof(T) == typeof(Vector2)) {
currentValue = (T)(object)Vector2.SmoothDamp((Vector2)(object)currentValue, (Vector2)(object)targetValue, ref refVelV2, currentSmoothing * Time.deltaTime);
}
if (typeof(T) == typeof(Vector3)) {
currentValue = (T)(object)Vector3.SmoothDamp((Vector3)(object)currentValue, (Vector3)(object)targetValue, ref refVelV3, currentSmoothing * Time.deltaTime);
}
if (typeof(T) == typeof(Vector4) || typeof(T) == typeof(Quaternion)) {
// I have... zero clue if this will work. There is no Vector4 or Quaternion SmoothDamp
Vector3 v3value = Vector3.SmoothDamp((Vector4)(object)currentValue, (Vector4)(object)targetValue, ref refVelV3, currentSmoothing * Time.deltaTime);
float v4value = Mathf.SmoothDamp(((Vector4)(object)currentValue).z, ((Vector4)(object)targetValue).z, ref refVelFloat, currentSmoothing * Time.deltaTime);
currentValue = (T)(object)new Vector4(v3value.x, v3value.y, v3value.z, v4value);
}
}
}