Browse Source

Changes to make it easier to add new conditional commands

master
chrisgregan 10 years ago
parent
commit
d72f92b959
  1. 52
      Assets/Fungus/FungusScript/Scripts/Commands/Condition.cs
  2. 8
      Assets/Fungus/FungusScript/Scripts/Commands/Condition.cs.meta
  3. 208
      Assets/Fungus/FungusScript/Scripts/Commands/If.cs
  4. 24
      Assets/Fungus/FungusScript/Scripts/VariableTypes/BooleanVariable.cs
  5. 34
      Assets/Fungus/FungusScript/Scripts/VariableTypes/FloatVariable.cs
  6. 34
      Assets/Fungus/FungusScript/Scripts/VariableTypes/IntegerVariable.cs
  7. 23
      Assets/Fungus/FungusScript/Scripts/VariableTypes/StringVariable.cs

52
Assets/Fungus/FungusScript/Scripts/Commands/Condition.cs

@ -0,0 +1,52 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
public enum CompareOperator
{
Equals, // ==
NotEquals, // !=
LessThan, // <
GreaterThan, // >
LessThanOrEquals, // <=
GreaterThanOrEquals // >=
}
[AddComponentMenu("")]
public abstract class Condition : Command
{
[Tooltip("The type of comparison to be performed")]
public CompareOperator compareOperator;
public static string GetOperatorDescription(CompareOperator compareOperator)
{
string summary = "";
switch (compareOperator)
{
case CompareOperator.Equals:
summary += "==";
break;
case CompareOperator.NotEquals:
summary += "!=";
break;
case CompareOperator.LessThan:
summary += "<";
break;
case CompareOperator.GreaterThan:
summary += ">";
break;
case CompareOperator.LessThanOrEquals:
summary += "<=";
break;
case CompareOperator.GreaterThanOrEquals:
summary += ">=";
break;
}
return summary;
}
}
}

8
Assets/Fungus/FungusScript/Scripts/Commands/Condition.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a5a19bbd99c314af6aa3f3319ca323b2
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

208
Assets/Fungus/FungusScript/Scripts/Commands/If.cs

@ -4,21 +4,12 @@ using System.Collections.Generic;
namespace Fungus namespace Fungus
{ {
public enum CompareOperator
{
Equals, // ==
NotEquals, // !=
LessThan, // <
GreaterThan, // >
LessThanOrEquals, // <=
GreaterThanOrEquals // >=
}
[CommandInfo("Scripting", [CommandInfo("Scripting",
"If", "If",
"If the test expression is true, execute the following block of commands.")] "If the test expression is true, execute the following block of commands.")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class If : Command public class If : Condition
{ {
[Tooltip("Variable to use in expression")] [Tooltip("Variable to use in expression")]
@ -28,9 +19,6 @@ namespace Fungus
typeof(StringVariable))] typeof(StringVariable))]
public Variable variable; public Variable variable;
[Tooltip("The type of comparison to be performed")]
public CompareOperator compareOperator;
[Tooltip("Boolean value to compare against")] [Tooltip("Boolean value to compare against")]
public BooleanData booleanData; public BooleanData booleanData;
@ -50,143 +38,93 @@ namespace Fungus
return; return;
} }
bool condition = false;
if (variable == null) if (variable == null)
{ {
Continue(); Continue();
return; return;
} }
if (variable.GetType() == typeof(BooleanVariable)) EvaluateCondition();
{ }
bool lhs = (variable as BooleanVariable).value;
bool rhs = booleanData.Value;
switch (compareOperator) protected void EvaluateCondition()
{ {
case CompareOperator.Equals: BooleanVariable booleanVariable = variable as BooleanVariable;
condition = lhs == rhs; IntegerVariable integerVariable = variable as IntegerVariable;
break; FloatVariable floatVariable = variable as FloatVariable;
case CompareOperator.NotEquals: StringVariable stringVariable = variable as StringVariable;
default:
condition = lhs != rhs; bool condition = false;
break;
} if (booleanVariable != null)
{
condition = booleanVariable.Evaluate(compareOperator, booleanData.Value);
} }
else if (variable.GetType() == typeof(IntegerVariable)) else if (integerVariable != null)
{ {
int lhs = (variable as IntegerVariable).value; condition = integerVariable.Evaluate(compareOperator, integerData.Value);
int rhs = integerData.Value;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
condition = lhs != rhs;
break;
case CompareOperator.LessThan:
condition = lhs < rhs;
break;
case CompareOperator.GreaterThan:
condition = lhs > rhs;
break;
case CompareOperator.LessThanOrEquals:
condition = lhs <= rhs;
break;
case CompareOperator.GreaterThanOrEquals:
condition = lhs >= rhs;
break;
}
} }
else if (variable.GetType() == typeof(FloatVariable)) else if (floatVariable != null)
{ {
float lhs = (variable as FloatVariable).value; condition = floatVariable.Evaluate(compareOperator, floatData.Value);
float rhs = floatData.Value;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
condition = lhs != rhs;
break;
case CompareOperator.LessThan:
condition = lhs < rhs;
break;
case CompareOperator.GreaterThan:
condition = lhs > rhs;
break;
case CompareOperator.LessThanOrEquals:
condition = lhs <= rhs;
break;
case CompareOperator.GreaterThanOrEquals:
condition = lhs >= rhs;
break;
}
} }
else if (variable.GetType() == typeof(StringVariable)) else if (stringVariable != null)
{ {
string lhs = (variable as StringVariable).value; condition = stringVariable.Evaluate(compareOperator, stringData.Value);
string rhs = stringData.Value;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
default:
condition = lhs != rhs;
break;
}
} }
if (condition) if (condition)
{ {
Continue(); OnTrue();
} }
else else
{ {
// Find the next Else or EndIf command at the same indent level as this If command OnFalse();
for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i) }
{ }
Command nextCommand = parentSequence.commandList[i];
public void OnTrue()
{
Continue();
}
// Find next command at same indent level as this If command public void OnFalse()
// Skip disabled commands & comments {
if (!nextCommand.enabled || // Find the next Else or EndIf command at the same indent level as this If command
nextCommand.GetType() == typeof(Comment) || for (int i = commandIndex; i < parentSequence.commandList.Count; ++i)
nextCommand.indentLevel != indentLevel) {
Command nextCommand = parentSequence.commandList[i];
// Find next command at same indent level as this If command
// Skip disabled commands & comments
if (!nextCommand.enabled ||
nextCommand.GetType() == typeof(Comment) ||
nextCommand.indentLevel != indentLevel)
{
continue;
}
System.Type type = nextCommand.GetType();
if (type == typeof(Else) ||
type == typeof(EndIf) || // Legacy support for old EndIf command
type == typeof(End))
{
if (i >= parentSequence.commandList.Count - 1)
{ {
continue; // Last command in Sequence, so stop
Stop();
} }
else
System.Type type = nextCommand.GetType();
if (type == typeof(Else) ||
type == typeof(EndIf) || // Legacy support for old EndIf command
type == typeof(End))
{ {
if (i >= parentSequence.commandList.Count - 1) // Execute command immediately after the Else or End command
{ Continue(nextCommand);
// Last command in Sequence, so stop return;
Stop();
}
else
{
// Execute command immediately after the Else or End command
Continue(nextCommand);
return;
}
} }
} }
// No matching End command found, so just stop the sequence
Stop();
} }
// No matching End command found, so just stop the sequence
Stop();
} }
public override string GetSummary() public override string GetSummary()
@ -196,28 +134,8 @@ namespace Fungus
return "Error: No variable selected"; return "Error: No variable selected";
} }
string summary = variable.key; string summary = variable.key + " ";
switch (compareOperator) summary += Condition.GetOperatorDescription(compareOperator) + " ";
{
case CompareOperator.Equals:
summary += " == ";
break;
case CompareOperator.NotEquals:
summary += " != ";
break;
case CompareOperator.LessThan:
summary += " < ";
break;
case CompareOperator.GreaterThan:
summary += " > ";
break;
case CompareOperator.LessThanOrEquals:
summary += " <= ";
break;
case CompareOperator.GreaterThanOrEquals:
summary += " >= ";
break;
}
if (variable.GetType() == typeof(BooleanVariable)) if (variable.GetType() == typeof(BooleanVariable))
{ {

24
Assets/Fungus/FungusScript/Scripts/VariableTypes/BooleanVariable.cs

@ -8,7 +8,29 @@ namespace Fungus
[VariableInfo("", "Boolean")] [VariableInfo("", "Boolean")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class BooleanVariable : VariableBase<bool> public class BooleanVariable : VariableBase<bool>
{} {
public virtual bool Evaluate(CompareOperator compareOperator, bool booleanValue)
{
bool condition = false;
bool lhs = value;
bool rhs = booleanValue;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
default:
condition = lhs != rhs;
break;
}
return condition;
}
}
[System.Serializable] [System.Serializable]
public struct BooleanData public struct BooleanData

34
Assets/Fungus/FungusScript/Scripts/VariableTypes/FloatVariable.cs

@ -6,7 +6,39 @@ namespace Fungus
[VariableInfo("", "Float")] [VariableInfo("", "Float")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class FloatVariable : VariableBase<float> public class FloatVariable : VariableBase<float>
{} {
public virtual bool Evaluate(CompareOperator compareOperator, float floatValue)
{
float lhs = value;
float rhs = floatValue;
bool condition = false;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
condition = lhs != rhs;
break;
case CompareOperator.LessThan:
condition = lhs < rhs;
break;
case CompareOperator.GreaterThan:
condition = lhs > rhs;
break;
case CompareOperator.LessThanOrEquals:
condition = lhs <= rhs;
break;
case CompareOperator.GreaterThanOrEquals:
condition = lhs >= rhs;
break;
}
return condition;
}
}
[System.Serializable] [System.Serializable]
public struct FloatData public struct FloatData

34
Assets/Fungus/FungusScript/Scripts/VariableTypes/IntegerVariable.cs

@ -6,7 +6,39 @@ namespace Fungus
[VariableInfo("", "Integer")] [VariableInfo("", "Integer")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class IntegerVariable : VariableBase<int> public class IntegerVariable : VariableBase<int>
{} {
public virtual bool Evaluate(CompareOperator compareOperator, int integerValue)
{
int lhs = value;
int rhs = integerValue;
bool condition = false;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
condition = lhs != rhs;
break;
case CompareOperator.LessThan:
condition = lhs < rhs;
break;
case CompareOperator.GreaterThan:
condition = lhs > rhs;
break;
case CompareOperator.LessThanOrEquals:
condition = lhs <= rhs;
break;
case CompareOperator.GreaterThanOrEquals:
condition = lhs >= rhs;
break;
}
return condition;
}
}
[System.Serializable] [System.Serializable]
public struct IntegerData public struct IntegerData

23
Assets/Fungus/FungusScript/Scripts/VariableTypes/StringVariable.cs

@ -7,7 +7,28 @@ namespace Fungus
[VariableInfo("", "String")] [VariableInfo("", "String")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class StringVariable : VariableBase<string> public class StringVariable : VariableBase<string>
{} {
public virtual bool Evaluate(CompareOperator compareOperator, string stringValue)
{
string lhs = value;
string rhs = stringValue;
bool condition = false;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = lhs == rhs;
break;
case CompareOperator.NotEquals:
default:
condition = lhs != rhs;
break;
}
return condition;
}
}
[System.Serializable] [System.Serializable]
public struct StringData public struct StringData

Loading…
Cancel
Save