Browse Source

While loops and Break command #81

master
chrisgregan 10 years ago
parent
commit
419d56ae29
  1. 40
      Assets/Fungus/FungusScript/Scripts/Commands/Break.cs
  2. 8
      Assets/Fungus/FungusScript/Scripts/Commands/Break.cs.meta
  3. 17
      Assets/Fungus/FungusScript/Scripts/Commands/End.cs
  4. 59
      Assets/Fungus/FungusScript/Scripts/Commands/While.cs
  5. 8
      Assets/Fungus/FungusScript/Scripts/Commands/While.cs.meta

40
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);
}
}
}

8
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:

17
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();
}

59
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);
}
}
}

8
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:
Loading…
Cancel
Save