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

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

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

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

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

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

@ -34,7 +34,7 @@ namespace Fungus
return instance;
}
protected virtual void Start()
protected override void Start()
{
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
if (sequence == null ||
sequence.transform.parent != transform)
sequence.transform.parent != transform && sequence.transform != transform)
{
return;
}

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

@ -20,6 +20,15 @@ namespace Fungus
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()
{
FungusScript fungusScript = GetComponent<FungusScript>();

Loading…
Cancel
Save