root/src/BRAINSFramework/Behaviors/RandomBehavior.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 1.51 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>
    /// This Behavior is a Composite behavior which will randomly select a sub behavior to run.
    /// </summary>
    [BehaviorAttribute("Random")]
    public class RandomBehavior : CompositeBehavior, ICompositeBehavior
    {

        public RandomBehavior()
        {

        }
        
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
        
        public override void OnSuccess()
        {
            CurrentSubBehavior = 0;
            base.OnSuccess();
        }

        public override void OnFailure()
        {
            SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
            CurrentSubBehavior = 0;
            base.OnFailure();
        }

        public override void OnSubBehaviorFailure()
        {
            OnFailure();
            base.OnSubBehaviorFailure();
        }

        public override void OnSubBehaviorSuccess()
        {
            SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;

            if (CurrentSubBehavior == Count - 1) //Is it the final behavior?
                OnSuccess();
            else //Move onto the next item in the sequence.
                CurrentSubBehavior++;

            base.OnSubBehaviorSuccess();
        }
    }
}