Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace PathFindingExample
  8. {
  9.     class PathFinding
  10.     {
  11.         public PathFinding()
  12.         {
  13.  
  14.         }
  15.  
  16.         public List<Node> findPath(Node A, Node B)
  17.         {
  18.             var myList = new List<Node>();
  19.             var searchedNodes = new List<Node>();
  20.             var nodePath = new Stack<Node>();
  21.  
  22.             bool foundPath = false;
  23.  
  24.             //Add the first node onto our track
  25.             nodePath.Push(A);
  26.             searchedNodes.Add(A);
  27.             while(!foundPath)
  28.             {
  29.                 //No path
  30.                 if (nodePath.Count() == 0)
  31.                 {
  32.                     Console.WriteLine("No path");
  33.                     return nodePath.ToList();
  34.                 }
  35.                 //Find a node we haven't touched yet that's attached to the stack
  36.                 //If we have none, remove it from the stack
  37.                 //If there's no nodes left, there is no path.
  38.  
  39.                 var possiblePaths = nodePath.Peek().neighbors.Where(a => !searchedNodes.Contains(a));
  40.                 if (possiblePaths.Count() > 0)
  41.                 {
  42.                     //Start with the closest to the goal
  43.                     var chosenPath = possiblePaths.OrderBy(x => Vector3.Distance(B.position, x.position)).First();
  44.                     nodePath.Push(chosenPath);
  45.                     searchedNodes.Add(chosenPath);
  46.                 }
  47.                 else
  48.                 {
  49.                     nodePath.Pop();
  50.                 }
  51.                 if (nodePath.Count == 0)
  52.                 {
  53.                     return nodePath.ToList();
  54.                 }
  55.  
  56.                 //We found our goal
  57.                 if (nodePath.Peek() == B)
  58.                 {
  59.                     return nodePath.ToList();
  60.                 }
  61.             }
  62.  
  63.  
  64.             return nodePath.ToList();
  65.         }
  66.     }
  67.  
  68.     class Node
  69.     {
  70.         public GameObject GO;
  71.         public Vector3 position;
  72.         public List<Node> neighbors;
  73.  
  74.         public static List<Node> allNodes;
  75.  
  76.  
  77.         public Node(Vector3 pos)
  78.         {
  79.             this.position = pos;
  80.             neighbors = new List<Node>();
  81.             if (allNodes == null)
  82.             {
  83.                 allNodes = new List<Node>();
  84.             }
  85.             allNodes.Add(this);
  86.         }
  87.         public void Connect(Node other)
  88.         {
  89.             if (other == this) { return; }
  90.             if (neighbors.Contains(other) == false) { neighbors.Add(other); }
  91.             if (other.neighbors.Contains(this) == false) { other.neighbors.Add(this); };
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement