root/src/BRAINSFramework/Behaviors/SequenceBehavior.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 1.79 KB

(July 05, 2009 18:01 UTC) Almost 3 years ago

Behavior Refactoring

 
Show/hide line numbers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brains.Framework.Designer;

namespace Brains.Framework.Behaviors
{
    /// <summary>
    /// The sequence behavior runs a sub behavior until it is complete, then moves onto the next sub behavior.
    /// </summary>
    [BehaviorAttribute("Sequence")]
    public class SequenceBehavior : CompositeBehavior, ICompositeBehavior
    {
      
        public SequenceBehavior()
        {
            
        }
        
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
        
        public override void OnSuccess()
        {
            Reset();
        }

        public override void OnFailure()
        {
            Reset();
        }

        /// <summary>
        /// If the child behavior has failed cleanly we mark the behavior as a failure too.
        /// </summary>
        public override void OnSubBehaviorFailure()
        {
            //If we fail one, we fail them all
            State = BehaviorState.Failed;
        }

        /// <summary>
        /// If a child behavior succeeds, we continue execution to the next behavior
        /// </summary>
        public override void OnSubBehaviorSuccess()
        {
            //Reset the current sub behavior to idle
            SubBehaviors[CurrentSubBehavior].Reset();

            //If it's the last behavior the sequence is a success
            if (CurrentSubBehavior == Count - 1)
            {
                State = BehaviorState.Success;
            }
            else //Move onto the next item in the sequence.
            {   
                CurrentSubBehavior++;
            }

        }
    }
}