Browse Source

Implemented If, Else, EndIf logic

master
chrisgregan 11 years ago
parent
commit
2efceb62ea
  1. 27
      Assets/Fungus/FungusScript/Commands/Else.cs
  2. 72
      Assets/Fungus/FungusScript/Commands/If.cs
  3. 9
      Assets/Fungus/FungusScript/Editor/FungusScriptEditor.cs
  4. 23
      Assets/Fungus/FungusScript/Editor/IfEditor.cs
  5. 7
      Assets/Fungus/FungusScript/Scripts/FungusCommand.cs
  6. BIN
      Assets/Shuttle/ShuttleGame.unity

27
Assets/Fungus/FungusScript/Commands/Else.cs

@ -12,9 +12,32 @@ namespace Fungus.Script
{
public override void OnEnter()
{
Continue();
}
// Find the next EndIf command at the same indent level as this Else command
bool foundThisCommand = false;
int indent = indentLevel;
foreach(FungusCommand command in parentSequence.commandList)
{
if (foundThisCommand &&
command.indentLevel == indent)
{
System.Type type = command.GetType();
if (type == typeof(EndIf))
{
// Execute command immediately after the EndIf command
Continue(command);
return;
}
}
else if (command == this)
{
foundThisCommand = true;
}
}
// No matching EndIf command found, so just stop the sequence
Stop();
}
public override int GetPreIndent()
{
return -1;

72
Assets/Fungus/FungusScript/Commands/If.cs

@ -16,7 +16,7 @@ namespace Fungus.Script
[CommandInfo("Scripting",
"If",
"Execute another sequence IF a condition is true. Sequences can be specified for both true (THEN) and false (ELSE) conditions.",
"If the test expression is true, execute the following block of commands.",
253, 253, 150)]
public class If : FungusCommand
{
@ -32,10 +32,6 @@ namespace Fungus.Script
public StringData stringValue;
public Sequence thenSequence;
public Sequence elseSequence;
public override void OnEnter()
{
bool condition = false;
@ -135,36 +131,38 @@ namespace Fungus.Script
if (condition)
{
if (thenSequence != null)
{
ExecuteSequence(thenSequence);
return;
}
Continue();
}
else
{
if (elseSequence != null)
// Find the next Else or EndIf command at the same indent level as this If command
bool foundThisCommand = false;
int indent = indentLevel;
foreach(FungusCommand command in parentSequence.commandList)
{
ExecuteSequence(elseSequence);
return;
if (foundThisCommand &&
command.indentLevel == indent)
{
System.Type type = command.GetType();
if (type == typeof(Else) ||
type == typeof(EndIf))
{
// Execute command immediately after the Else or EndIf command
Continue(command);
return;
}
}
else if (command == this)
{
foundThisCommand = true;
}
}
}
Continue();
}
public override void GetConnectedSequences(ref List<Sequence> connectedSequences)
{
if (thenSequence != null)
{
connectedSequences.Add(thenSequence);
}
if (elseSequence != null)
{
connectedSequences.Add(elseSequence);
// No matching EndIf command found, so just stop the sequence
Stop();
}
}
public override string GetSummary()
{
if (variable == null)
@ -212,26 +210,6 @@ namespace Fungus.Script
summary += stringValue.GetDescription();
}
summary += " THEN ";
if (thenSequence == null)
{
summary += "<Continue>";
}
else
{
summary += thenSequence.name;
}
summary += " ELSE ";
if (elseSequence == null)
{
summary += "<Continue>";
}
else
{
summary += elseSequence.name;
}
return summary;
}

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

@ -26,7 +26,14 @@ namespace Fungus.Script
if (Application.isPlaying)
{
t.selectedCommand = t.executingSequence.activeCommand;
if (t.executingSequence == null)
{
t.selectedCommand = null;
}
else
{
t.selectedCommand = t.executingSequence.activeCommand;
}
}
EditorGUI.BeginChangeCheck();

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

@ -76,29 +76,6 @@ namespace Fungus.Script
EditorGUILayout.PropertyField(serializedObject.FindProperty("stringValue"));
}
EditorGUILayout.Separator();
Sequence thenSequence = SequenceEditor.SequenceField(new GUIContent("Then", "Sequence to execute if comparision is true"),
new GUIContent("<Continue>"),
t.GetFungusScript(),
t.thenSequence);
Sequence elseSequence = SequenceEditor.SequenceField(new GUIContent("Else", "Sequence to execute if comparision is false"),
new GUIContent("<Continue>"),
t.GetFungusScript(),
t.elseSequence);
if (thenSequence != t.thenSequence)
{
Undo.RecordObject(t, "Set Then Sequence");
t.thenSequence = thenSequence;
}
if (elseSequence != t.elseSequence)
{
Undo.RecordObject(t, "Set Else Sequence");
t.elseSequence = elseSequence;
}
serializedObject.ApplyModifiedProperties();
}
}

7
Assets/Fungus/FungusScript/Scripts/FungusCommand.cs

@ -80,9 +80,14 @@ namespace Fungus.Script
}
public virtual void Continue()
{
Continue(this);
}
public virtual void Continue(FungusCommand currentCommand)
{
OnExit();
parentSequence.ExecuteNextCommand(this);
parentSequence.ExecuteNextCommand(currentCommand);
}
public virtual void Stop()

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save