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.
201 lines
6.2 KiB
201 lines
6.2 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
10 years ago
|
using UnityEngine;
|
||
|
|
||
10 years ago
|
namespace Fungus
|
||
10 years ago
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// If the test expression is true, execute the following command block.
|
||
|
/// </summary>
|
||
8 years ago
|
[CommandInfo("Flow",
|
||
|
"If",
|
||
|
"If the test expression is true, execute the following command block.")]
|
||
|
[AddComponentMenu("")]
|
||
|
public class If : Condition
|
||
|
{
|
||
|
[Tooltip("Variable to use in expression")]
|
||
|
[VariableProperty(typeof(BooleanVariable),
|
||
|
typeof(IntegerVariable),
|
||
|
typeof(FloatVariable),
|
||
|
typeof(StringVariable))]
|
||
8 years ago
|
[SerializeField] protected Variable variable;
|
||
8 years ago
|
|
||
|
[Tooltip("Boolean value to compare against")]
|
||
8 years ago
|
[SerializeField] protected BooleanData booleanData;
|
||
8 years ago
|
|
||
|
[Tooltip("Integer value to compare against")]
|
||
8 years ago
|
[SerializeField] protected IntegerData integerData;
|
||
8 years ago
|
|
||
|
[Tooltip("Float value to compare against")]
|
||
8 years ago
|
[SerializeField] protected FloatData floatData;
|
||
8 years ago
|
|
||
|
[Tooltip("String value to compare against")]
|
||
8 years ago
|
[SerializeField] protected StringDataMulti stringData;
|
||
8 years ago
|
|
||
|
public override void OnEnter()
|
||
|
{
|
||
8 years ago
|
if (ParentBlock == null)
|
||
8 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (variable == null)
|
||
|
{
|
||
|
Continue();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
EvaluateAndContinue();
|
||
|
}
|
||
|
|
||
|
public bool EvaluateCondition()
|
||
|
{
|
||
|
BooleanVariable booleanVariable = variable as BooleanVariable;
|
||
|
IntegerVariable integerVariable = variable as IntegerVariable;
|
||
|
FloatVariable floatVariable = variable as FloatVariable;
|
||
|
StringVariable stringVariable = variable as StringVariable;
|
||
|
|
||
|
bool condition = false;
|
||
|
|
||
|
if (booleanVariable != null)
|
||
|
{
|
||
|
condition = booleanVariable.Evaluate(compareOperator, booleanData.Value);
|
||
|
}
|
||
|
else if (integerVariable != null)
|
||
|
{
|
||
|
condition = integerVariable.Evaluate(compareOperator, integerData.Value);
|
||
|
}
|
||
|
else if (floatVariable != null)
|
||
|
{
|
||
|
condition = floatVariable.Evaluate(compareOperator, floatData.Value);
|
||
|
}
|
||
|
else if (stringVariable != null)
|
||
|
{
|
||
|
condition = stringVariable.Evaluate(compareOperator, stringData.Value);
|
||
|
}
|
||
|
|
||
|
return condition;
|
||
|
}
|
||
|
|
||
|
protected void EvaluateAndContinue()
|
||
|
{
|
||
|
if (EvaluateCondition())
|
||
|
{
|
||
|
OnTrue();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
OnFalse();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void OnTrue()
|
||
|
{
|
||
|
Continue();
|
||
|
}
|
||
|
|
||
|
protected virtual void OnFalse()
|
||
|
{
|
||
|
// Last command in block
|
||
8 years ago
|
if (CommandIndex >= ParentBlock.CommandList.Count)
|
||
8 years ago
|
{
|
||
|
StopParentBlock();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Find the next Else, ElseIf or End command at the same indent level as this If command
|
||
8 years ago
|
for (int i = CommandIndex + 1; i < ParentBlock.CommandList.Count; ++i)
|
||
8 years ago
|
{
|
||
8 years ago
|
Command nextCommand = ParentBlock.CommandList[i];
|
||
8 years ago
|
|
||
|
if (nextCommand == null)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// Find next command at same indent level as this If command
|
||
|
// Skip disabled commands, comments & labels
|
||
|
if (!nextCommand.enabled ||
|
||
|
nextCommand.GetType() == typeof(Comment) ||
|
||
|
nextCommand.GetType() == typeof(Label) ||
|
||
8 years ago
|
nextCommand.IndentLevel != indentLevel)
|
||
8 years ago
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
System.Type type = nextCommand.GetType();
|
||
|
if (type == typeof(Else) ||
|
||
|
type == typeof(End))
|
||
|
{
|
||
8 years ago
|
if (i >= ParentBlock.CommandList.Count - 1)
|
||
8 years ago
|
{
|
||
|
// Last command in Block, so stop
|
||
|
StopParentBlock();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Execute command immediately after the Else or End command
|
||
8 years ago
|
Continue(nextCommand.CommandIndex + 1);
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
else if (type == typeof(ElseIf))
|
||
|
{
|
||
|
// Execute the Else If command
|
||
|
Continue(i);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// No matching End command found, so just stop the block
|
||
|
StopParentBlock();
|
||
|
}
|
||
|
|
||
|
public override string GetSummary()
|
||
|
{
|
||
|
if (variable == null)
|
||
|
{
|
||
|
return "Error: No variable selected";
|
||
|
}
|
||
|
|
||
8 years ago
|
string summary = variable.Key + " ";
|
||
8 years ago
|
summary += Condition.GetOperatorDescription(compareOperator) + " ";
|
||
|
|
||
|
if (variable.GetType() == typeof(BooleanVariable))
|
||
|
{
|
||
|
summary += booleanData.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
|
{
|
||
|
summary += integerData.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
|
summary += floatData.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
|
summary += stringData.GetDescription();
|
||
|
}
|
||
|
|
||
|
return summary;
|
||
|
}
|
||
|
|
||
|
public override bool HasReference(Variable variable)
|
||
|
{
|
||
|
return (variable == this.variable);
|
||
|
}
|
||
|
|
||
|
public override bool OpenBlock()
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public override Color GetButtonColor()
|
||
|
{
|
||
|
return new Color32(253, 253, 150, 255);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|