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

1322
9
9
10
namespace Brains.Framework.Behaviors.PathFinding
10
namespace Brains.Framework.Behaviors.PathFinding
11
{
11
{
12
    public class FollowPathBehavior:BehaviorBase,IActionBehavior
12
    public class FollowPathBehavior:CompositeBehavior,IActionBehavior
13
    {
13
    {
14
        
14
        protected PathFinder pathFinder;
15
        protected PathFinder pathFinder;
15
        protected int currentNode = 0;
16
        protected int currentNode = 0;
16
        protected bool copiedPath = false;
17
        protected bool copiedPath = false;
...
...
173
                                desiredDirection.Normalize();
174
                                desiredDirection.Normalize();
174
                                Owner.DesiredOrientation = desiredDirection;
175
                                Owner.DesiredOrientation = desiredDirection;
175
                                Owner.DesiredPosition = pathNodes[currentNode].nodePosition;
176
                                Owner.DesiredPosition = pathNodes[currentNode].nodePosition;
176
177
                                if (Owner.Locomotion is Locomotion.LocomtionSteering)
178
                                    ((Locomotion.LocomtionSteering)Owner.Locomotion).TurnOnSeek(Owner.DesiredPosition);
177
                            }
179
                            }
178
                            else
180
                            else
179
                            {
181
                            {
...
...
247
                        }
249
                        }
248
                        else
250
                        else
249
                        {
251
                        {
252
250
                            // copying nodes
253
                            // copying nodes
254
                            //if(!StartAtClosetCell)  
251
                            pathNodes = new FollowPathNode[pathFinder.closeList.Count];
255
                            pathNodes = new FollowPathNode[pathFinder.closeList.Count];
252
                            int counter = 0;
256
                            int counter = 0;
253
                            //for (int index = pathFinder.closeList.Count - 1; index >= 0; index--)
257
                            //for (int index = pathFinder.closeList.Count - 1; index >= 0; index--)
254
                            for (int index = 0; index <= pathFinder.closeList.Count - 1; index++)
258
                            for (int index = 0; index <= pathFinder.closeList.Count - 1; index++)
255
                            {
259
                            {
260
                                
256
                                FollowPathNode newNode = new FollowPathNode();
261
                                FollowPathNode newNode = new FollowPathNode();
257
                                Grid grid=PathFinder.Grid;
262
                                Grid grid=PathFinder.Grid;
258
                                GridCell _node =grid.GetCell(pathFinder.closeList[index].X,
263
                                GridCell _node =grid.GetCell(pathFinder.closeList[index].X,
...
...
268
                                counter++;
273
                                counter++;
269
                            }
274
                            }
270
275
276
                            
271
                            copiedPath = true;
277
                            copiedPath = true;
272
278
273
                        }
279
                        }
...
...
288
            base.Update(gameTime);
294
            base.Update(gameTime);
289
        }
295
        }
290
296
297
        private bool NodeMatch(FollowPathNode item, GridCell _aa)
298
        {
299
            if (item.X == _aa.X && item.Y == _aa.Y)
300
            {
301
                return true;
302
            }
303
            return false;
304
        }
305
291
    }
306
    }
292
    public struct FollowPathNode
307
    public struct FollowPathNode
293
    {
308
    {
...
...
300
        public int X;
315
        public int X;
301
        public int Y;
316
        public int Y;
302
    }
317
    }
318
319
    public class PreStoredPath
320
    {
321
        public int FromGrid { get; set; }
322
        public int ToGrid{ get; set; }
323
        public List<StoredPathNode> Nodes{ get; set; }
324
    }
325
    public struct StoredPathNode
326
    {
327
        public Vector2 nodePosition;
328
        public Vector2 nodeDirection;
329
        public Vector2 biNormalStart;
330
        public Vector2 biNormalEnd;
331
        public bool biNormalGenerated;
332
        public bool nodeVisited;
333
        public int X;
334
        public int Y;
335
        
336
    }
303
}
337
}