using System;
using UnityEngine;
namespace MalbersAnimations.Scriptables
{
/// Int Scriptable Variable. Based on the Talk - Game Architecture with Scriptable Objects by Ryan Hipple
[CreateAssetMenu(menuName = "Malbers Animations/Variables/Integer", order = 1000)]
public class IntVar : ScriptableVar
{
/// The current value
[SerializeField] private int value = 0;
/// Invoked when the value changes
public Action OnValueChanged = delegate { };
/// Value of the Int Scriptable variable
public virtual int Value
{
get => value;
set
{
// if (this.value != value) //If the value is different change it
{
this.value = value;
OnValueChanged(value); //If we are using OnChange event Invoked
#if UNITY_EDITOR
if (debug) Debug.Log($"{name} -> [ {value} ] ",this);
#endif
}
}
}
/// Set the Value using another IntVar
public virtual void SetValue(IntVar var) => Value = var.Value;
/// Add or Remove the passed var value
public virtual void Add(IntVar var) => Value += var.Value;
/// Add or Remove the passed var value
public virtual void Add(int var) => Value += var;
public virtual void Multiply(int var) => Value *= var;
public virtual void Multiply(IntVar var) => Value *= var;
public virtual void Divide(IntVar var) => Value /= var;
public static implicit operator int(IntVar reference) => reference.Value;
}
[System.Serializable]
public class IntReference : ReferenceVar
{
public int ConstantValue;
#pragma warning disable CA2235 // Mark all non-serializable fields
[RequiredField] public IntVar Variable;
#pragma warning restore CA2235 // Mark all non-serializable fields
public IntReference() => Value = 0;
public IntReference(int value) => Value = value;
public IntReference(IntVar value) => Value = value.Value;
public int Value
{
get => UseConstant || Variable == null ? ConstantValue : Variable.Value;
set
{
if (UseConstant || Variable == null)
ConstantValue = value;
else
Variable.Value = value;
}
}
#region Operators
public static implicit operator int(IntReference reference) => reference.Value;
public static implicit operator IntReference(int reference) => new IntReference(reference);
public static implicit operator IntReference(IntVar reference) => new IntReference(reference);
#endregion
}
#if UNITY_EDITOR
[UnityEditor.CanEditMultipleObjects, UnityEditor.CustomEditor(typeof(IntVar))]
public class IntVarEditor : VariableEditor
{
public override void OnInspectorGUI() => PaintInspectorGUI("Int Variable");
}
#endif
}