Browse Source

Call a block in another Flowchart. Stop, Continue or Wait Until Finished.

master
chrisgregan 10 years ago
parent
commit
7a11cceaa3
  1. 25
      Assets/Fungus/Flowchart/Editor/CallEditor.cs
  2. 11
      Assets/Fungus/Flowchart/Scripts/Block.cs
  3. 27
      Assets/Fungus/Flowchart/Scripts/Command.cs
  4. 71
      Assets/Fungus/Flowchart/Scripts/Commands/Call.cs
  5. 6
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs
  6. 6
      Assets/Fungus/Narrative/Deprecated/Choose.cs

25
Assets/Fungus/Flowchart/Editor/CallEditor.cs

@ -8,13 +8,15 @@ namespace Fungus
[CustomEditor (typeof(Call))]
public class CallEditor : CommandEditor
{
protected SerializedProperty targetFlowchartProp;
protected SerializedProperty targetBlockProp;
protected SerializedProperty stopParentBlockProp;
protected SerializedProperty callModeProp;
protected virtual void OnEnable()
{
targetFlowchartProp = serializedObject.FindProperty("targetFlowchart");
targetBlockProp = serializedObject.FindProperty("targetBlock");
stopParentBlockProp = serializedObject.FindProperty("stopParentBlock");
callModeProp = serializedObject.FindProperty("callMode");
}
public override void DrawCommandGUI()
@ -23,18 +25,27 @@ namespace Fungus
Call t = target as Call;
Flowchart flowchart = t.GetFlowchart();
if (flowchart == null)
Flowchart flowchart = null;
if (targetFlowchartProp.objectReferenceValue == null)
{
return;
flowchart = t.GetFlowchart();
}
else
{
flowchart = targetFlowchartProp.objectReferenceValue as Flowchart;
}
EditorGUILayout.PropertyField(targetFlowchartProp);
if (flowchart != null)
{
BlockEditor.BlockField(targetBlockProp,
new GUIContent("Target Block", "Block to call"),
new GUIContent("<Continue>"),
new GUIContent("<None>"),
flowchart);
}
EditorGUILayout.PropertyField(stopParentBlockProp);
EditorGUILayout.PropertyField(callModeProp);
serializedObject.ApplyModifiedProperties();
}

11
Assets/Fungus/Flowchart/Scripts/Block.cs

@ -142,7 +142,7 @@ namespace Fungus
return executionCount;
}
public virtual bool Execute()
public virtual bool Execute(Action onComplete = null)
{
if (executionState != ExecutionState.Idle)
{
@ -150,12 +150,12 @@ namespace Fungus
}
executionCount++;
StartCoroutine(ExecuteBlock());
StartCoroutine(ExecuteBlock(onComplete));
return true;
}
protected virtual IEnumerator ExecuteBlock()
protected virtual IEnumerator ExecuteBlock(Action onComplete = null)
{
Flowchart flowchart = GetFlowchart();
executionState = ExecutionState.Executing;
@ -242,6 +242,11 @@ namespace Fungus
executionState = ExecutionState.Idle;
activeCommand = null;
if (onComplete != null)
{
onComplete();
}
}
public virtual void Stop()

27
Assets/Fungus/Flowchart/Scripts/Command.cs

@ -104,33 +104,6 @@ namespace Fungus
}
}
public virtual void ExecuteBlock(Block s, bool stopParentBlock)
{
OnExit();
if (parentBlock != null)
{
Flowchart flowchart = parentBlock.GetFlowchart();
if (flowchart == null)
{
return;
}
if (stopParentBlock)
{
parentBlock.Stop();
// If the executing block is currently selected then follow the execution
// onto the next block in the inspector.
if (flowchart.selectedBlock == parentBlock)
{
flowchart.selectedBlock = s;
}
}
flowchart.ExecuteBlock(s);
}
}
/**
* Called when the new command is added to a block in the editor.
*/

71
Assets/Fungus/Flowchart/Scripts/Commands/Call.cs

@ -2,6 +2,7 @@ using UnityEngine;
using UnityEngine.Serialization;
using System.Collections;
using System.Collections.Generic;
using System;
namespace Fungus
{
@ -12,24 +13,63 @@ namespace Fungus
[AddComponentMenu("")]
public class Call : Command
{
[Tooltip("Flowchart which contains the block to execute. If none is specified then the current Flowchart is used.")]
public Flowchart targetFlowchart;
[FormerlySerializedAs("targetSequence")]
[Tooltip("Block to start executing")]
public Block targetBlock;
[Tooltip("Stop executing the parent block that contains this command")]
public bool stopParentBlock = true;
public enum CallMode
{
Stop,
Continue,
WaitUntilFinished
}
[Tooltip("Select if the calling block should stop or continue executing commands, or wait until the called block finishes.")]
public CallMode callMode;
public override void OnEnter()
{
Flowchart flowchart = GetFlowchart();
if (targetBlock != null)
{
ExecuteBlock(targetBlock, stopParentBlock);
if (!stopParentBlock)
// Callback action for Wait Until Finished mode
Action onComplete = null;
if (callMode == CallMode.WaitUntilFinished)
{
onComplete = delegate {
flowchart.selectedBlock = parentBlock;
Continue();
};
}
if (targetFlowchart == null ||
targetFlowchart == GetFlowchart())
{
// If the executing block is currently selected then follow the execution
// onto the next block in the inspector.
if (flowchart.selectedBlock == parentBlock)
{
flowchart.selectedBlock = targetBlock;
}
targetBlock.Execute(onComplete);
}
else
{
// Execute block in another Flowchart
targetFlowchart.ExecuteBlock(targetBlock, onComplete);
}
}
if (callMode == CallMode.Stop)
{
Stop();
}
else if (callMode == CallMode.Continue)
{
Continue();
}
@ -45,12 +85,31 @@ namespace Fungus
public override string GetSummary()
{
string summary = "";
if (targetBlock == null)
{
return "<Continue>";
summary = "<None>";
}
else
{
summary = targetBlock.blockName;
}
switch (callMode)
{
case CallMode.Stop:
summary += " : Stop";
break;
case CallMode.Continue:
summary += " : Continue";
break;
case CallMode.WaitUntilFinished:
summary += " : Wait";
break;
}
return targetBlock.blockName;
return summary;
}
public override Color GetButtonColor()

6
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -222,11 +222,11 @@ namespace Fungus
}
/**
* Start running another Flowchart by executing a specific child block.
* Start executing a specific child block in the flowchart.
* The block must be in an idle state to be executed.
* Returns true if the Block started execution.
*/
public virtual bool ExecuteBlock(Block block)
public virtual bool ExecuteBlock(Block block, Action onComplete = null)
{
// Block must be a component of the Flowchart game object
if (block == null ||
@ -242,7 +242,7 @@ namespace Fungus
}
// Execute the first command in the command list
block.Execute();
block.Execute(onComplete);
return true;
}

6
Assets/Fungus/Narrative/Deprecated/Choose.cs

@ -100,7 +100,8 @@ namespace Fungus
}
else
{
ExecuteBlock(onSelectBlock, true);
Stop();
onSelectBlock.Execute();
}
};
@ -175,7 +176,8 @@ namespace Fungus
{
options.Clear();
showBasicGUI = false;
ExecuteBlock(option.targetBlock, true);
Stop();
option.targetBlock.Execute();
}
}

Loading…
Cancel
Save