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()
{
Sequence sequence = GetSequence();
if (sequence == null)
{
return;
}
// Find the next EndIf command at the same indent level as this Else command
bool foundThisCommand = false;
int indent = indentLevel;
foreach(Command command in parentSequence.commandList)
foreach(Command command in sequence.commandList)
{
if (foundThisCommand &&
command.indentLevel == indent)

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

@ -33,6 +33,12 @@ namespace Fungus
public override void OnEnter()
{
Sequence sequence = GetSequence();
if (sequence == null)
{
return;
}
bool condition = false;
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
bool foundThisCommand = false;
int indent = indentLevel;
foreach(Command command in parentSequence.commandList)
foreach(Command command in sequence.commandList)
{
if (foundThisCommand &&
command.indentLevel == indent)

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

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

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

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

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

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

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

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

Loading…
Cancel
Save