From b616f72548c11dba23d0f257129834a10bea3c3f Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 20 Jan 2015 15:14:20 +0000 Subject: [PATCH] Added Else If command #77 --- Assets/Fungus/FungusScript/Editor/IfEditor.cs | 2 +- Assets/Fungus/FungusScript/Scripts/Command.cs | 6 +- .../FungusScript/Scripts/Commands/Else.cs | 27 ++++--- .../FungusScript/Scripts/Commands/ElseIf.cs | 79 +++++++++++++++++++ .../Scripts/Commands/ElseIf.cs.meta | 8 ++ .../FungusScript/Scripts/Commands/If.cs | 26 ++++-- .../FungusScript/Scripts/FungusScript.cs | 2 +- .../Fungus/FungusScript/Scripts/Sequence.cs | 52 ++++++++---- 8 files changed, 165 insertions(+), 37 deletions(-) create mode 100644 Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs create mode 100644 Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs.meta diff --git a/Assets/Fungus/FungusScript/Editor/IfEditor.cs b/Assets/Fungus/FungusScript/Editor/IfEditor.cs index 29da556d..77015fd4 100644 --- a/Assets/Fungus/FungusScript/Editor/IfEditor.cs +++ b/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; diff --git a/Assets/Fungus/FungusScript/Scripts/Command.cs b/Assets/Fungus/FungusScript/Scripts/Command.cs index a08fa8f1..f2b89caa 100644 --- a/Assets/Fungus/FungusScript/Scripts/Command.cs +++ b/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); } } diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/Else.cs b/Assets/Fungus/FungusScript/Scripts/Commands/Else.cs index 8d4e7ad2..b6036bf2 100644 --- a/Assets/Fungus/FungusScript/Scripts/Commands/Else.cs +++ b/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(); } diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs b/Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs new file mode 100644 index 00000000..b0d68367 --- /dev/null +++ b/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); + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs.meta b/Assets/Fungus/FungusScript/Scripts/Commands/ElseIf.cs.meta new file mode 100644 index 00000000..343d868d --- /dev/null +++ b/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: diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/If.cs b/Assets/Fungus/FungusScript/Scripts/Commands/If.cs index a533edc9..e5fb427b 100644 --- a/Assets/Fungus/FungusScript/Scripts/Commands/If.cs +++ b/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 diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index d975f57d..df0747e4 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/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; } diff --git a/Assets/Fungus/FungusScript/Scripts/Sequence.cs b/Assets/Fungus/FungusScript/Scripts/Sequence.cs index 12f16b43..e979b33e 100644 --- a/Assets/Fungus/FungusScript/Scripts/Sequence.cs +++ b/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; + } } }