コード例 #1
0
    private void ExecuteNextChild()
    {
        ExecuteNextChildCallDepth++;
        if (ExecuteNextChildCallDepth > ChildBehaviors.Count)
        {
            StopSelf();
            return;     // This can happen if all children are disabled. Don't want an infinite loop.
        }

        if (CurrentBehaviorIndex < ChildBehaviors.Count)
        {
            CurrentBehaviorIndex++;
        }
        else
        {
            if (FinishingAtomicExecution)
            {
                AtomicExecutionFinished = true;
            }
            else
            {
                if (Loop && (CurrentLoopCount < LoopCount - 1 || LoopCount < 0))
                {
                    // This is a looping behavior. Roll around to the first behavior.
                    CurrentBehaviorIndex = 0;
                    CurrentLoopCount++;
                }
                else
                {
                    // This is not a looping behavior. End after the last behavior has ended.
                    StopSelf();
                    ExecuteNextChildCallDepth--;
                    return;
                }
            }
        }

        if (CurrentBehaviorIndex >= 0 && CurrentBehaviorIndex < ChildBehaviors.Count)
        {
            CurrentBehavior = ChildBehaviors[CurrentBehaviorIndex];
            if (CurrentBehavior != null)
            {
                if (CurrentBehavior.Disabled /*|| CurrentBehavior is Condition*/)
                {
                    ExecuteNextChild();
                }
                else
                {
                    CurrentBehavior.Begin();
                }
            }
        }

        ExecuteNextChildCallDepth--;
    }