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.
181 lines
3.5 KiB
181 lines
3.5 KiB
10 years ago
|
using UnityEngine;
|
||
10 years ago
|
using System.Collections;
|
||
|
|
||
10 years ago
|
namespace Fungus.Script
|
||
10 years ago
|
{
|
||
10 years ago
|
[HelpText("Sets a variable to a new value using simple arithmetic operations. The value can be a constant or another variable.")]
|
||
10 years ago
|
public class Set : FungusCommand
|
||
10 years ago
|
{
|
||
10 years ago
|
public enum SetOperator
|
||
|
{
|
||
|
Assign, // =
|
||
|
Negate, // !
|
||
10 years ago
|
Add, // +=
|
||
|
Subtract, // -=
|
||
|
Multiply, // *=
|
||
|
Divide // /=
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public FungusVariable variable;
|
||
10 years ago
|
|
||
10 years ago
|
public SetOperator setOperator;
|
||
|
|
||
10 years ago
|
public BooleanData booleanData;
|
||
10 years ago
|
|
||
10 years ago
|
public IntegerData integerData;
|
||
10 years ago
|
|
||
10 years ago
|
public FloatData floatData;
|
||
10 years ago
|
|
||
10 years ago
|
public StringData stringData;
|
||
10 years ago
|
|
||
|
public override void OnEnter()
|
||
10 years ago
|
{
|
||
10 years ago
|
if (variable == null)
|
||
10 years ago
|
{
|
||
10 years ago
|
Continue();
|
||
|
return;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if (variable.GetType() == typeof(BooleanVariable))
|
||
10 years ago
|
{
|
||
10 years ago
|
BooleanVariable lhs = (variable as BooleanVariable);
|
||
10 years ago
|
bool rhs = booleanData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
10 years ago
|
lhs.Value = rhs;
|
||
10 years ago
|
break;
|
||
|
case SetOperator.Negate:
|
||
10 years ago
|
lhs.Value = !rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
10 years ago
|
{
|
||
10 years ago
|
IntegerVariable lhs = (variable as IntegerVariable);
|
||
10 years ago
|
int rhs = integerData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (setOperator)
|
||
10 years ago
|
{
|
||
10 years ago
|
default:
|
||
|
case SetOperator.Assign:
|
||
10 years ago
|
lhs.Value = rhs;
|
||
10 years ago
|
break;
|
||
|
case SetOperator.Add:
|
||
10 years ago
|
lhs.Value += rhs;
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SetOperator.Subtract:
|
||
10 years ago
|
lhs.Value -= rhs;
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SetOperator.Multiply:
|
||
10 years ago
|
lhs.Value *= rhs;
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SetOperator.Divide:
|
||
10 years ago
|
lhs.Value /= rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
10 years ago
|
FloatVariable lhs = (variable as FloatVariable);
|
||
10 years ago
|
float rhs = floatData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
10 years ago
|
lhs.Value = rhs;
|
||
10 years ago
|
break;
|
||
|
case SetOperator.Add:
|
||
10 years ago
|
lhs.Value += rhs;
|
||
10 years ago
|
break;
|
||
|
case SetOperator.Subtract:
|
||
10 years ago
|
lhs.Value -= rhs;
|
||
10 years ago
|
break;
|
||
|
case SetOperator.Multiply:
|
||
10 years ago
|
lhs.Value *= rhs;
|
||
10 years ago
|
break;
|
||
|
case SetOperator.Divide:
|
||
10 years ago
|
lhs.Value /= rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
10 years ago
|
StringVariable lhs = (variable as StringVariable);
|
||
10 years ago
|
string rhs = stringData.Value;
|
||
10 years ago
|
|
||
10 years ago
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
10 years ago
|
lhs.Value = rhs;
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Continue();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public override string GetSummary()
|
||
10 years ago
|
{
|
||
|
if (variable == null)
|
||
|
{
|
||
10 years ago
|
return "Error: Variable not selected";
|
||
10 years ago
|
}
|
||
|
|
||
|
string description = variable.key;
|
||
|
|
||
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
|
description += " = ";
|
||
|
break;
|
||
|
case SetOperator.Negate:
|
||
|
description += " != ";
|
||
|
break;
|
||
|
case SetOperator.Add:
|
||
|
description += " += ";
|
||
|
break;
|
||
|
case SetOperator.Subtract:
|
||
|
description += " -= ";
|
||
|
break;
|
||
|
case SetOperator.Multiply:
|
||
|
description += " *= ";
|
||
|
break;
|
||
|
case SetOperator.Divide:
|
||
|
description += " /= ";
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (variable.GetType() == typeof(BooleanVariable))
|
||
|
{
|
||
|
description += booleanData.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
|
{
|
||
|
description += integerData.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
|
description += floatData.GetDescription();
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
|
description += stringData.GetDescription();
|
||
|
}
|
||
|
|
||
|
return description;
|
||
|
}
|
||
10 years ago
|
|
||
|
public override bool HasReference(FungusVariable variable)
|
||
|
{
|
||
|
return (variable == this.variable);
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|