Browse Source

Fix #46 - commands now store reference to their parent sequence

The reference to the parent sequence is only populate at runtime, so
don’t try to use it in custom editors, etc.
master
chrisgregan 10 years ago
parent
commit
4401060c61
  1. 5
      Assets/Fungus/FungusScript/Commands/Else.cs
  2. 5
      Assets/Fungus/FungusScript/Commands/If.cs
  3. 42
      Assets/Fungus/FungusScript/Scripts/Command.cs
  4. 2
      Assets/Fungus/FungusScript/Scripts/CommandCopyBuffer.cs
  5. 2
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  6. 9
      Assets/Fungus/FungusScript/Scripts/Sequence.cs

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

@ -11,8 +11,7 @@ namespace Fungus
{ {
public override void OnEnter() public override void OnEnter()
{ {
Sequence sequence = GetSequence(); if (parentSequence == null)
if (sequence == null)
{ {
return; return;
} }
@ -20,7 +19,7 @@ namespace Fungus
// 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 sequence.commandList) foreach(Command command in parentSequence.commandList)
{ {
if (foundThisCommand && if (foundThisCommand &&
command.indentLevel == indent) command.indentLevel == indent)

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

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

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

@ -31,31 +31,32 @@ namespace Fungus
[HideInInspector] [HideInInspector]
public bool selected; public bool selected;
public virtual Sequence GetSequence() /**
{ * Reference to the Sequence object that this command belongs to.
return gameObject.GetComponent<Sequence>(); * This reference is only set at runtime (null in editor).
} */
[NonSerialized]
public Sequence parentSequence;
public virtual FungusScript GetFungusScript() public virtual FungusScript GetFungusScript()
{ {
Sequence s = GetSequence(); FungusScript fungusScript = GetComponent<FungusScript>();
if (s == null) if (fungusScript == null &&
transform.parent != null)
{ {
return null; fungusScript = transform.parent.GetComponent<FungusScript>();
} }
return fungusScript;
return s.GetFungusScript();
} }
public virtual bool IsExecuting() public virtual bool IsExecuting()
{ {
Sequence sequence = GetSequence(); if (parentSequence == null)
if (sequence == null)
{ {
return false; return false;
} }
return (sequence.activeCommand == this); return (parentSequence.activeCommand == this);
} }
public virtual void Execute() public virtual void Execute()
@ -71,31 +72,28 @@ namespace Fungus
public virtual void Continue(Command currentCommand) public virtual void Continue(Command currentCommand)
{ {
OnExit(); OnExit();
Sequence sequence = GetSequence(); if (parentSequence != null)
if (sequence != null)
{ {
sequence.ExecuteNextCommand(currentCommand); parentSequence.ExecuteNextCommand(currentCommand);
} }
} }
public virtual void Stop() public virtual void Stop()
{ {
OnExit(); OnExit();
Sequence sequence = GetSequence(); if (parentSequence != null)
if (sequence != null)
{ {
sequence.Stop(); parentSequence.Stop();
} }
} }
public virtual void ExecuteSequence(Sequence s) public virtual void ExecuteSequence(Sequence s)
{ {
OnExit(); OnExit();
Sequence sequence = GetSequence(); if (parentSequence != null)
if (sequence != null)
{ {
sequence.Stop(); parentSequence.Stop();
FungusScript fungusScript = sequence.GetFungusScript(); FungusScript fungusScript = parentSequence.GetFungusScript();
if (fungusScript != null) if (fungusScript != null)
{ {
fungusScript.ExecuteSequence(s); fungusScript.ExecuteSequence(s);

2
Assets/Fungus/FungusScript/Scripts/CommandCopyBuffer.cs

@ -34,7 +34,7 @@ namespace Fungus
return instance; return instance;
} }
protected virtual void Start() protected override void Start()
{ {
if (Application.isPlaying) if (Application.isPlaying)
{ {

2
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -148,7 +148,7 @@ namespace Fungus
{ {
// Sequence must be a child of the parent Fungus Script // Sequence must be a child of the parent Fungus Script
if (sequence == null || if (sequence == null ||
sequence.transform.parent != transform) sequence.transform.parent != transform && sequence.transform != transform)
{ {
return; return;
} }

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

@ -20,6 +20,15 @@ namespace Fungus
protected int executionCount; protected int executionCount;
protected virtual void Start()
{
// Give each child command a reference back to its parent sequence
foreach (Command command in commandList)
{
command.parentSequence = this;
}
}
public virtual FungusScript GetFungusScript() public virtual FungusScript GetFungusScript()
{ {
FungusScript fungusScript = GetComponent<FungusScript>(); FungusScript fungusScript = GetComponent<FungusScript>();

Loading…
Cancel
Save