using System; using UnityEngine; namespace MalbersAnimations.Scriptables { /// Bool Scriptable Variable. Based on the Talk - Game Architecture with Scriptable Objects by Ryan Hipple [CreateAssetMenu(menuName = "Malbers Animations/Variables/Bool", order = 1000)] public class BoolVar : ScriptableVar { [SerializeField] private bool value; /// Invoked when the value changes public Action OnValueChanged = delegate { }; /// Value of the Bool variable public virtual bool 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} ] ", this); #endif } } } public virtual void SetValue(BoolVar var) => SetValue(var.Value); public virtual void SetValue(bool var) => Value = var; public virtual void SetValueInverted(bool var) => Value = !var; public virtual void Toggle() => Value ^= true; public virtual void UpdateValue() => OnValueChanged?.Invoke(value); public static implicit operator bool(BoolVar reference) => reference.Value; } [System.Serializable] public class BoolReference : ReferenceVar { public bool ConstantValue; [RequiredField] public BoolVar Variable; public BoolReference() => Value = false; public BoolReference(bool value) => Value = value; public BoolReference(BoolVar value) => Value = value.Value; public bool Value { get => UseConstant || Variable == null ? ConstantValue : Variable.Value; set { // Debug.Log(value); if (UseConstant || Variable == null) ConstantValue = value; else Variable.Value = value; } } #region Operators public static implicit operator bool(BoolReference reference) => reference.Value; #endregion } #if UNITY_EDITOR [UnityEditor.CanEditMultipleObjects, UnityEditor.CustomEditor(typeof(BoolVar))] public class BoolVarEditor : VariableEditor { public override void OnInspectorGUI() => PaintInspectorGUI("Bool Variable"); } #endif }