Browse Source

Refactored GetFungusScript() and GetSequence()

master
chrisgregan 11 years ago
parent
commit
e968bfd507
  1. BIN
      Assets/Example/Scenes/Example.unity
  2. 8
      Assets/Fungus/FungusScript/Commands/Else.cs
  3. 8
      Assets/Fungus/FungusScript/Commands/If.cs
  4. 9
      Assets/Fungus/FungusScript/Editor/CommandEditor.cs
  5. 15
      Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs
  6. 58
      Assets/Fungus/FungusScript/Scripts/Command.cs
  7. 38
      Assets/Fungus/FungusScript/Scripts/Sequence.cs

BIN
Assets/Example/Scenes/Example.unity

Binary file not shown.

8
Assets/Fungus/FungusScript/Commands/Else.cs

@ -11,10 +11,16 @@ namespace Fungus
{ {
public override void OnEnter() public override void OnEnter()
{ {
Sequence sequence = GetSequence();
if (sequence == null)
{
return;
}
// Find the next EndIf command at the same indent level as this Else command // Find the next EndIf command at the same indent level as this Else command
bool foundThisCommand = false; bool foundThisCommand = false;
int indent = indentLevel; int indent = indentLevel;
foreach(Command command in parentSequence.commandList) foreach(Command command in sequence.commandList)
{ {
if (foundThisCommand && if (foundThisCommand &&
command.indentLevel == indent) command.indentLevel == indent)

8
Assets/Fungus/FungusScript/Commands/If.cs

@ -33,6 +33,12 @@ namespace Fungus
public override void OnEnter() public override void OnEnter()
{ {
Sequence sequence = GetSequence();
if (sequence == null)
{
return;
}
bool condition = false; bool condition = false;
if (variable == null) if (variable == null)
@ -137,7 +143,7 @@ namespace Fungus
// Find the next Else or EndIf command at the same indent level as this If command // Find the next Else or EndIf command at the same indent level as this If command
bool foundThisCommand = false; bool foundThisCommand = false;
int indent = indentLevel; int indent = indentLevel;
foreach(Command command in parentSequence.commandList) foreach(Command command in sequence.commandList)
{ {
if (foundThisCommand && if (foundThisCommand &&
command.indentLevel == indent) command.indentLevel == indent)

9
Assets/Fungus/FungusScript/Editor/CommandEditor.cs

@ -125,6 +125,11 @@ namespace Fungus
static public Command PasteCommand(Command copyCommand, Sequence sequence) static public Command PasteCommand(Command copyCommand, Sequence sequence)
{ {
if (sequence == null)
{
return null;
}
System.Type type = copyCommand.GetType(); System.Type type = copyCommand.GetType();
Component copy = Undo.AddComponent(sequence.gameObject, type); Component copy = Undo.AddComponent(sequence.gameObject, type);
System.Reflection.FieldInfo[] fields = type.GetFields(); System.Reflection.FieldInfo[] fields = type.GetFields();
@ -133,9 +138,7 @@ namespace Fungus
field.SetValue(copy, field.GetValue(copyCommand)); field.SetValue(copy, field.GetValue(copyCommand));
} }
FungusScript fungusScript = sequence.GetFungusScript(); Undo.RecordObject(sequence, "Paste Command");
Undo.RecordObject(fungusScript, "Paste Command");
Command newCommand = copy as Command; Command newCommand = copy as Command;
sequence.commandList.Add(newCommand); sequence.commandList.Add(newCommand);

15
Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs

@ -286,11 +286,13 @@ namespace Fungus
if (s != null) if (s != null)
{ {
FungusScript fungusScript = s.GetFungusScript(); FungusScript fungusScript = s.GetFungusScript();
fungusScript.selectedSequence = s; if (fungusScript != null)
fungusScript.selectedCommand = null; {
fungusScript.selectedSequence = s;
Selection.activeGameObject = fungusScript.gameObject; fungusScript.selectedCommand = null;
GUIUtility.keyboardControl = 0; // Fix for textarea not refeshing (change focus) Selection.activeGameObject = fungusScript.gameObject;
GUIUtility.keyboardControl = 0; // Fix for textarea not refeshing (change focus)
}
} }
} }
} }
@ -320,8 +322,7 @@ namespace Fungus
bool sequenceIsSelected = (fungusScript.selectedSequence == sequence); bool sequenceIsSelected = (fungusScript.selectedSequence == sequence);
Command[] commands = sequence.GetComponentsInChildren<Command>(); foreach (Command command in sequence.commandList)
foreach (Command command in commands)
{ {
bool commandIsSelected = (fungusScript.selectedCommand == command); bool commandIsSelected = (fungusScript.selectedCommand == command);

58
Assets/Fungus/FungusScript/Scripts/Command.cs

@ -26,49 +26,35 @@ namespace Fungus
[RequireComponent(typeof(Sequence))] [RequireComponent(typeof(Sequence))]
public class Command : MonoBehaviour public class Command : MonoBehaviour
{ {
[HideInInspector]
public string errorMessage = ""; public string errorMessage = "";
[HideInInspector]
public FungusScript parentFungusScript;
[HideInInspector]
public Sequence parentSequence;
[HideInInspector]
public int indentLevel; public int indentLevel;
public virtual void Start() public Sequence GetSequence()
{ {
parentSequence = GetComponent<Sequence>(); return gameObject.GetComponent<Sequence>();
parentFungusScript = GetFungusScript();
} }
public FungusScript GetFungusScript() public FungusScript GetFungusScript()
{ {
FungusScript sc = null; Sequence s = GetSequence();
if (s == null)
Transform parent = transform.parent;
while (parent != null)
{ {
sc = parent.gameObject.GetComponent<FungusScript>(); return null;
if (sc != null)
{
break;
}
parent = parent.transform.parent;
} }
return sc;
return s.GetFungusScript();
} }
public bool IsExecuting() public bool IsExecuting()
{ {
if (parentSequence == null) Sequence sequence = GetSequence();
if (sequence == null)
{ {
return false; return false;
} }
return (parentSequence.activeCommand == this); return (sequence.activeCommand == this);
} }
public virtual void Execute() public virtual void Execute()
@ -84,20 +70,36 @@ namespace Fungus
public virtual void Continue(Command currentCommand) public virtual void Continue(Command currentCommand)
{ {
OnExit(); OnExit();
parentSequence.ExecuteNextCommand(currentCommand); Sequence sequence = GetSequence();
if (sequence != null)
{
sequence.ExecuteNextCommand(currentCommand);
}
} }
public virtual void Stop() public virtual void Stop()
{ {
OnExit(); OnExit();
parentSequence.Stop(); Sequence sequence = GetSequence();
if (sequence != null)
{
sequence.Stop();
}
} }
public virtual void ExecuteSequence(Sequence s) public virtual void ExecuteSequence(Sequence s)
{ {
OnExit(); OnExit();
parentSequence.Stop(); Sequence sequence = GetSequence();
parentFungusScript.ExecuteSequence(s); if (sequence != null)
{
sequence.Stop();
FungusScript fungusScript = sequence.GetFungusScript();
if (fungusScript != null)
{
fungusScript.ExecuteSequence(s);
}
}
} }
public virtual void OnEnter() public virtual void OnEnter()

38
Assets/Fungus/FungusScript/Scripts/Sequence.cs

@ -12,18 +12,12 @@ namespace Fungus
[ExecuteInEditMode] [ExecuteInEditMode]
public class Sequence : MonoBehaviour public class Sequence : MonoBehaviour
{ {
[HideInInspector]
public Rect nodeRect = new Rect(10, 10, 100, 40); public Rect nodeRect = new Rect(10, 10, 100, 40);
[HideInInspector]
public string sequenceName = "Sequence"; public string sequenceName = "Sequence";
[HideInInspector]
public string description = ""; public string description = "";
[System.NonSerialized]
public FungusScript fungusScript;
[System.NonSerialized] [System.NonSerialized]
public Command activeCommand; public Command activeCommand;
@ -31,25 +25,9 @@ namespace Fungus
int executionCount; int executionCount;
public virtual void Start()
{
fungusScript = GetFungusScript();
}
public FungusScript GetFungusScript() public FungusScript GetFungusScript()
{ {
FungusScript sc = null; return GetComponentInParent<FungusScript>();
Transform parent = transform.parent;
while (parent != null)
{
sc = parent.gameObject.GetComponent<FungusScript>();
if (sc != null)
{
break;
}
}
return sc;
} }
public bool HasError() public bool HasError()
@ -67,6 +45,8 @@ namespace Fungus
public bool IsRunning() public bool IsRunning()
{ {
FungusScript fungusScript = GetFungusScript();
if (fungusScript == null || if (fungusScript == null ||
fungusScript.executingSequence == null) fungusScript.executingSequence == null)
{ {
@ -114,14 +94,16 @@ namespace Fungus
} }
else else
{ {
if (GetFungusScript().stepTime == 0f) FungusScript fungusScript = GetFungusScript();
if (fungusScript.stepTime == 0f)
{ {
activeCommand = nextCommand; activeCommand = nextCommand;
nextCommand.Execute(); nextCommand.Execute();
} }
else else
{ {
StartCoroutine(ExecuteAfterDelay(nextCommand, GetFungusScript().stepTime)); StartCoroutine(ExecuteAfterDelay(nextCommand, fungusScript.stepTime));
} }
} }
@ -136,6 +118,12 @@ namespace Fungus
public void Stop() public void Stop()
{ {
FungusScript fungusScript = GetFungusScript();
if (fungusScript == null)
{
return;
}
activeCommand = null; activeCommand = null;
fungusScript.executingSequence = null; fungusScript.executingSequence = null;
fungusScript.selectedSequence = null; fungusScript.selectedSequence = null;

Loading…
Cancel
Save