root/src/BRAINSFramework/Behaviors/PathFinding/CyclicPathBehavior.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


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

namespace Brains.Framework.Behaviors.PathFinding
{
    /// <summary>
    /// This behaviour allows to define a list of point which should be visited in
    /// a sequential fashion. When last point is reached, the whole behaviour starts
    /// over, going to point #1 and repeating sequence.
    /// 
    /// How it works:
    /// A list of AIBehaviourGoto instances is being build and added to local 
    /// subbehaviours container.
    /// </summary>
    public class CyclicPathBehavior : CompositeBehavior,IActionBehavior
    {

        protected List<GridCell> nodesToVisit;
        protected bool initialized;
        protected int sequenceCounter;
        public List<GridCell> CellsToVisit { get { return nodesToVisit; } }
        public CyclicPathBehavior()
        {
            initialized = false;
            nodesToVisit = new List<GridCell>();

        }

        ~CyclicPathBehavior()
        {
        }

        public void AddPoint(GridCell intermediateNode)
        {
            nodesToVisit.Add(intermediateNode);
        }

        public override void Reset()
        {
            sequenceCounter = 0;
            this.State = BehaviorState.Idle;
            base.Reset();
        }
        public override void OnSuccess()
        {
            base.OnSuccess();
            
            sequenceCounter++;
            if (sequenceCounter > nodesToVisit.Count - 1)
            {
                this.Reset();
            }
            else
            {
                // stop actor for a moment, until we get new desired direction
                this.Owner.DesiredOrientation = this.Owner.DesiredOrientation;
            }
        }
        public override void OnSubBehaviorSuccess()
        {
            SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
            CurrentSubBehavior++;
            if (CurrentSubBehavior >= nodesToVisit.Count)
            {
                this.Reset();
            }
            
        }
        public override void Update(GameTime gameTime)
        {
            if (!initialized)
            {
                SubBehaviors = new BehaviorList<IBehavior>();
                if (Owner != null)
                {
                    // 1 add goto behaviour from character position to first node
                    CompositeBehavior newBehaviour = new GoToBehavior();
                    newBehaviour.SetOwner(this.Owner);
                    //newBehaviour.gr= this.Map;

                    // loop
                    for (int index = 0; index < nodesToVisit.Count - 1; index++)
                    {
                        CompositeBehavior newIBehaviour = new GoToBehavior();
                        newIBehaviour.SetOwner(this.Owner);
                        ((GoToBehavior)(newIBehaviour)).StartNode = nodesToVisit[index];
                        ((GoToBehavior)(newIBehaviour)).EndNode = nodesToVisit[index + 1];
                        SubBehaviors.Add(newIBehaviour);

                    }

                    newBehaviour = new GoToBehavior();
                    newBehaviour.SetOwner(Owner);
                    ((GoToBehavior)(newBehaviour)).StartNode = nodesToVisit[nodesToVisit.Count - 1];
                    ((GoToBehavior)(newBehaviour)).EndNode = nodesToVisit[0];
                    SubBehaviors.Add(newBehaviour);

                    // add goto behaviour from last position to first position
                }
                initialized = true;
            }
            base.Update(gameTime);
        }

    }
}