root/src/BRAINSFramework/Behaviors/CompositeBehavior.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 4.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;

namespace Brains.Framework.Behaviors
{
    public class CompositeBehavior : IBehavior,ISubBehaviorHolder
    {
        private Agent _agent;
        private BehaviorList<IBehavior> _subBehaviors;
        private int _currentSubBehavior;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public CompositeBehavior()
        {
            _subBehaviors = new BehaviorList<IBehavior>();
            _currentSubBehavior = 0;
        }

        /// <summary>
        /// Gets the count of subbehaviors
        /// </summary>
        public int Count
        {
            get { return SubBehaviors.Count; }
        }

        /// <summary>
        /// Gets the Agent owner of the behavior
        /// </summary>
        public Agent Owner
        {
            get { return _agent; }
        }

        /// <summary>
        /// Gets or sets the subbehaviors list
        /// </summary>
        public BehaviorList<IBehavior> SubBehaviors
        {
            get
            {
                return _subBehaviors;
            }
            set
            {
                _subBehaviors = value;
            }
        }

        /// <summary>
        /// Gets or sets the index of the currently active sub behavior
        /// </summary>
        public int CurrentSubBehavior
        {
            get
            {
                return _currentSubBehavior;
            }
            set
            {
                if (_currentSubBehavior != value)
                {
                    _currentSubBehavior = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the current state of the behavior.
        /// </summary>
        /// <remarks>If the state is Idle and the curent index changes to this behavior, State will automatically get set to Running</remarks>
        public BehaviorState State { get; set; }

        /// <summary>
        /// Sets the Agent owner of the Behavior
        /// </summary>
        /// <param name="actor">The agent</param>
        /// <remarks>This method sets the agent of all subbehaviors if exist</remarks>
        public virtual void SetOwner(Agent actor)
        {
            _agent = actor;
            _subBehaviors.Owner = this;
        }
        
        /// <summary>
        /// Updates the currently active sub behavior of this behavior.
        /// </summary>
        /// <param name="gameTime"></param>
        /// <remarks>If the sub behavior is Idle, it get sets to Running. 
        /// If the sub behavior is Success or Failed it calls the overridable methods OnSubBehaviorSuccess and OnSubBehaviorFailure.</remarks>
        public virtual void Update(GameTime gameTime)
        {
            if (_subBehaviors != null && Count > 0)
            {
                UpdateCurrentSubBehavior(gameTime);
            }
        }
        
        private void UpdateCurrentSubBehavior(GameTime gameTime)
        {
            IBehavior _behavior = _subBehaviors[_currentSubBehavior];

            if (_behavior.State == BehaviorState.Idle || _behavior.State == BehaviorState.Running)
            {
                _behavior.State = BehaviorState.Running;
                _behavior.Update(gameTime);
            }
            else
            {
                //Check state of sub behavior after running it
                if (_behavior.State == BehaviorState.Success)
                    OnSubBehaviorSuccess();
                else if (_behavior.State == BehaviorState.Failed)
                    OnSubBehaviorFailure();
            }
        }

        public virtual void OnSuccess()
        {
        }

        public virtual void OnFailure()
        {

        }

        public virtual void OnSubBehaviorSuccess()
        {
        }

        public virtual void OnSubBehaviorFailure()
        {
        }

        public virtual void Reset()
        {
            State = BehaviorState.Idle;
            CurrentSubBehavior = 0;
            foreach (var item in SubBehaviors)
            {
                item.Reset();
            }
        }
        
        /// <summary>
        /// Gets the currently active sub behavior as an IBehavior
        /// </summary>
        public IBehavior CurrentBehavior
        {
            get { return SubBehaviors[CurrentSubBehavior]; }
        }

    }
}