From 57776a67daa473e54f2077e86a09369b3dede390 Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Sun, 8 Apr 2018 20:21:44 -0400 Subject: [PATCH] Add comparison and setting functionality for the Transform variable --- Assets/Fungus/Scripts/Commands/SetVariable.cs | 18 ++++++++- .../Scripts/Commands/VariableCondition.cs | 18 ++++++++- .../Scripts/Editor/SetVariableEditor.cs | 3 +- .../Scripts/Editor/VariableConditionEditor.cs | 3 +- .../VariableTypes/TransformVariable.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 8376be82..d30df7db 100644 --- a/Assets/Fungus/Scripts/Commands/SetVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SetVariable.cs @@ -28,7 +28,8 @@ namespace Fungus typeof(ObjectVariable), typeof(Rigidbody2DVariable), typeof(SpriteVariable), - typeof(TextureVariable))] + typeof(TextureVariable), + typeof(TransformVariable))] [SerializeField] protected Variable variable; [Tooltip("The type of math operation to be performed")] @@ -73,6 +74,9 @@ namespace Fungus [Tooltip("Texture value to set with")] [SerializeField] protected TextureData textureData; + [Tooltip("Transform value to set with")] + [SerializeField] protected TransformData transformData; + protected virtual void DoSetOperation() { if (variable == null) @@ -146,6 +150,11 @@ namespace Fungus TextureVariable textureVariable = (variable as TextureVariable); textureVariable.Apply(setOperator, textureData.Value); } + else if (variable.GetType() == typeof(TransformVariable)) + { + TransformVariable transformVariable = (variable as TransformVariable); + transformVariable.Apply(setOperator, transformData.Value); + } } #region Public members @@ -163,7 +172,8 @@ namespace Fungus { typeof(ObjectVariable), ObjectVariable.setOperators }, { typeof(Rigidbody2DVariable), Rigidbody2DVariable.setOperators }, { typeof(SpriteVariable), SpriteVariable.setOperators }, - { typeof(TextureVariable), TextureVariable.setOperators } + { typeof(TextureVariable), TextureVariable.setOperators }, + { typeof(TransformVariable), TransformVariable.setOperators } }; /// @@ -262,6 +272,10 @@ namespace Fungus { description += textureData.GetDescription(); } + else if (variable.GetType() == typeof(TransformVariable)) + { + description += transformData.GetDescription(); + } return description; } diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index 35335034..829669fc 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -24,7 +24,8 @@ namespace Fungus typeof(ObjectVariable), typeof(Rigidbody2DVariable), typeof(SpriteVariable), - typeof(TextureVariable))] + typeof(TextureVariable), + typeof(TransformVariable))] [SerializeField] protected Variable variable; [Tooltip("Boolean value to compare against")] @@ -66,6 +67,9 @@ namespace Fungus [Tooltip("Texture value to compare against")] [SerializeField] protected TextureData textureData; + [Tooltip("Transform value to compare against")] + [SerializeField] protected TransformData transformData; + protected override bool EvaluateCondition() { if (variable == null) @@ -140,6 +144,11 @@ namespace Fungus TextureVariable textureVariable = (variable as TextureVariable); condition = textureVariable.Evaluate(compareOperator, textureData.Value); } + else if (variable.GetType() == typeof(TransformVariable)) + { + TransformVariable transformVariable = (variable as TransformVariable); + condition = transformVariable.Evaluate(compareOperator, transformData.Value); + } return condition; } @@ -164,7 +173,8 @@ namespace Fungus { typeof(ObjectVariable), ObjectVariable.compareOperators }, { typeof(Rigidbody2DVariable), Rigidbody2DVariable.compareOperators }, { typeof(SpriteVariable), SpriteVariable.compareOperators }, - { typeof(TextureVariable), TextureVariable.compareOperators } + { typeof(TextureVariable), TextureVariable.compareOperators }, + { typeof(TransformVariable), TransformVariable.compareOperators } }; /// @@ -234,6 +244,10 @@ namespace Fungus { summary += textureData.GetDescription(); } + else if (variable.GetType() == typeof(TransformVariable)) + { + summary += transformData.GetDescription(); + } return summary; } diff --git a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs index 994472aa..b908f590 100644 --- a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs +++ b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs @@ -52,7 +52,8 @@ namespace Fungus.EditorUtils { typeof(ObjectVariable), new VariablePropertyInfo("Object", serializedObject.FindProperty("objectData")) }, { typeof(Rigidbody2DVariable), new VariablePropertyInfo("Rigidbody2D", serializedObject.FindProperty("rigidbody2DData")) }, { typeof(SpriteVariable), new VariablePropertyInfo("Sprite", serializedObject.FindProperty("spriteData")) }, - { typeof(TextureVariable), new VariablePropertyInfo("Texture", serializedObject.FindProperty("textureData")) } + { typeof(TextureVariable), new VariablePropertyInfo("Texture", serializedObject.FindProperty("textureData")) }, + { typeof(TransformVariable), new VariablePropertyInfo("Transform", serializedObject.FindProperty("transformData")) } }; } diff --git a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs index e93595cb..eea653d7 100644 --- a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs @@ -37,7 +37,8 @@ namespace Fungus.EditorUtils { typeof(ObjectVariable), serializedObject.FindProperty("objectData") }, { typeof(Rigidbody2DVariable), serializedObject.FindProperty("rigidbody2DData") }, { typeof(SpriteVariable), serializedObject.FindProperty("spriteData") }, - { typeof(TextureVariable), serializedObject.FindProperty("textureData") } + { typeof(TextureVariable), serializedObject.FindProperty("textureData") }, + { typeof(TransformVariable), serializedObject.FindProperty("transformData") } }; } diff --git a/Assets/Fungus/Scripts/VariableTypes/TransformVariable.cs b/Assets/Fungus/Scripts/VariableTypes/TransformVariable.cs index bbc32db4..19a17232 100644 --- a/Assets/Fungus/Scripts/VariableTypes/TransformVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/TransformVariable.cs @@ -13,7 +13,43 @@ namespace Fungus [AddComponentMenu("")] [System.Serializable] public class TransformVariable : VariableBase - {} + { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; + public static readonly SetOperator[] setOperators = { SetOperator.Assign }; + + public virtual bool Evaluate(CompareOperator compareOperator, Transform 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, Transform value) + { + switch (setOperator) + { + case SetOperator.Assign: + Value = value; + break; + default: + Debug.LogError("The " + setOperator.ToString() + " set operator is not valid."); + break; + } + } + } /// /// Container for a Transform variable reference or constant value.