You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.4 KiB
71 lines
2.4 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
10 years ago
|
using UnityEngine;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Force a loop to terminate immediately.
|
||
|
/// </summary>
|
||
8 years ago
|
[CommandInfo("Flow",
|
||
|
"Break",
|
||
|
"Force a loop to terminate immediately.")]
|
||
|
[AddComponentMenu("")]
|
||
|
public class Break : Command
|
||
|
{
|
||
|
public override void OnEnter()
|
||
|
{
|
||
|
// Find index of previous while command
|
||
|
int whileIndex = -1;
|
||
|
int whileIndentLevel = -1;
|
||
8 years ago
|
for (int i = CommandIndex - 1; i >=0; --i)
|
||
8 years ago
|
{
|
||
8 years ago
|
While whileCommand = ParentBlock.CommandList[i] as While;
|
||
8 years ago
|
if (whileCommand != null)
|
||
|
{
|
||
|
whileIndex = i;
|
||
8 years ago
|
whileIndentLevel = whileCommand.IndentLevel;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if (whileIndex == -1)
|
||
|
{
|
||
|
// No enclosing While command found, just continue
|
||
|
Continue();
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// Find matching End statement at same indent level as While
|
||
8 years ago
|
for (int i = whileIndex + 1; i < ParentBlock.CommandList.Count; ++i)
|
||
8 years ago
|
{
|
||
8 years ago
|
End endCommand = ParentBlock.CommandList[i] as End;
|
||
8 years ago
|
|
||
|
if (endCommand != null &&
|
||
8 years ago
|
endCommand.IndentLevel == whileIndentLevel)
|
||
8 years ago
|
{
|
||
|
// Sanity check that break command is actually between the While and End commands
|
||
8 years ago
|
if (CommandIndex > whileIndex && CommandIndex < endCommand.CommandIndex)
|
||
8 years ago
|
{
|
||
|
// Continue at next command after End
|
||
8 years ago
|
Continue (endCommand.CommandIndex + 1);
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// No matching End command found so just continue
|
||
|
Continue();
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
public override Color GetButtonColor()
|
||
|
{
|
||
|
return new Color32(253, 253, 150, 255);
|
||
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|