using System;
using UnityEngine;
namespace MalbersAnimations.Scriptables
{
/// Float Scriptable Variable. Based on the Talk - Game Architecture with Scriptable Objects by Ryan Hipple
[CreateAssetMenu(menuName = "Malbers Animations/Variables/Float",order = 1000)]
public class FloatVar : ScriptableVar
{
/// The current value
[SerializeField,HideInInspector] private float value = 0;
/// Invoked when the value changes
public Action OnValueChanged = delegate { };
/// Value of the Float Scriptable variable
public virtual float Value
{
get => value;
set
{
// if (this.value != value) //If the value is diferent change it
{
this.value = value;
OnValueChanged(value); //If we are using OnChange event Invoked
#if UNITY_EDITOR
if (debug) Debug.Log($"{name} -> [ {value:F3} ] ", this);
#endif
}
}
}
/// Set the Value using another FoatVar
public virtual void SetValue(FloatVar var) => Value = var.Value;
/// Add or Remove the passed var value
public virtual void Add(FloatVar var) => Value += var.Value;
/// Add or Remove the passed var value
public virtual void Add(float var) => Value += var;
public static implicit operator float(FloatVar reference) => reference.Value;
}
[System.Serializable]
public class FloatReference : ReferenceVar
{
public float ConstantValue;
[RequiredField] public FloatVar Variable;
public FloatReference() => Value = 0;
public FloatReference(float value) => Value = value;
public FloatReference(FloatVar value) => Value = value.Value;
public float Value
{
get => UseConstant || Variable == null ? ConstantValue : Variable.Value;
set
{
if (UseConstant || Variable == null)
ConstantValue = value;
else
Variable.Value = value;
}
}
public static implicit operator float(FloatReference reference) => reference.Value;
public static implicit operator FloatReference(float reference) => new FloatReference(reference);
public static implicit operator FloatReference(FloatVar reference) => new FloatReference(reference);
}
#if UNITY_EDITOR
[UnityEditor.CanEditMultipleObjects, UnityEditor.CustomEditor(typeof(FloatVar))]
public class FloatVarEditor : VariableEditor
{
public override void OnInspectorGUI() => PaintInspectorGUI("Float Variable");
}
#endif
}