Browse Source

Variables are now components

master
chrisgregan 11 years ago
parent
commit
5b725896a3
  1. 12
      Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs
  2. 23
      Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs
  3. 52
      Assets/Fungus/Editor/FungusScript/JumpEditor.cs
  4. 12
      Assets/Fungus/Editor/FungusScript/SayEditor.cs
  5. 37
      Assets/Fungus/Editor/FungusScript/SequenceEditor.cs
  6. 52
      Assets/Fungus/Editor/FungusScript/SetEditor.cs
  7. 48
      Assets/Fungus/Editor/FungusScript/VariablesWindow.cs
  8. 9
      Assets/Fungus/VisualScripting/AddOption.cs
  9. 18
      Assets/Fungus/VisualScripting/BooleanVariable.cs
  10. 8
      Assets/Fungus/VisualScripting/BooleanVariable.cs.meta
  11. 18
      Assets/Fungus/VisualScripting/FloatVariable.cs
  12. 8
      Assets/Fungus/VisualScripting/FloatVariable.cs.meta
  13. 14
      Assets/Fungus/VisualScripting/FungusScript.cs
  14. 18
      Assets/Fungus/VisualScripting/IntegerVariable.cs
  15. 8
      Assets/Fungus/VisualScripting/IntegerVariable.cs.meta
  16. 61
      Assets/Fungus/VisualScripting/Jump.cs
  17. 9
      Assets/Fungus/VisualScripting/Say.cs
  18. 61
      Assets/Fungus/VisualScripting/Set.cs
  19. 18
      Assets/Fungus/VisualScripting/StringVariable.cs
  20. 8
      Assets/Fungus/VisualScripting/StringVariable.cs.meta
  21. 62
      Assets/Fungus/VisualScripting/Variable.cs
  22. BIN
      Assets/Shuttle/ShuttleGame.unity

12
Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs

@ -20,17 +20,15 @@ namespace Fungus.Script
Sequence targetSequence = SequenceEditor.SequenceField(new GUIContent("Target Sequence", "Sequence to execute when option is selected"), t.GetFungusScript(), t.targetSequence); Sequence targetSequence = SequenceEditor.SequenceField(new GUIContent("Target Sequence", "Sequence to execute when option is selected"), t.GetFungusScript(), t.targetSequence);
AddOption.ShowCondition showCondition = (AddOption.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this option should be visible."), t.showCondition); AddOption.ShowCondition showCondition = (AddOption.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this option should be visible."), t.showCondition);
string booleanVariableKey = t.booleanVariableKey; BooleanVariable booleanVariable = t.booleanVariable;
if (showCondition == AddOption.ShowCondition.BooleanIsFalse || if (showCondition == AddOption.ShowCondition.BooleanIsFalse ||
showCondition == AddOption.ShowCondition.BooleanIsTrue) showCondition == AddOption.ShowCondition.BooleanIsTrue)
{ {
VariableType type = VariableType.Boolean; booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
booleanVariableKey = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
t.GetFungusScript (), t.GetFungusScript (),
t.booleanVariableKey, t.booleanVariable,
ref type, v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable;
v => { return v.type == VariableType.Boolean; });
} }
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
@ -39,7 +37,7 @@ namespace Fungus.Script
t.optionText = optionText; t.optionText = optionText;
t.targetSequence = targetSequence; t.targetSequence = targetSequence;
t.showCondition = showCondition; t.showCondition = showCondition;
t.booleanVariableKey = booleanVariableKey; t.booleanVariable = booleanVariable;
} }
} }
} }

23
Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs

@ -3,12 +3,12 @@ using UnityEngine;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Fungus; using Fungus;
using Rotorz.ReorderableList; //using Rotorz.ReorderableList;
using System.Linq; //using System.Linq;
namespace Fungus.Script namespace Fungus.Script
{ {
/*
[CustomPropertyDrawer (typeof(Variable))] [CustomPropertyDrawer (typeof(Variable))]
public class VariableDrawer : PropertyDrawer public class VariableDrawer : PropertyDrawer
{ {
@ -56,20 +56,21 @@ namespace Fungus.Script
EditorGUI.EndProperty(); EditorGUI.EndProperty();
} }
} }
*/
[CustomEditor (typeof(FungusScript))] [CustomEditor (typeof(FungusScript))]
public class FungusScriptEditor : Editor public class FungusScriptEditor : Editor
{ {
SerializedProperty variablesProperty; SerializedProperty variablesProperty;
void OnEnable() //void OnEnable()
{ //{
variablesProperty = serializedObject.FindProperty("variables"); // variablesProperty = serializedObject.FindProperty("variables");
} //}
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
serializedObject.Update(); //serializedObject.Update();
FungusScript t = target as FungusScript; FungusScript t = target as FungusScript;
@ -111,10 +112,10 @@ namespace Fungus.Script
EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style); EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style);
} }
ReorderableListGUI.Title("Variables"); //ReorderableListGUI.Title("Variables");
ReorderableListGUI.ListField(variablesProperty); //ReorderableListGUI.ListField(variablesProperty);
serializedObject.ApplyModifiedProperties(); //serializedObject.ApplyModifiedProperties();
} }
} }

52
Assets/Fungus/Editor/FungusScript/JumpEditor.cs

@ -37,42 +37,27 @@ namespace Fungus.Script
return; return;
} }
List<string> keys = new List<string>(); FungusVariable fungusVariable = SequenceEditor.VariableField(new GUIContent("Compare Variable", "Variable to use in compare operation"),
keys.Add("<None>"); t.GetFungusScript(),
int index = 0; t.variable,
for (int i = 0; i < sc.variables.Count; ++i) null);
{
Variable v = sc.variables[i];
keys.Add(v.key);
if (v.key == t.variableKey &&
index == 0)
{
index = i + 1;
}
}
int newIndex = EditorGUILayout.Popup("Compare Variable", index, keys.ToArray());
bool keyChanged = (t.variableKey != keys[newIndex]);
if (keyChanged) if (fungusVariable != t.variable)
{ {
Undo.RecordObject(t, "Select variable"); Undo.RecordObject(t, "Select variable");
t.variableKey = keys[newIndex]; t.variable = fungusVariable;
} }
if (t.variableKey == "<None>") if (t.variable == null)
{ {
return; return;
} }
VariableType variableType = sc.variables[newIndex - 1].type;
List<GUIContent> operatorList = new List<GUIContent>(); List<GUIContent> operatorList = new List<GUIContent>();
operatorList.Add(new GUIContent("==")); operatorList.Add(new GUIContent("=="));
operatorList.Add(new GUIContent("!=")); operatorList.Add(new GUIContent("!="));
if (variableType == VariableType.Integer || if (t.variable.GetType() == typeof(IntegerVariable) ||
variableType == VariableType.Float) t.variable.GetType() == typeof(FloatVariable))
{ {
operatorList.Add(new GUIContent("<")); operatorList.Add(new GUIContent("<"));
operatorList.Add(new GUIContent(">")); operatorList.Add(new GUIContent(">"));
@ -95,20 +80,21 @@ namespace Fungus.Script
float floatValue = t.floatData.value; float floatValue = t.floatData.value;
string stringValue = t.stringData.value; string stringValue = t.stringData.value;
switch (variableType) if (t.variable.GetType() == typeof(BooleanVariable))
{ {
case VariableType.Boolean:
booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue);
break; }
case VariableType.Integer: else if (t.variable.GetType() == typeof(IntegerVariable))
{
integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
break; }
case VariableType.Float: else if (t.variable.GetType() == typeof(FloatVariable))
{
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
break; }
case VariableType.String: else if (t.variable.GetType() == typeof(StringVariable))
{
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue);
break;
} }
if (booleanValue != t.booleanData.value) if (booleanValue != t.booleanData.value)

12
Assets/Fungus/Editor/FungusScript/SayEditor.cs

@ -24,17 +24,15 @@ namespace Fungus.Script
Say.ShowCondition showCondition = (Say.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this say text should be visible."), t.showCondition); Say.ShowCondition showCondition = (Say.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this say text should be visible."), t.showCondition);
string booleanVariableKey = t.booleanVariableKey; BooleanVariable booleanVariable = t.booleanVariable;
if (showCondition == Say.ShowCondition.BooleanIsFalse || if (showCondition == Say.ShowCondition.BooleanIsFalse ||
showCondition == Say.ShowCondition.BooleanIsTrue) showCondition == Say.ShowCondition.BooleanIsTrue)
{ {
VariableType type = VariableType.Boolean; booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
booleanVariableKey = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
t.GetFungusScript (), t.GetFungusScript (),
t.booleanVariableKey, t.booleanVariable,
ref type, v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable;
v => { return v.type == VariableType.Boolean; });
} }
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
@ -43,7 +41,7 @@ namespace Fungus.Script
t.character = character; t.character = character;
t.text = text; t.text = text;
t.showCondition = showCondition; t.showCondition = showCondition;
t.booleanVariableKey = booleanVariableKey; t.booleanVariable = booleanVariable;
} }
} }

37
Assets/Fungus/Editor/FungusScript/SequenceEditor.cs

@ -3,6 +3,7 @@ using UnityEngine;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Fungus.Script namespace Fungus.Script
{ {
@ -48,15 +49,19 @@ namespace Fungus.Script
return result; return result;
} }
static public string VariableField(GUIContent label, FungusScript fungusScript, string variableKey, ref VariableType variableType, Func<Variable, bool> filter = null) static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func<FungusVariable, bool> filter = null)
{ {
List<string> keys = new List<string>(); List<string> variableKeys = new List<string>();
keys.Add("<None>"); List<FungusVariable> variableObjects = new List<FungusVariable>();
variableKeys.Add("<None>");
variableObjects.Add(null);
FungusVariable[] variables = fungusScript.GetComponents<FungusVariable>();
int index = 0; int index = 0;
for (int i = 0; i < fungusScript.variables.Count; ++i) int selectedIndex = 0;
foreach (FungusVariable v in variables)
{ {
Variable v = fungusScript.variables[i];
if (filter != null) if (filter != null)
{ {
if (!filter(v)) if (!filter(v))
@ -65,22 +70,20 @@ namespace Fungus.Script
} }
} }
keys.Add(v.key); variableKeys.Add(v.key);
if (v.key == variableKey && variableObjects.Add(v);
index == 0)
{
index = i + 1;
}
}
int newIndex = EditorGUILayout.Popup(label.text, index, keys.ToArray()); index++;
if (newIndex > 0) if (v == variable)
{ {
variableType = fungusScript.variables[newIndex - 1].type; selectedIndex = index;
} }
}
selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray());
return keys[newIndex]; return variableObjects[selectedIndex];
} }
} }

52
Assets/Fungus/Editor/FungusScript/SetEditor.cs

@ -19,32 +19,29 @@ namespace Fungus.Script
return; return;
} }
VariableType variableType = VariableType.Boolean; FungusVariable variable = SequenceEditor.VariableField(new GUIContent("Variable", "Variable to set"),
string variableKey = SequenceEditor.VariableField(new GUIContent("Variable", "Variable to set"),
fungusScript, fungusScript,
t.variableKey, t.variable);
ref variableType);
if (variable != t.variable)
if (variableKey != t.variableKey)
{ {
Undo.RecordObject(t, "Set Variable Key"); Undo.RecordObject(t, "Set Variable Key");
t.variableKey = variableKey; t.variable = variable;
} }
if (t.variableKey == "<None>") if (t.variable == null)
{ {
return; return;
} }
List<GUIContent> operatorsList = new List<GUIContent>(); List<GUIContent> operatorsList = new List<GUIContent>();
operatorsList.Add(new GUIContent("=")); operatorsList.Add(new GUIContent("="));
if (variableType == VariableType.Boolean) if (variable.GetType() == typeof(BooleanVariable))
{ {
operatorsList.Add(new GUIContent("!")); operatorsList.Add(new GUIContent("!"));
} }
else if (variableType == VariableType.Integer || else if (variable.GetType() == typeof(IntegerVariable) ||
variableType == VariableType.Float) variable.GetType() == typeof(FloatVariable))
{ {
operatorsList.Add(new GUIContent("+")); operatorsList.Add(new GUIContent("+"));
operatorsList.Add(new GUIContent("-")); operatorsList.Add(new GUIContent("-"));
@ -79,11 +76,9 @@ namespace Fungus.Script
selectedIndex = EditorGUILayout.Popup(new GUIContent("Operator", "Arithmetic operator to use"), selectedIndex, operatorsList.ToArray()); selectedIndex = EditorGUILayout.Popup(new GUIContent("Operator", "Arithmetic operator to use"), selectedIndex, operatorsList.ToArray());
Set.SetOperator setOperator = Set.SetOperator.Assign; Set.SetOperator setOperator = Set.SetOperator.Assign;
switch (variableType) if (variable.GetType() == typeof(BooleanVariable) ||
variable.GetType() == typeof(StringVariable))
{ {
default:
case VariableType.Boolean:
case VariableType.String:
switch (selectedIndex) switch (selectedIndex)
{ {
default: default:
@ -94,9 +89,10 @@ namespace Fungus.Script
setOperator = Set.SetOperator.Negate; setOperator = Set.SetOperator.Negate;
break; break;
} }
break; }
case VariableType.Integer: else if (variable.GetType() == typeof(IntegerVariable) ||
case VariableType.Float: variable.GetType() == typeof(FloatVariable))
{
switch (selectedIndex) switch (selectedIndex)
{ {
default: default:
@ -116,7 +112,6 @@ namespace Fungus.Script
setOperator = Set.SetOperator.Divide; setOperator = Set.SetOperator.Divide;
break; break;
} }
break;
} }
if (setOperator != t.setOperator) if (setOperator != t.setOperator)
@ -130,20 +125,21 @@ namespace Fungus.Script
float floatValue = t.floatData.value; float floatValue = t.floatData.value;
string stringValue = t.stringData.value; string stringValue = t.stringData.value;
switch (variableType) if (variable.GetType() == typeof(BooleanVariable))
{ {
case VariableType.Boolean:
booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue);
break; }
case VariableType.Integer: else if (variable.GetType() == typeof(IntegerVariable))
{
integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
break; }
case VariableType.Float: else if (variable.GetType() == typeof(FloatVariable))
{
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
break; }
case VariableType.String: else if (variable.GetType() == typeof(StringVariable))
{
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue);
break;
} }
if (booleanValue != t.booleanData.value) if (booleanValue != t.booleanData.value)

48
Assets/Fungus/Editor/FungusScript/VariablesWindow.cs

@ -27,24 +27,25 @@ namespace Fungus.Script
} }
// Warn about conflicting global variable types // Warn about conflicting global variable types
Dictionary<string, VariableType> globalTypes = new Dictionary<string, VariableType>(); Dictionary<string, FungusVariable> globals = new Dictionary<string, FungusVariable>();
FungusScript[] fungusScripts = GameObject.FindObjectsOfType<FungusScript>(); FungusScript[] fungusScripts = GameObject.FindObjectsOfType<FungusScript>();
foreach (FungusScript fs in fungusScripts) foreach (FungusScript fs in fungusScripts)
{ {
foreach (Variable v in fs.variables) FungusVariable[] variables = fs.GetComponents<FungusVariable>();
foreach (FungusVariable v in variables)
{ {
if (v.scope == VariableScope.Global) if (v.scope == VariableScope.Global)
{ {
if (globalTypes.ContainsKey(v.key)) if (globals.ContainsKey(v.key))
{ {
if (globalTypes[v.key] != v.type) if (globals[v.key].GetType() != v.GetType())
{ {
GUIStyle errorStyle = new GUIStyle(GUI.skin.label); GUIStyle errorStyle = new GUIStyle(GUI.skin.label);
errorStyle.normal.textColor = new Color(1,0,0); errorStyle.normal.textColor = new Color(1,0,0);
GUILayout.Label("Error: Global '" + v.key + "' must use the same type in all scripts.", errorStyle); GUILayout.Label("Error: Global '" + v.key + "' must use the same type in all scripts.", errorStyle);
} }
} }
globalTypes[v.key] = v.type; globals[v.key] = v;
} }
} }
} }
@ -71,7 +72,8 @@ namespace Fungus.Script
boxStyle.margin.top = 0; boxStyle.margin.top = 0;
boxStyle.margin.bottom = 0; boxStyle.margin.bottom = 0;
foreach (Variable variable in fungusScript.variables) FungusVariable[] fsVariables = fungusScript.GetComponents<FungusVariable>();
foreach (FungusVariable variable in fsVariables)
{ {
GUILayout.BeginHorizontal(boxStyle); GUILayout.BeginHorizontal(boxStyle);
@ -91,33 +93,31 @@ namespace Fungus.Script
break; break;
} }
switch (variable.type) if (variable.GetType() == typeof(BooleanVariable))
{ {
case VariableType.Boolean:
typeString = "Boolean"; typeString = "Boolean";
valueString = variable.BooleanValue ? "True" : "False"; valueString = (variable as BooleanVariable).Value ? "True" : "False";
break; }
case VariableType.Integer: else if (variable.GetType() == typeof(IntegerVariable))
{
typeString = "Integer"; typeString = "Integer";
valueString = variable.IntegerValue.ToString(); valueString = (variable as IntegerVariable).Value.ToString();
break; }
case VariableType.Float: else if (variable.GetType() == typeof(FloatVariable))
{
typeString = "Float"; typeString = "Float";
valueString = variable.FloatValue.ToString(); valueString = (variable as FloatVariable).Value.ToString();
break; }
case VariableType.String: else if (variable.GetType() == typeof(StringVariable))
{
typeString = "String"; typeString = "String";
valueString = (variable as StringVariable).Value;
if (variable.StringValue == null || if (valueString == null ||
variable.StringValue.Length == 0) valueString.Length == 0)
{ {
valueString = "\"\""; valueString = "\"\"";
} }
else
{
valueString = variable.StringValue;
}
break;
} }
GUILayout.Label(keyString, GUILayout.Width(columnWidth)); GUILayout.Label(keyString, GUILayout.Width(columnWidth));

9
Assets/Fungus/VisualScripting/AddOption.cs

@ -18,7 +18,7 @@ namespace Fungus.Script
public string optionText; public string optionText;
public Sequence targetSequence; public Sequence targetSequence;
public ShowCondition showCondition; public ShowCondition showCondition;
public string booleanVariableKey; public BooleanVariable booleanVariable;
public override void OnEnter() public override void OnEnter()
{ {
@ -39,20 +39,19 @@ namespace Fungus.Script
} }
else else
{ {
Variable v = parentFungusScript.GetVariable(booleanVariableKey); if (booleanVariable == null)
if (v == null)
{ {
showOption = false; showOption = false;
} }
else else
{ {
if (showCondition == ShowCondition.BooleanIsTrue && if (showCondition == ShowCondition.BooleanIsTrue &&
v.BooleanValue != true) booleanVariable.Value != true)
{ {
showOption = false; showOption = false;
} }
else if (showCondition == ShowCondition.BooleanIsFalse && else if (showCondition == ShowCondition.BooleanIsFalse &&
v.BooleanValue != false) booleanVariable.Value != false)
{ {
showOption = false; showOption = false;
} }

18
Assets/Fungus/VisualScripting/BooleanVariable.cs

@ -0,0 +1,18 @@
using UnityEngine;
using System.Collections;
namespace Fungus.Script
{
public class BooleanVariable : FungusVariable
{
bool booleanValue;
public bool Value
{
get { return (scope == VariableScope.Local) ? booleanValue : Variables.GetBoolean(key); }
set { if (scope == VariableScope.Local) { booleanValue = value; } else { Variables.SetBoolean(key, value); } }
}
}
}

8
Assets/Fungus/VisualScripting/BooleanVariable.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5d02d9822eec54c98afe95bb497211b3
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

18
Assets/Fungus/VisualScripting/FloatVariable.cs

@ -0,0 +1,18 @@
using UnityEngine;
using System.Collections;
namespace Fungus.Script
{
public class FloatVariable : FungusVariable
{
float floatValue;
public float Value
{
get { return (scope == VariableScope.Local) ? floatValue : Variables.GetFloat(key); }
set { if (scope == VariableScope.Local) { floatValue = value; } else { Variables.SetFloat(key, value); } }
}
}
}

8
Assets/Fungus/VisualScripting/FloatVariable.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 705fa1ac97df74e3a84ff952ffd923f1
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

14
Assets/Fungus/VisualScripting/FungusScript.cs

@ -18,8 +18,6 @@ namespace Fungus.Script
[System.NonSerialized] [System.NonSerialized]
public Sequence activeSequence; public Sequence activeSequence;
public List<Variable> variables = new List<Variable>();
public bool startAutomatically = false; public bool startAutomatically = false;
void Start() void Start()
@ -54,18 +52,6 @@ namespace Fungus.Script
activeSequence = sequence; activeSequence = sequence;
sequence.ExecuteNextCommand(); sequence.ExecuteNextCommand();
} }
public Variable GetVariable(string key)
{
foreach (Variable v in variables)
{
if (v.key == key)
{
return v;
}
}
return null;
}
} }
} }

18
Assets/Fungus/VisualScripting/IntegerVariable.cs

@ -0,0 +1,18 @@
using UnityEngine;
using System.Collections;
namespace Fungus.Script
{
public class IntegerVariable : FungusVariable
{
int integerValue;
public int Value
{
get { return (scope == VariableScope.Local) ? integerValue : Variables.GetInteger(key); }
set { if (scope == VariableScope.Local) { integerValue = value; } else { Variables.SetInteger(key, value); } }
}
}
}

8
Assets/Fungus/VisualScripting/IntegerVariable.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: afb91b566ceda411bad1e9d3c3243ecc
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

61
Assets/Fungus/VisualScripting/Jump.cs

@ -26,7 +26,7 @@ namespace Fungus.Script
public Sequence targetSequence; // Only used for Always condition public Sequence targetSequence; // Only used for Always condition
public string variableKey; public FungusVariable variable;
public CompareOperator compareOperator; public CompareOperator compareOperator;
@ -51,82 +51,87 @@ namespace Fungus.Script
return; return;
} }
Variable v = parentFungusScript.GetVariable(variableKey);
bool condition = false; bool condition = false;
switch (v.type) if (variable == null)
{
Continue();
return;
}
if (variable.GetType() == typeof(BooleanVariable))
{ {
case VariableType.Boolean:
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (v.BooleanValue == booleanData.value); condition = (variable as BooleanVariable).Value == booleanData.value;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
default: default:
condition = (v.BooleanValue != booleanData.value); condition = (variable as BooleanVariable).Value != booleanData.value;
break; break;
} }
break; }
case VariableType.Integer: else if (variable.GetType() == typeof(IntegerVariable))
{
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (v.IntegerValue == integerData.value); condition = (variable as IntegerVariable).Value == integerData.value;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
condition = (v.IntegerValue != integerData.value); condition = (variable as IntegerVariable).Value != integerData.value;
break; break;
case CompareOperator.LessThan: case CompareOperator.LessThan:
condition = (v.IntegerValue < integerData.value); condition = (variable as IntegerVariable).Value < integerData.value;
break; break;
case CompareOperator.GreaterThan: case CompareOperator.GreaterThan:
condition = (v.IntegerValue > integerData.value); condition = (variable as IntegerVariable).Value > integerData.value;
break; break;
case CompareOperator.LessThanOrEquals: case CompareOperator.LessThanOrEquals:
condition = (v.IntegerValue <= integerData.value); condition = (variable as IntegerVariable).Value <= integerData.value;
break; break;
case CompareOperator.GreaterThanOrEquals: case CompareOperator.GreaterThanOrEquals:
condition = (v.IntegerValue >= integerData.value); condition = (variable as IntegerVariable).Value >= integerData.value;
break; break;
} }
break; }
case VariableType.Float: else if (variable.GetType() == typeof(FloatVariable))
{
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (v.FloatValue == floatData.value); condition = (variable as FloatVariable).Value == floatData.value;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
condition = (v.FloatValue != floatData.value); condition = (variable as FloatVariable).Value != floatData.value;
break; break;
case CompareOperator.LessThan: case CompareOperator.LessThan:
condition = (v.FloatValue < floatData.value); condition = (variable as FloatVariable).Value < floatData.value;
break; break;
case CompareOperator.GreaterThan: case CompareOperator.GreaterThan:
condition = (v.FloatValue > floatData.value); condition = (variable as FloatVariable).Value > floatData.value;
break; break;
case CompareOperator.LessThanOrEquals: case CompareOperator.LessThanOrEquals:
condition = (v.FloatValue <= floatData.value); condition = (variable as FloatVariable).Value <= floatData.value;
break; break;
case CompareOperator.GreaterThanOrEquals: case CompareOperator.GreaterThanOrEquals:
condition = (v.FloatValue >= floatData.value); condition = (variable as FloatVariable).Value >= floatData.value;
break; break;
} }
break; }
case VariableType.String: else if (variable.GetType() == typeof(StringVariable))
{
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (v.StringValue == stringData.value); condition = (variable as StringVariable).Value == stringData.value;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
default: default:
condition = (v.StringValue != stringData.value); condition = (variable as StringVariable).Value != stringData.value;
break; break;
} }
break;
} }
if (condition) if (condition)

9
Assets/Fungus/VisualScripting/Say.cs

@ -18,7 +18,7 @@ namespace Fungus.Script
public string character; public string character;
public string text; public string text;
public ShowCondition showCondition; public ShowCondition showCondition;
public string booleanVariableKey; public BooleanVariable booleanVariable;
int executionCount; int executionCount;
@ -46,20 +46,19 @@ namespace Fungus.Script
} }
else else
{ {
Variable v = parentFungusScript.GetVariable(booleanVariableKey); if (booleanVariable == null)
if (v == null)
{ {
showSayText = false; showSayText = false;
} }
else else
{ {
if (showCondition == ShowCondition.BooleanIsTrue && if (showCondition == ShowCondition.BooleanIsTrue &&
v.BooleanValue != true) booleanVariable.Value != true)
{ {
showSayText = false; showSayText = false;
} }
else if (showCondition == ShowCondition.BooleanIsFalse && else if (showCondition == ShowCondition.BooleanIsFalse &&
v.BooleanValue != false) booleanVariable.Value != false)
{ {
showSayText = false; showSayText = false;
} }

61
Assets/Fungus/VisualScripting/Set.cs

@ -16,7 +16,7 @@ namespace Fungus.Script
Divide // / Divide // /
} }
public string variableKey; public FungusVariable variable;
public SetOperator setOperator; public SetOperator setOperator;
@ -30,89 +30,82 @@ namespace Fungus.Script
public override void OnEnter() public override void OnEnter()
{ {
if (variableKey.Length == 0) if (variable == null)
{ {
Continue(); Continue();
return; return;
} }
Variable v = parentFungusScript.GetVariable(variableKey); if (variable.GetType() == typeof(BooleanVariable))
if (v == null)
{ {
Debug.LogError("Variable " + variableKey + " not defined.");
}
else
{
switch (v.type)
{
case VariableType.Boolean:
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
v.BooleanValue = booleanData.value; (variable as BooleanVariable).Value = booleanData.value;
break; break;
case SetOperator.Negate: case SetOperator.Negate:
v.BooleanValue = !booleanData.value; (variable as BooleanVariable).Value = !booleanData.value;
break; break;
} }
break; }
case VariableType.Integer: else if (variable.GetType() == typeof(IntegerVariable))
{
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
v.IntegerValue = integerData.value; (variable as IntegerVariable).Value = integerData.value;
break; break;
case SetOperator.Negate: case SetOperator.Negate:
v.IntegerValue = -integerData.value; (variable as IntegerVariable).Value = -integerData.value;
break; break;
case SetOperator.Add: case SetOperator.Add:
v.IntegerValue += integerData.value; (variable as IntegerVariable).Value += integerData.value;
break; break;
case SetOperator.Subtract: case SetOperator.Subtract:
v.IntegerValue -= integerData.value; (variable as IntegerVariable).Value -= integerData.value;
break; break;
case SetOperator.Multiply: case SetOperator.Multiply:
v.IntegerValue *= integerData.value; (variable as IntegerVariable).Value *= integerData.value;
break; break;
case SetOperator.Divide: case SetOperator.Divide:
v.IntegerValue /= integerData.value; (variable as IntegerVariable).Value /= integerData.value;
break; break;
} }
break; }
case VariableType.Float: else if (variable.GetType() == typeof(FloatVariable))
{
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
v.FloatValue = floatData.value; (variable as FloatVariable).Value = floatData.value;
break; break;
case SetOperator.Negate: case SetOperator.Negate:
v.FloatValue = -floatData.value; (variable as FloatVariable).Value = -floatData.value;
break; break;
case SetOperator.Add: case SetOperator.Add:
v.FloatValue += floatData.value; (variable as FloatVariable).Value += floatData.value;
break; break;
case SetOperator.Subtract: case SetOperator.Subtract:
v.FloatValue -= floatData.value; (variable as FloatVariable).Value -= floatData.value;
break; break;
case SetOperator.Multiply: case SetOperator.Multiply:
v.FloatValue *= floatData.value; (variable as FloatVariable).Value *= floatData.value;
break; break;
case SetOperator.Divide: case SetOperator.Divide:
v.FloatValue /= floatData.value; (variable as FloatVariable).Value /= floatData.value;
break; break;
} }
break; }
case VariableType.String: else if (variable.GetType() == typeof(StringVariable))
{
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
v.StringValue = stringData.value; (variable as StringVariable).Value = stringData.value;
break;
}
break; break;
} }
} }

18
Assets/Fungus/VisualScripting/StringVariable.cs

@ -0,0 +1,18 @@
using UnityEngine;
using System.Collections;
namespace Fungus.Script
{
public class StringVariable : FungusVariable
{
string stringValue;
public string Value
{
get { return (scope == VariableScope.Local) ? stringValue : Variables.GetString(key); }
set { if (scope == VariableScope.Local) { stringValue = value; } else { Variables.SetString(key, value); } }
}
}
}

8
Assets/Fungus/VisualScripting/StringVariable.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4580f28dd8581476b810b38eea2f1316
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

62
Assets/Fungus/VisualScripting/Variable.cs

@ -4,6 +4,11 @@ using System.Collections;
namespace Fungus.Script namespace Fungus.Script
{ {
public class FungusVariable : MonoBehaviour
{
public VariableScope scope;
public string key;
}
public enum VariableType public enum VariableType
{ {
@ -19,63 +24,6 @@ namespace Fungus.Script
Global Global
}; };
[Serializable]
public class Variable
{
public string key;
public VariableType type;
public VariableScope scope;
bool booleanValue;
int integerValue;
float floatValue;
string stringValue;
public bool BooleanValue
{
get { return (scope == VariableScope.Local) ? booleanValue : Variables.GetBoolean(key); }
set { if (scope == VariableScope.Local) { booleanValue = value; } else { Variables.SetBoolean(key, value); } }
}
public int IntegerValue
{
get { return (scope == VariableScope.Local) ? integerValue : Variables.GetInteger(key); }
set { if (scope == VariableScope.Local) { integerValue = value; } else { Variables.SetInteger(key, value); } }
}
public float FloatValue
{
get { return (scope == VariableScope.Local) ? floatValue : Variables.GetFloat(key); }
set { if (scope == VariableScope.Local) { floatValue = value; } else { Variables.SetFloat(key, value); } }
}
public string StringValue
{
get { return (scope == VariableScope.Local) ? stringValue : Variables.GetString(key); }
set { if (scope == VariableScope.Local) { stringValue = value; } else { Variables.SetString(key, value); } }
}
public bool IsSameType(Variable other)
{
return (this.type == other.type);
}
public bool Assign(Variable other)
{
if (!IsSameType(other))
{
return false;
}
booleanValue = other.booleanValue;
integerValue = other.integerValue;
floatValue = other.floatValue;
stringValue = other.stringValue;
return true;
}
}
[Serializable] [Serializable]
public class VariableData public class VariableData
{ {

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save