Пример #1
0
    public List <Vector2> FindPath(Node start, Node end)
    {
        List <Node>    openSet   = new List <Node>();
        HashSet <Node> closedSet = new HashSet <Node>();

        openSet.Add(start);
        while (openSet.Count > 0)
        {
            Node currentNode = openSet[0];

            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].FCost < currentNode.FCost || (openSet[i].FCost == currentNode.FCost && openSet[i].HCost < currentNode.HCost))
                {
                    currentNode = openSet[i];
                }
            }
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            if (currentNode == end)
            {
                return(RetracePathVector2s(start, end));
            }

            foreach (Node neighbourNode in pathfindingGrid.GetNeighbours8(currentNode))
            {
                if (neighbourNode.Walkable == false || closedSet.Contains(neighbourNode) == true)
                {
                    continue;
                }

                int newMovementCostToNeigbour = currentNode.GCost + GetDistance(currentNode, neighbourNode);
                if (newMovementCostToNeigbour < neighbourNode.GCost || openSet.Contains(neighbourNode) == false)
                {
                    neighbourNode.GCost      = newMovementCostToNeigbour;
                    neighbourNode.HCost      = GetDistance(neighbourNode, end);
                    neighbourNode.ParentNode = currentNode;

                    if (openSet.Contains(neighbourNode) == false)
                    {
                        openSet.Add(neighbourNode);
                    }
                }
            }
        }
        return(null);
    }