Browse Source

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

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

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

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

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

@ -142,7 +142,7 @@ namespace Fungus
return executionCount; return executionCount;
} }
public virtual bool Execute() public virtual bool Execute(Action onComplete = null)
{ {
if (executionState != ExecutionState.Idle) if (executionState != ExecutionState.Idle)
{ {
@ -150,12 +150,12 @@ namespace Fungus
} }
executionCount++; executionCount++;
StartCoroutine(ExecuteBlock()); StartCoroutine(ExecuteBlock(onComplete));
return true; return true;
} }
protected virtual IEnumerator ExecuteBlock() protected virtual IEnumerator ExecuteBlock(Action onComplete = null)
{ {
Flowchart flowchart = GetFlowchart(); Flowchart flowchart = GetFlowchart();
executionState = ExecutionState.Executing; executionState = ExecutionState.Executing;
@ -242,6 +242,11 @@ namespace Fungus
executionState = ExecutionState.Idle; executionState = ExecutionState.Idle;
activeCommand = null; activeCommand = null;
if (onComplete != null)
{
onComplete();
}
} }
public virtual void Stop() 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. * Called when the new command is added to a block in the editor.
*/ */

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

@ -2,6 +2,7 @@ using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System;
namespace Fungus namespace Fungus
{ {
@ -12,25 +13,64 @@ namespace Fungus
[AddComponentMenu("")] [AddComponentMenu("")]
public class Call : Command 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")] [FormerlySerializedAs("targetSequence")]
[Tooltip("Block to start executing")] [Tooltip("Block to start executing")]
public Block targetBlock; public Block targetBlock;
[Tooltip("Stop executing the parent block that contains this command")] public enum CallMode
public bool stopParentBlock = true; {
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() public override void OnEnter()
{ {
Flowchart flowchart = GetFlowchart();
if (targetBlock != null) if (targetBlock != null)
{ {
ExecuteBlock(targetBlock, stopParentBlock); // Callback action for Wait Until Finished mode
if (!stopParentBlock) 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
{ {
Continue(); // Execute block in another Flowchart
targetFlowchart.ExecuteBlock(targetBlock, onComplete);
} }
} }
else
{ if (callMode == CallMode.Stop)
{
Stop();
}
else if (callMode == CallMode.Continue)
{
Continue(); Continue();
} }
} }
@ -45,12 +85,31 @@ namespace Fungus
public override string GetSummary() public override string GetSummary()
{ {
string summary = "";
if (targetBlock == null) 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() 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. * The block must be in an idle state to be executed.
* Returns true if the Block started execution. * 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 // Block must be a component of the Flowchart game object
if (block == null || if (block == null ||
@ -242,7 +242,7 @@ namespace Fungus
} }
// Execute the first command in the command list // Execute the first command in the command list
block.Execute(); block.Execute(onComplete);
return true; return true;
} }

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

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

Loading…
Cancel
Save