예제 #1
0
    private PathNode getPathNodeOfNearest(int startX, int startY, IGridObjectType gridObjectType)
    {
        PathNode startNode = grid.GetGridObject(startX, startY);

        if (startNode == null)
        {
            // Invalid Path
            return(null);
        }

        openList = new List <PathNode> {
            startNode
        };
        closedList = new List <PathNode>();

        while (openList.Count > 0)
        {
            PathNode currentNode = GetLowestFCostNode(openList);

            openList.Remove(currentNode);
            closedList.Add(currentNode);

            if (currentNode.referenceToObjects.Exists(referenceToObject =>
                                                      referenceToObject.gridObjectType.Equals(gridObjectType)))
            {
                return(currentNode);
            }

            foreach (PathNode neighbourNode in GetNeighbourList(currentNode))
            {
                if (closedList.Contains(neighbourNode))
                {
                    continue;
                }

                if (!neighbourNode.isWalkable && !neighbourNode.referenceToObjects.Exists(referenceToObject =>
                                                                                          referenceToObject.gridObjectType.Equals(gridObjectType)))
                {
                    closedList.Add(neighbourNode);
                    continue;
                }

                if (!openList.Contains(neighbourNode))
                {
                    openList.Add(neighbourNode);
                }
            }
        }

        // Out of nodes on the openList
        return(null);
    }