You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
5.2 KiB
242 lines
5.2 KiB
10 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
10 years ago
|
namespace Fungus
|
||
10 years ago
|
{
|
||
|
public enum CompareOperator
|
||
|
{
|
||
|
Equals, // ==
|
||
|
NotEquals, // !=
|
||
|
LessThan, // <
|
||
|
GreaterThan, // >
|
||
|
LessThanOrEquals, // <=
|
||
|
GreaterThanOrEquals // >=
|
||
|
}
|
||
|
|
||
10 years ago
|
[CommandInfo("Scripting",
|
||
|
"If",
|
||
10 years ago
|
"If the test expression is true, execute the following block of commands.")]
|
||
10 years ago
|
public class If : Command
|
||
10 years ago
|
{
|
||
10 years ago
|
[Tooltip("The variable whos value will be checked")]
|
||
10 years ago
|
public Variable variable;
|
||
10 years ago
|
|
||
10 years ago
|
[Tooltip("The type of comparison to be performed")]
|
||
10 years ago
|
public CompareOperator compareOperator;
|
||
|
|
||
10 years ago
|
[Tooltip("Boolean value to compare against")]
|
||
10 years ago
|
public BooleanData booleanData;
|
||
10 years ago
|
|
||
10 years ago
|
[Tooltip("Integer value to compare against")]
|
||
10 years ago
|
public IntegerData integerData;
|
||
10 years ago
|
|
||
10 years ago
|
[Tooltip("Float value to compare against")]
|
||
10 years ago
|
public FloatData floatData;
|
||
10 years ago
|
|
||
10 years ago
|
[Tooltip("String value to compare against")]
|
||
10 years ago
|
public StringData stringData;
|
||
10 years ago
|
|
||
|
public override void OnEnter()
|
||
|
{
|
||
10 years ago
|
if (parentSequence == null)
|
||
10 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
bool condition = false;
|
||
|
|
||
10 years ago
|
if (variable == null)
|
||
|
{
|
||
|
Continue();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (variable.GetType() == typeof(BooleanVariable))
|
||
10 years ago
|
{
|
||
10 years ago
|
bool lhs = (variable as BooleanVariable).Value;
|
||
10 years ago
|
bool rhs = booleanData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (compareOperator)
|
||
|
{
|
||
|
case CompareOperator.Equals:
|
||
10 years ago
|
condition = lhs == rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.NotEquals:
|
||
|
default:
|
||
10 years ago
|
condition = lhs != rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
|
{
|
||
10 years ago
|
int lhs = (variable as IntegerVariable).Value;
|
||
10 years ago
|
int rhs = integerData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (compareOperator)
|
||
|
{
|
||
|
case CompareOperator.Equals:
|
||
10 years ago
|
condition = lhs == rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.NotEquals:
|
||
10 years ago
|
condition = lhs != rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.LessThan:
|
||
10 years ago
|
condition = lhs < rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.GreaterThan:
|
||
10 years ago
|
condition = lhs > rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.LessThanOrEquals:
|
||
10 years ago
|
condition = lhs <= rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.GreaterThanOrEquals:
|
||
10 years ago
|
condition = lhs >= rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
10 years ago
|
float lhs = (variable as FloatVariable).Value;
|
||
10 years ago
|
float rhs = floatData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (compareOperator)
|
||
|
{
|
||
|
case CompareOperator.Equals:
|
||
10 years ago
|
condition = lhs == rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.NotEquals:
|
||
10 years ago
|
condition = lhs != rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.LessThan:
|
||
10 years ago
|
condition = lhs < rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.GreaterThan:
|
||
10 years ago
|
condition = lhs > rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.LessThanOrEquals:
|
||
10 years ago
|
condition = lhs <= rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.GreaterThanOrEquals:
|
||
10 years ago
|
condition = lhs >= rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
10 years ago
|
string lhs = (variable as StringVariable).Value;
|
||
10 years ago
|
string rhs = stringData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (compareOperator)
|
||
|
{
|
||
|
case CompareOperator.Equals:
|
||
10 years ago
|
condition = lhs == rhs;
|
||
10 years ago
|
break;
|
||
|
case CompareOperator.NotEquals:
|
||
|
default:
|
||
10 years ago
|
condition = lhs != rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (condition)
|
||
|
{
|
||
10 years ago
|
Continue();
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
// Find the next Else or EndIf command at the same indent level as this If command
|
||
|
bool foundThisCommand = false;
|
||
|
int indent = indentLevel;
|
||
10 years ago
|
foreach(Command command in parentSequence.commandList)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (foundThisCommand &&
|
||
|
command.indentLevel == indent)
|
||
|
{
|
||
|
System.Type type = command.GetType();
|
||
|
if (type == typeof(Else) ||
|
||
|
type == typeof(EndIf))
|
||
|
{
|
||
|
// Execute command immediately after the Else or EndIf command
|
||
|
Continue(command);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
else if (command == this)
|
||
|
{
|
||
|
foundThisCommand = true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// No matching EndIf command found, so just stop the sequence
|
||
|
Stop();
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public override string GetSummary()
|
||
10 years ago
|
{
|
||
10 years ago
|
if (variable == null)
|
||
10 years ago
|
{
|
||
10 years ago
|
return "Error: No variable selected";
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
string summary = variable.key;
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
if (variable.GetType() == typeof(BooleanVariable))
|
||
|
{
|
||
10 years ago
|
summary += booleanData.GetDescription();
|
||
10 years ago
|
}
|
||
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
|
{
|
||
10 years ago
|
summary += integerData.GetDescription();
|
||
10 years ago
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
10 years ago
|
summary += floatData.GetDescription();
|
||
10 years ago
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
10 years ago
|
summary += stringData.GetDescription();
|
||
10 years ago
|
}
|
||
|
|
||
|
return summary;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public override bool HasReference(Variable variable)
|
||
10 years ago
|
{
|
||
|
return (variable == this.variable);
|
||
|
}
|
||
10 years ago
|
|
||
|
public override int GetPostIndent()
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
10 years ago
|
|
||
|
public override Color GetButtonColor()
|
||
|
{
|
||
|
return new Color32(253, 253, 150, 255);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|