From 041950b35f498c299ca60498eb02dfc1770f281f Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Sun, 8 Apr 2018 19:40:54 -0400 Subject: [PATCH] Add comparison and setting functionality for the Rigidbody2D variable --- Assets/Fungus/Scripts/Commands/SetVariable.cs | 18 ++++++++- .../Scripts/Commands/VariableCondition.cs | 18 ++++++++- .../Scripts/Editor/SetVariableEditor.cs | 3 +- .../Scripts/Editor/VariableConditionEditor.cs | 3 +- .../VariableTypes/Rigidbody2DVariable.cs | 38 ++++++++++++++++++- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/SetVariable.cs b/Assets/Fungus/Scripts/Commands/SetVariable.cs index e0eed681..87e7615a 100644 --- a/Assets/Fungus/Scripts/Commands/SetVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SetVariable.cs @@ -25,7 +25,8 @@ namespace Fungus typeof(ColorVariable), typeof(GameObjectVariable), typeof(MaterialVariable), - typeof(ObjectVariable))] + typeof(ObjectVariable), + typeof(Rigidbody2DVariable))] [SerializeField] protected Variable variable; [Tooltip("The type of math operation to be performed")] @@ -61,6 +62,9 @@ namespace Fungus [Tooltip("Object value to set with")] [SerializeField] protected ObjectData objectData; + [Tooltip("Rigidbody2D value to set with")] + [SerializeField] protected Rigidbody2DData rigidbody2DData; + protected virtual void DoSetOperation() { if (variable == null) @@ -119,6 +123,11 @@ namespace Fungus ObjectVariable objectVariable = (variable as ObjectVariable); objectVariable.Apply(setOperator, objectData.Value); } + else if (variable.GetType() == typeof(Rigidbody2DVariable)) + { + Rigidbody2DVariable rigidbody2DVariable = (variable as Rigidbody2DVariable); + rigidbody2DVariable.Apply(setOperator, rigidbody2DData.Value); + } } #region Public members @@ -133,7 +142,8 @@ namespace Fungus { typeof(ColorVariable), ColorVariable.setOperators }, { typeof(GameObjectVariable), GameObjectVariable.setOperators }, { typeof(MaterialVariable), MaterialVariable.setOperators }, - { typeof(ObjectVariable), ObjectVariable.setOperators } + { typeof(ObjectVariable), ObjectVariable.setOperators }, + { typeof(Rigidbody2DVariable), Rigidbody2DVariable.setOperators } }; /// @@ -220,6 +230,10 @@ namespace Fungus { description += objectData.GetDescription(); } + else if (variable.GetType() == typeof(Rigidbody2DVariable)) + { + description += rigidbody2DData.GetDescription(); + } return description; } diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index f0b1c3c3..a229d7c7 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -21,7 +21,8 @@ namespace Fungus typeof(ColorVariable), typeof(GameObjectVariable), typeof(MaterialVariable), - typeof(ObjectVariable))] + typeof(ObjectVariable), + typeof(Rigidbody2DVariable))] [SerializeField] protected Variable variable; [Tooltip("Boolean value to compare against")] @@ -54,6 +55,9 @@ namespace Fungus [Tooltip("Object value to compare against")] [SerializeField] protected ObjectData objectData; + [Tooltip("Rigidbody2D value to compare against")] + [SerializeField] protected Rigidbody2DData rigidbody2DData; + protected override bool EvaluateCondition() { if (variable == null) @@ -113,6 +117,11 @@ namespace Fungus ObjectVariable objectVariable = (variable as ObjectVariable); condition = objectVariable.Evaluate(compareOperator, objectData.Value); } + else if (variable.GetType() == typeof(Rigidbody2DVariable)) + { + Rigidbody2DVariable rigidbody2DVariable = (variable as Rigidbody2DVariable); + condition = rigidbody2DVariable.Evaluate(compareOperator, rigidbody2DData.Value); + } return condition; } @@ -134,7 +143,8 @@ namespace Fungus { typeof(ColorVariable), ColorVariable.compareOperators }, { typeof(GameObjectVariable), GameObjectVariable.compareOperators }, { typeof(MaterialVariable), MaterialVariable.compareOperators }, - { typeof(ObjectVariable), ObjectVariable.compareOperators } + { typeof(ObjectVariable), ObjectVariable.compareOperators }, + { typeof(Rigidbody2DVariable), Rigidbody2DVariable.compareOperators } }; /// @@ -192,6 +202,10 @@ namespace Fungus { summary += objectData.GetDescription(); } + else if (variable.GetType() == typeof(Rigidbody2DVariable)) + { + summary += rigidbody2DData.GetDescription(); + } return summary; } diff --git a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs index 003afba0..c90d9596 100644 --- a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs +++ b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs @@ -49,7 +49,8 @@ namespace Fungus.EditorUtils { typeof(ColorVariable), new VariablePropertyInfo("Color", serializedObject.FindProperty("colorData")) }, { typeof(GameObjectVariable), new VariablePropertyInfo("GameObject", serializedObject.FindProperty("gameObjectData")) }, { typeof(MaterialVariable), new VariablePropertyInfo("Material", serializedObject.FindProperty("materialData")) }, - { typeof(ObjectVariable), new VariablePropertyInfo("Object", serializedObject.FindProperty("objectData")) } + { typeof(ObjectVariable), new VariablePropertyInfo("Object", serializedObject.FindProperty("objectData")) }, + { typeof(Rigidbody2DVariable), new VariablePropertyInfo("Rigidbody2D", serializedObject.FindProperty("rigidbody2DData")) } }; } diff --git a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs index a7c992d4..56558ff6 100644 --- a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs @@ -34,7 +34,8 @@ namespace Fungus.EditorUtils { typeof(ColorVariable), serializedObject.FindProperty("colorData") }, { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") }, { typeof(MaterialVariable), serializedObject.FindProperty("materialData") }, - { typeof(ObjectVariable), serializedObject.FindProperty("objectData") } + { typeof(ObjectVariable), serializedObject.FindProperty("objectData") }, + { typeof(Rigidbody2DVariable), serializedObject.FindProperty("rigidbody2DData") } }; } diff --git a/Assets/Fungus/Scripts/VariableTypes/Rigidbody2DVariable.cs b/Assets/Fungus/Scripts/VariableTypes/Rigidbody2DVariable.cs index f48d6dcb..e011975a 100644 --- a/Assets/Fungus/Scripts/VariableTypes/Rigidbody2DVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/Rigidbody2DVariable.cs @@ -9,7 +9,43 @@ namespace Fungus [AddComponentMenu("")] [System.Serializable] public class Rigidbody2DVariable : VariableBase - { } + { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; + public static readonly SetOperator[] setOperators = { SetOperator.Assign }; + + public virtual bool Evaluate(CompareOperator compareOperator, Rigidbody2D 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, Rigidbody2D value) + { + switch (setOperator) + { + case SetOperator.Assign: + Value = value; + break; + default: + Debug.LogError("The " + setOperator.ToString() + " set operator is not valid."); + break; + } + } + } /// /// Container for a Rigidbody2D variable reference or constant value.