Browse Source

Added Else If command #77

master
chrisgregan 10 years ago
parent
commit
b616f72548
  1. 2
      Assets/Fungus/FungusScript/Editor/IfEditor.cs
  2. 6
      Assets/Fungus/FungusScript/Scripts/Command.cs
  3. 27
      Assets/Fungus/FungusScript/Scripts/Commands/Else.cs
  4. 79
      Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs
  5. 8
      Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs.meta
  6. 26
      Assets/Fungus/FungusScript/Scripts/Commands/If.cs
  7. 2
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  8. 52
      Assets/Fungus/FungusScript/Scripts/Sequence.cs

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

@ -7,7 +7,7 @@ using System.Reflection;
namespace Fungus
{
[CustomEditor (typeof(If))]
[CustomEditor (typeof(If), true)]
public class IfEditor : CommandEditor
{
protected SerializedProperty variableProp;

6
Assets/Fungus/FungusScript/Scripts/Command.cs

@ -75,15 +75,15 @@ namespace Fungus
public virtual void Continue()
{
Continue(this);
Continue(commandIndex + 1);
}
public virtual void Continue(Command currentCommand)
public virtual void Continue(int nextCommandIndex)
{
OnExit();
if (parentSequence != null)
{
parentSequence.ExecuteNextCommand(currentCommand);
parentSequence.ExecuteCommand(nextCommandIndex);
}
}

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

@ -6,7 +6,7 @@ namespace Fungus
{
[CommandInfo("Scripting",
"Else",
"Marks the start of a sequence block to be executed when the preceding If statement is False.")]
"Marks the start of a command block to be executed when the preceding If statement is False.")]
[AddComponentMenu("")]
public class Else : Command
{
@ -17,30 +17,33 @@ namespace Fungus
return;
}
// Stop if this is the last command in the list
if (commandIndex >= parentSequence.commandList.Count - 1)
{
Stop();
return;
}
// Find the next End command at the same indent level as this Else command
bool foundThisCommand = false;
int indent = indentLevel;
foreach(Command command in parentSequence.commandList)
for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i)
{
if (foundThisCommand &&
command.indentLevel == indent)
Command command = parentSequence.commandList[i];
if (command.indentLevel == indent)
{
System.Type type = command.GetType();
if (type == typeof(EndIf) || // Legacy support for old EndIf command
type == typeof(End))
{
// Execute command immediately after the EndIf command
Continue(command);
Continue(command.commandIndex + 1);
return;
}
}
else if (command == this)
{
foundThisCommand = true;
}
}
// No matching EndIf command found, so just stop the sequence
// No End command found
Stop();
}

79
Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs

@ -0,0 +1,79 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
namespace Fungus
{
[CommandInfo("Scripting",
"Else If",
"Marks the start of a sequence block to be executed when the preceding If statement is False and the test expression is true.")]
[AddComponentMenu("")]
public class ElseIf : If
{
public override void OnEnter()
{
System.Type previousCommandType = parentSequence.GetPreviousActiveCommandType();
if (previousCommandType == typeof(If) ||
previousCommandType == typeof(ElseIf) )
{
// Else If behaves the same as an If command
EvaluateCondition();
}
else
{
// Else If behaves mostly like an Else command,
// but will also jump to a following Else command.
// Stop if this is the last command in the list
if (commandIndex >= parentSequence.commandList.Count - 1)
{
Stop();
return;
}
// Find the next End command at the same indent level as this Else If command
int indent = indentLevel;
for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i)
{
Command command = parentSequence.commandList[i];
if (command.indentLevel == indent)
{
System.Type type = command.GetType();
if (//type == typeof(Else) ||
type == typeof(EndIf) || // Legacy support for old EndIf command
type == typeof(End))
{
// Execute command immediately after the Else or End command
Continue(command.commandIndex + 1);
return;
}
}
}
// No End command found
Stop();
}
}
public override bool OpenBlock()
{
return true;
}
public override bool CloseBlock()
{
return true;
}
public override Color GetButtonColor()
{
return new Color32(253, 253, 150, 255);
}
}
}

8
Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs.meta

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

26
Assets/Fungus/FungusScript/Scripts/Commands/If.cs

@ -7,7 +7,7 @@ namespace Fungus
[CommandInfo("Scripting",
"If",
"If the test expression is true, execute the following block of commands.")]
"If the test expression is true, execute the following command block.")]
[AddComponentMenu("")]
public class If : Condition
{
@ -83,15 +83,22 @@ namespace Fungus
}
}
public void OnTrue()
protected virtual void OnTrue()
{
Continue();
}
public void OnFalse()
protected virtual void OnFalse()
{
// Find the next Else or EndIf command at the same indent level as this If command
for (int i = commandIndex; i < parentSequence.commandList.Count; ++i)
// Last command in sequence
if (commandIndex >= parentSequence.commandList.Count)
{
Stop();
return;
}
// Find the next Else, ElseIf or End command at the same indent level as this If command
for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i)
{
Command nextCommand = parentSequence.commandList[i];
@ -117,10 +124,17 @@ namespace Fungus
else
{
// Execute command immediately after the Else or End command
Continue(nextCommand);
Continue(nextCommand.commandIndex + 1);
return;
}
}
else if (type == typeof(ElseIf))
{
// Execute the Else If command
Continue(i);
return;
}
}
// No matching End command found, so just stop the sequence

2
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -168,7 +168,7 @@ namespace Fungus
}
// Execute the first command in the command list
sequence.ExecuteNextCommand();
sequence.ExecuteCommand(0);
return true;
}

52
Assets/Fungus/FungusScript/Scripts/Sequence.cs

@ -25,6 +25,12 @@ namespace Fungus
[System.NonSerialized]
public Command activeCommand;
// Index of last command executed before the current one
// -1 indicates no previous command
[HideInInspector]
[System.NonSerialized]
public int previousActiveCommandIndex = -1;
[HideInInspector]
[System.NonSerialized]
public float executingIconTimer;
@ -37,6 +43,7 @@ namespace Fungus
protected virtual void Awake()
{
// Give each child command a reference back to its parent sequence
// and tell each command its index in the list.
int index = 0;
foreach (Command command in commandList)
{
@ -104,32 +111,38 @@ namespace Fungus
return executionCount;
}
public virtual void ExecuteNextCommand(Command currentCommand = null)
public virtual void ExecuteCommand(int commandIndex)
{
if (currentCommand == null)
if (activeCommand == null)
{
executionCount++;
previousActiveCommandIndex = -1;
}
else
{
previousActiveCommandIndex = activeCommand.commandIndex;
}
FungusScript fungusScript = GetFungusScript();
if (commandIndex >= commandList.Count)
{
Stop();
return;
}
int commandIndex = 0;
if (currentCommand != null)
if (commandIndex == 0)
{
commandIndex = currentCommand.commandIndex + 1;
executionCount++;
}
FungusScript fungusScript = GetFungusScript();
// Skip disabled commands and comments
while (commandIndex < commandList.Count &&
(!commandList[commandIndex].enabled || commandList[commandIndex].GetType() == typeof(Comment)))
{
commandIndex = commandList[commandIndex].commandIndex + 1;
}
Command nextCommand = null;
if (commandIndex < commandList.Count)
{
nextCommand = commandList[commandIndex];
}
Command nextCommand = commandList[commandIndex];
activeCommand = null;
executingIconTimer = 0.5f;
@ -143,8 +156,8 @@ namespace Fungus
if (fungusScript.gameObject.activeInHierarchy)
{
// Auto select a command in some situations
if ((fungusScript.selectedCommands.Count == 0 && currentCommand == null) ||
(fungusScript.selectedCommands.Count == 1 && fungusScript.selectedCommands[0] == currentCommand))
if ((fungusScript.selectedCommands.Count == 0 && commandIndex == 0) ||
(fungusScript.selectedCommands.Count == 1 && fungusScript.selectedCommands[0].commandIndex == commandIndex))
{
fungusScript.ClearSelectedCommands();
fungusScript.AddSelectedCommand(nextCommand);
@ -192,5 +205,16 @@ namespace Fungus
}
return connectedSequences;
}
public virtual System.Type GetPreviousActiveCommandType()
{
if (previousActiveCommandIndex >= 0 &&
previousActiveCommandIndex < commandList.Count)
{
return commandList[previousActiveCommandIndex].GetType();
}
return null;
}
}
}

Loading…
Cancel
Save