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.
241 lines
4.8 KiB
241 lines
4.8 KiB
10 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
namespace Fungus.Script
|
||
|
{
|
||
|
public enum CompareOperator
|
||
|
{
|
||
|
Equals, // ==
|
||
|
NotEquals, // !=
|
||
|
LessThan, // <
|
||
|
GreaterThan, // >
|
||
|
LessThanOrEquals, // <=
|
||
|
GreaterThanOrEquals // >=
|
||
|
}
|
||
|
|
||
10 years ago
|
[HelpText("Execute another sequence IF a condition is true. Sequences can be specified for both true (THEN) and false (ELSE) conditions.")]
|
||
|
public class If : FungusCommand
|
||
10 years ago
|
{
|
||
10 years ago
|
public FungusVariable variable;
|
||
10 years ago
|
|
||
|
public CompareOperator compareOperator;
|
||
|
|
||
10 years ago
|
public BooleanData booleanValue;
|
||
10 years ago
|
|
||
10 years ago
|
public IntegerData integerValue;
|
||
10 years ago
|
|
||
10 years ago
|
public FloatData floatValue;
|
||
10 years ago
|
|
||
10 years ago
|
public StringData stringValue;
|
||
10 years ago
|
|
||
10 years ago
|
public Sequence thenSequence;
|
||
10 years ago
|
|
||
10 years ago
|
public Sequence elseSequence;
|
||
10 years ago
|
|
||
|
public override void OnEnter()
|
||
|
{
|
||
|
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 = booleanValue.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 = integerValue.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 = floatValue.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 = stringValue.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
|
if (thenSequence != null)
|
||
10 years ago
|
{
|
||
10 years ago
|
ExecuteSequence(thenSequence);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
if (elseSequence != null)
|
||
10 years ago
|
{
|
||
10 years ago
|
ExecuteSequence(elseSequence);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Continue();
|
||
|
}
|
||
|
|
||
|
public override void GetConnectedSequences(ref List<Sequence> connectedSequences)
|
||
|
{
|
||
10 years ago
|
if (thenSequence != null)
|
||
10 years ago
|
{
|
||
10 years ago
|
connectedSequences.Add(thenSequence);
|
||
10 years ago
|
}
|
||
10 years ago
|
if (elseSequence != null)
|
||
10 years ago
|
{
|
||
10 years ago
|
connectedSequences.Add(elseSequence);
|
||
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))
|
||
|
{
|
||
|
summary += booleanValue.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
|
{
|
||
|
summary += integerValue.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
|
summary += floatValue.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
|
summary += stringValue.GetDescription();
|
||
|
}
|
||
|
|
||
|
summary += " THEN ";
|
||
10 years ago
|
|
||
|
if (thenSequence == null)
|
||
|
{
|
||
10 years ago
|
summary += "<Continue>";
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
summary += thenSequence.name;
|
||
10 years ago
|
}
|
||
10 years ago
|
summary += " ELSE ";
|
||
10 years ago
|
if (elseSequence == null)
|
||
|
{
|
||
10 years ago
|
summary += "<Continue>";
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
|
{
|
||
10 years ago
|
summary += elseSequence.name;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
return summary;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
public override bool HasReference(FungusVariable variable)
|
||
|
{
|
||
|
return (variable == this.variable);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|