From 110c11f2802d40d9ef3deca9bc3fcb39cd7b0d8e Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Sun, 1 Apr 2018 17:08:59 -0400 Subject: [PATCH] Add comparison and setting functionality for the AudioSource variable --- Assets/Fungus/Scripts/Commands/SetVariable.cs | 14 +++++++ .../Scripts/Commands/VariableCondition.cs | 14 +++++++ .../Scripts/Editor/SetVariableEditor.cs | 1 + .../Scripts/Editor/VariableConditionEditor.cs | 1 + .../VariableTypes/AudioSourceVariable.cs | 38 ++++++++++++++++++- 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Assets/Fungus/Scripts/Commands/SetVariable.cs b/Assets/Fungus/Scripts/Commands/SetVariable.cs index ee8df49b..a63852a0 100644 --- a/Assets/Fungus/Scripts/Commands/SetVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SetVariable.cs @@ -21,6 +21,7 @@ namespace Fungus typeof(FloatVariable), typeof(StringVariable), typeof(AnimatorVariable), + typeof(AudioSourceVariable), typeof(GameObjectVariable))] [SerializeField] protected Variable variable; @@ -42,6 +43,9 @@ namespace Fungus [Tooltip("Animator value to set with")] [SerializeField] protected AnimatorData animatorData; + [Tooltip("AudioSource value to set with")] + [SerializeField] protected AudioSourceData audioSourceData; + [Tooltip("GameObject value to set with")] [SerializeField] protected GameObjectData gameObjectData; @@ -78,6 +82,11 @@ namespace Fungus AnimatorVariable animatorVariable = (variable as AnimatorVariable); animatorVariable.Apply(setOperator, animatorData.Value); } + else if (variable.GetType() == typeof(AudioSourceVariable)) + { + AudioSourceVariable audioSourceVariable = (variable as AudioSourceVariable); + audioSourceVariable.Apply(setOperator, audioSourceData.Value); + } else if (variable.GetType() == typeof(GameObjectVariable)) { GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); @@ -93,6 +102,7 @@ namespace Fungus { typeof(FloatVariable), FloatVariable.setOperators }, { typeof(StringVariable), StringVariable.setOperators }, { typeof(AnimatorVariable), AnimatorVariable.setOperators }, + { typeof(AudioSourceVariable), AudioSourceVariable.setOperators }, { typeof(GameObjectVariable), GameObjectVariable.setOperators } }; @@ -160,6 +170,10 @@ namespace Fungus { description += animatorData.GetDescription(); } + else if (variable.GetType() == typeof(AudioSourceVariable)) + { + description += audioSourceData.GetDescription(); + } else if (variable.GetType() == typeof(GameObjectVariable)) { description += gameObjectData.GetDescription(); diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index 58ce1e02..e7019e07 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -17,6 +17,7 @@ namespace Fungus typeof(FloatVariable), typeof(StringVariable), typeof(AnimatorVariable), + typeof(AudioSourceVariable), typeof(GameObjectVariable))] [SerializeField] protected Variable variable; @@ -35,6 +36,9 @@ namespace Fungus [Tooltip("Animator value to compare against")] [SerializeField] protected AnimatorData animatorData; + [Tooltip("AudioSource value to compare against")] + [SerializeField] protected AudioSourceData audioSourceData; + [Tooltip("GameObject value to compare against")] [SerializeField] protected GameObjectData gameObjectData; @@ -72,6 +76,11 @@ namespace Fungus AnimatorVariable animatorVariable = (variable as AnimatorVariable); condition = animatorVariable.Evaluate(compareOperator, animatorData.Value); } + else if (variable.GetType() == typeof(AudioSourceVariable)) + { + AudioSourceVariable audioSourceVariable = (variable as AudioSourceVariable); + condition = audioSourceVariable.Evaluate(compareOperator, audioSourceData.Value); + } else if (variable.GetType() == typeof(GameObjectVariable)) { GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); @@ -94,6 +103,7 @@ namespace Fungus { typeof(FloatVariable), FloatVariable.compareOperators }, { typeof(StringVariable), StringVariable.compareOperators }, { typeof(AnimatorVariable), AnimatorVariable.compareOperators }, + { typeof(AudioSourceVariable), AudioSourceVariable.compareOperators }, { typeof(GameObjectVariable), GameObjectVariable.compareOperators } }; @@ -132,6 +142,10 @@ namespace Fungus { summary += animatorData.GetDescription(); } + else if (variable.GetType() == typeof(AudioSourceVariable)) + { + summary += audioSourceData.GetDescription(); + } else if (variable.GetType() == typeof(GameObjectVariable)) { summary += gameObjectData.GetDescription(); diff --git a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs index a604b4a1..2526651b 100644 --- a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs +++ b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs @@ -45,6 +45,7 @@ namespace Fungus.EditorUtils { typeof(FloatVariable), new VariablePropertyInfo("Float", serializedObject.FindProperty("floatData")) }, { typeof(StringVariable), new VariablePropertyInfo("String", serializedObject.FindProperty("stringData")) }, { typeof(AnimatorVariable), new VariablePropertyInfo("Animator", serializedObject.FindProperty("animatorData")) }, + { typeof(AudioSourceVariable), new VariablePropertyInfo("AudioSource", serializedObject.FindProperty("audioSourceData")) }, { typeof(GameObjectVariable), new VariablePropertyInfo("GameObject", serializedObject.FindProperty("gameObjectData")) } }; } diff --git a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs index b2c02858..7017d53c 100644 --- a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs @@ -30,6 +30,7 @@ namespace Fungus.EditorUtils { typeof(FloatVariable), serializedObject.FindProperty("floatData") }, { typeof(StringVariable), serializedObject.FindProperty("stringData") }, { typeof(AnimatorVariable), serializedObject.FindProperty("animatorData") }, + { typeof(AudioSourceVariable), serializedObject.FindProperty("audioSourceData") }, { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") } }; } diff --git a/Assets/Fungus/Scripts/VariableTypes/AudioSourceVariable.cs b/Assets/Fungus/Scripts/VariableTypes/AudioSourceVariable.cs index 5727e4a4..3cf056c9 100644 --- a/Assets/Fungus/Scripts/VariableTypes/AudioSourceVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/AudioSourceVariable.cs @@ -12,7 +12,43 @@ namespace Fungus [AddComponentMenu("")] [System.Serializable] public class AudioSourceVariable : VariableBase - {} + { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; + public static readonly SetOperator[] setOperators = { SetOperator.Assign }; + + public virtual bool Evaluate(CompareOperator compareOperator, AudioSource value) + { + bool condition = false; + + switch (compareOperator) + { + case CompareOperator.Equals: + condition = Value == value; + break; + case CompareOperator.NotEquals: + condition = Value != value; + break; + default: + Debug.LogError("The " + compareOperator.ToString() + " comparison operator is not valid."); + break; + } + + return condition; + } + + public override void Apply(SetOperator setOperator, AudioSource value) + { + switch (setOperator) + { + case SetOperator.Assign: + Value = value; + break; + default: + Debug.LogError("The " + setOperator.ToString() + " set operator is not valid."); + break; + } + } + } /// /// Container for an AudioSource variable reference or constant value.