diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/Break.cs b/Assets/Fungus/FungusScript/Scripts/Commands/Break.cs new file mode 100644 index 00000000..c2000ac8 --- /dev/null +++ b/Assets/Fungus/FungusScript/Scripts/Commands/Break.cs @@ -0,0 +1,40 @@ +using UnityEngine; +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus +{ + [CommandInfo("Scripting", + "Break", + "Force a loop to terminate immediately.")] + [AddComponentMenu("")] + public class Break : Command + { + public override void OnEnter() + { + // Find next End statement at -1 relative indent level + for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i) + { + End endCommand = parentSequence.commandList[i] as End; + + if (endCommand != null && + endCommand.indentLevel == indentLevel - 1) + { + // Continue at next command after End + Continue (endCommand.commandIndex + 1); + return; + } + } + + // No matching End command found so just continue + Continue(); + } + + 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/Break.cs.meta b/Assets/Fungus/FungusScript/Scripts/Commands/Break.cs.meta new file mode 100644 index 00000000..0f24a0db --- /dev/null +++ b/Assets/Fungus/FungusScript/Scripts/Commands/Break.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c9b9c7a9785c34fc889da2b3a40344db +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/End.cs b/Assets/Fungus/FungusScript/Scripts/Commands/End.cs index 389f716b..2442205f 100644 --- a/Assets/Fungus/FungusScript/Scripts/Commands/End.cs +++ b/Assets/Fungus/FungusScript/Scripts/Commands/End.cs @@ -1,4 +1,5 @@ using UnityEngine; +using System; using System.Collections; using System.Collections.Generic; @@ -10,8 +11,24 @@ namespace Fungus [AddComponentMenu("")] public class End : Command { + [NonSerialized] + public bool loop = false; + public override void OnEnter() { + if (loop) + { + for (int i = commandIndex - 1; i >= 0; --i) + { + System.Type commandType = parentSequence.commandList[i].GetType(); + if (commandType == typeof(While)) + { + Continue(i); + return; + } + } + } + Continue(); } diff --git a/Assets/Fungus/FungusScript/Scripts/Commands/While.cs b/Assets/Fungus/FungusScript/Scripts/Commands/While.cs new file mode 100644 index 00000000..69d26f8f --- /dev/null +++ b/Assets/Fungus/FungusScript/Scripts/Commands/While.cs @@ -0,0 +1,59 @@ +using UnityEngine; +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus +{ + [CommandInfo("Scripting", + "While", + "Continuously loop through a block of commands while the condition is true. Use the Break command to force the loop to terminate immediately.")] + [AddComponentMenu("")] + public class While : If + { + public override void OnEnter() + { + bool execute = true; + if (variable != null) + { + execute = EvaluateCondition(); + } + + // Find next End statement at same indent level + End endCommand = null; + for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i) + { + End command = parentSequence.commandList[i] as End; + + if (command != null && + command.indentLevel == indentLevel) + { + endCommand = command; + } + } + + if (execute) + { + // Tell the following end command to loop back + endCommand.loop = true; + Continue(); + } + else + { + // Continue at next command after End + Continue (endCommand.commandIndex + 1); + } + } + + public override bool OpenBlock() + { + 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/While.cs.meta b/Assets/Fungus/FungusScript/Scripts/Commands/While.cs.meta new file mode 100644 index 00000000..4ef44e4b --- /dev/null +++ b/Assets/Fungus/FungusScript/Scripts/Commands/While.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 663c8a7831a104d16ad7078a4dc2bd10 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: