Пример #1
0
 public void initializeMap()
 {
     Map = new Node[levelSize.I, levelSize.J];
     for (int i = 0; i < levelSize.I; i++)
     {
         for (int j = 0; j < levelSize.J; j++)
         {
             Map[i, j] = new Node(informations.EndLocationIndex, new IndexPair(i, j));
         }
     }
 }
Пример #2
0
        private List<Node> GetAdjacentWalkableNodes(Node fromNode)
        {
            List<Node> walkableNodes = new List<Node>();
            IEnumerable<IndexPair> nextLocations = GetAdjacentLocations(fromNode.Location);
            foreach (var location in nextLocations)
            {
                int x = location.I;
                int y = location.J;

                // Stay within the grid's boundaries
                if (x < 0 || x >= levelSize.I || y < 0 || y >= levelSize.J)
                    continue;

                Node node = Map[x, y];
                // Ignore non-walkable nodes
                if (!node.walkable)
                    continue;

                // Ignore already-closed nodes
                if (node.State == NodeState.Closed)
                    continue;

                // Already-open nodes are only added to the list if their G-value is lower going via this route.
                if (node.State == NodeState.Open)
                {
                    float traversalCost = Node.GetTraversalCost(node.Location, node.ParentNode.Location);
                    float gTemp = fromNode.G + traversalCost;
                    if (gTemp < node.G)
                    {
                        node.ParentNode = fromNode;
                        walkableNodes.Add(node);
                    }
                }
                else
                {
                    // If it's untested, set the parent and flag it as 'Open' for consideration
                    node.ParentNode = fromNode;
                    node.State = NodeState.Open;
                    walkableNodes.Add(node);
                }
            }
            return walkableNodes;
        }
Пример #3
0
 public PathFinder(RouteInformation informations)
 {
     levelSize = MapLoader.LevelDimensions;
     this.informations = informations;
     initializeMap();
     startNode = Map[informations.StartLocationIndex.I, informations.StartLocationIndex.J];
     startNode.State = NodeState.Open;
     endNode = Map[informations.EndLocationIndex.I, informations.EndLocationIndex.J];
 }
Пример #4
0
        /// <summary>
        /// returns true if(the path leads to the destination and false if the path leads to dead end
        /// </summary>
        /// <param name="currentNode"></param>
        /// <returns></returns>
        private bool Search(Node currentNode)
        {
            // Set the current node to Closed since it cannot be traversed more than once
            currentNode.State = NodeState.Closed;
            List<Node> nextNodes = GetAdjacentWalkableNodes(currentNode);

            // Sort by F-value so that the shortest possible routes are considered first
            nextNodes.Sort((node1, node2) => node1.F.CompareTo(node2.F));
            foreach (var nextNode in nextNodes)
            {
                // Check whether the end node has been reached
                if (nextNode.Location == this.endNode.Location)
                {
                    return true;
                }
                else
                {
                    // If not, check the next set of nodes
                    if (Search(nextNode)) // Note: Recurses back into Search(Node)
                        return true;
                }
            }

            // The method returns false if this path leads to be a dead end
            return false;
        }