示例#1
0
    public static GameBoardComponent DetermineNextNode
        (GameBoardComponent current, Vector2 destination)
    {
        int currentLowestDistance  = Int32.MaxValue;
        int currentLowestHeuristic = Int32.MaxValue;

        GameBoardComponent closestComponent = null;
        Vector2            origin           = current.Vector;

        GameBoardComponent[] components = current.GetNeighbors();

        for (int i = 0; i < components.Length; i++)
        {
            if (!components[i].IsPassable)
            {
                break;
            }

            int h = ParseVectorDistance(components[i].Vector, destination);
            int g = ParseVectorDistance(components[i].Vector, origin);
            int d = h + g;

            if (d < currentLowestDistance)
            {
                closestComponent       = components[i];
                currentLowestDistance  = d;
                currentLowestHeuristic = h;
            }

            else if (d == currentLowestDistance)
            {
                if (h < currentLowestHeuristic)
                {
                    closestComponent       = components[i];
                    currentLowestDistance  = d;
                    currentLowestHeuristic = h;
                }

                else if (h == currentLowestHeuristic)
                {
                    if (Utility.RandomBoolean())
                    {
                        closestComponent       = components[i];
                        currentLowestDistance  = d;
                        currentLowestHeuristic = h;
                    }
                }
            }
        }

        if (closestComponent != null)
        {
            return(closestComponent);
        }
        else
        {
            return(null);
        }
    }