Example #1
0
        public PathFinder(Tile[,] map, Vector2 startLocation, Vector2 endLocation)
        {
            InitializeNodes(Game1.map, endLocation);

            this.StartNode = this.Nodes[(int)startLocation.X, (int)startLocation.Y];
            this.StartNode.State = NodeState.Open;

            this.EndNode = this.Nodes[(int)endLocation.X, (int)endLocation.Y];
        }
Example #2
0
        private List<PathNode> GetAdjacentWalkableNodes(PathNode fromNode)
        {
            List<PathNode> walkableNodes = new List<PathNode>();
            IEnumerable<Vector2> nextLocations = GetAdjacentLocations(fromNode.Point);

            foreach (var location in nextLocations)
            {
                int x = (int) location.X;
                int y = (int) location.Y;

                if (x < 0 || x >= this.Width || y < 0 || y >= this.Height)
                    continue;

                PathNode node = this.Nodes[x, y];

                if (!node.IsWalkable)
                    continue;

                if (node.State == NodeState.Open)
                {
                    float transversalCost = PathNode.GetTransversalCost(node.Point, node.Parent.Point);

                    float gTemp = fromNode.G + transversalCost;

                    if (gTemp < node.G)
                    {
                        node.Parent = fromNode;
                        walkableNodes.Add(node);
                    }
                }
                else
                {
                    node.Parent = fromNode;
                    node.State = NodeState.Open;
                    walkableNodes.Add(node);
                }
            }

            return walkableNodes;
        }
Example #3
0
        private bool Search(PathNode currentNode)
        {
            currentNode.State = NodeState.Closed;
            List<PathNode> nextNodes = GetAdjacentWalkableNodes(currentNode);

            nextNodes.Sort((node1, node2) => node1.F.CompareTo(node2.F));
            foreach(var nextNode in nextNodes)
            {
                if (nextNode.Point == this.EndNode.Point)
                {
                    return true;
                }
                else
                {
                    if (Search(nextNode))
                        return true;
                }
            }

            return false;
        }