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.
208 lines
6.6 KiB
208 lines
6.6 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
|
|
||
10 years ago
|
namespace Fungus
|
||
10 years ago
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Sets a Boolean, Integer, Float or String variable to a new value using a simple arithmetic operation. The value can be a constant or reference another variable of the same type.
|
||
|
/// </summary>
|
||
8 years ago
|
[CommandInfo("Variable",
|
||
|
"Set Variable",
|
||
|
"Sets a Boolean, Integer, Float or String variable to a new value using a simple arithmetic operation. The value can be a constant or reference another variable of the same type.")]
|
||
|
[AddComponentMenu("")]
|
||
|
public class SetVariable : Command
|
||
|
{
|
||
|
public enum SetOperator
|
||
|
{
|
||
|
Assign, // =
|
||
|
Negate, // =!
|
||
|
Add, // +=
|
||
|
Subtract, // -=
|
||
|
Multiply, // *=
|
||
|
Divide // /=
|
||
|
}
|
||
|
|
||
|
[Tooltip("The variable whos value will be set")]
|
||
|
[VariableProperty(typeof(BooleanVariable),
|
||
|
typeof(IntegerVariable),
|
||
|
typeof(FloatVariable),
|
||
|
typeof(StringVariable))]
|
||
8 years ago
|
[SerializeField] protected Variable variable;
|
||
8 years ago
|
|
||
|
[Tooltip("The type of math operation to be performed")]
|
||
8 years ago
|
[SerializeField] protected SetOperator setOperator;
|
||
8 years ago
|
public virtual SetOperator _SetOperator { get { return setOperator; } }
|
||
8 years ago
|
|
||
|
[Tooltip("Boolean value to set with")]
|
||
8 years ago
|
[SerializeField] protected BooleanData booleanData;
|
||
8 years ago
|
|
||
|
[Tooltip("Integer value to set with")]
|
||
8 years ago
|
[SerializeField] protected IntegerData integerData;
|
||
8 years ago
|
|
||
|
[Tooltip("Float value to set with")]
|
||
8 years ago
|
[SerializeField] protected FloatData floatData;
|
||
8 years ago
|
|
||
|
[Tooltip("String value to set with")]
|
||
8 years ago
|
[SerializeField] protected StringDataMulti stringData;
|
||
8 years ago
|
|
||
|
public override void OnEnter()
|
||
|
{
|
||
|
DoSetOperation();
|
||
|
|
||
|
Continue();
|
||
|
}
|
||
|
|
||
|
public override string GetSummary()
|
||
|
{
|
||
|
if (variable == null)
|
||
|
{
|
||
|
return "Error: Variable not selected";
|
||
|
}
|
||
|
|
||
8 years ago
|
string description = variable.Key;
|
||
8 years ago
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
public override bool HasReference(Variable variable)
|
||
|
{
|
||
|
return (variable == this.variable);
|
||
|
}
|
||
|
|
||
|
public override Color GetButtonColor()
|
||
|
{
|
||
|
return new Color32(253, 253, 150, 255);
|
||
|
}
|
||
|
|
||
|
protected virtual void DoSetOperation()
|
||
|
{
|
||
|
if (variable == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (variable.GetType() == typeof(BooleanVariable))
|
||
|
{
|
||
|
BooleanVariable lhs = (variable as BooleanVariable);
|
||
|
bool rhs = booleanData.Value;
|
||
|
|
||
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
8 years ago
|
lhs.Value = rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Negate:
|
||
8 years ago
|
lhs.Value = !rhs;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(IntegerVariable))
|
||
|
{
|
||
|
IntegerVariable lhs = (variable as IntegerVariable);
|
||
|
int rhs = integerData.Value;
|
||
|
|
||
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
8 years ago
|
lhs.Value = rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Add:
|
||
8 years ago
|
lhs.Value += rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Subtract:
|
||
8 years ago
|
lhs.Value -= rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Multiply:
|
||
8 years ago
|
lhs.Value *= rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Divide:
|
||
8 years ago
|
lhs.Value /= rhs;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(FloatVariable))
|
||
|
{
|
||
|
FloatVariable lhs = (variable as FloatVariable);
|
||
|
float rhs = floatData.Value;
|
||
|
|
||
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
8 years ago
|
lhs.Value = rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Add:
|
||
8 years ago
|
lhs.Value += rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Subtract:
|
||
8 years ago
|
lhs.Value -= rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Multiply:
|
||
8 years ago
|
lhs.Value *= rhs;
|
||
8 years ago
|
break;
|
||
|
case SetOperator.Divide:
|
||
8 years ago
|
lhs.Value /= rhs;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
else if (variable.GetType() == typeof(StringVariable))
|
||
|
{
|
||
|
StringVariable lhs = (variable as StringVariable);
|
||
|
string rhs = stringData.Value;
|
||
|
|
||
|
switch (setOperator)
|
||
|
{
|
||
|
default:
|
||
|
case SetOperator.Assign:
|
||
8 years ago
|
lhs.Value = rhs;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|