예제 #1
0
    /// <summary>
    /// Calculates the length of a connection in world space taking into account subnodes.
    /// </summary>
    /// <param name="nodeIDFrom">Node A ID.</param>
    /// <param name="nodeIDTo">Node B ID.</param>
    /// <returns>The legnth of a connection.</returns>
    private float CalculatePathLength(int nodeIDFrom, int nodeIDTo)
    {
        float pathLength = 0;

        // Find connection data
        GwConnection connection = nodes[nodeIDFrom].connections[nodeIDTo];

        // Check if connection path contains subnodes
        Vector3 currentPosition = nodes[nodeIDFrom].position;

        if (connection.subnodes != null && connection.subnodes.Length > 0)
        {
            for (int i = 0; i < connection.subnodes.Length; i++)
            {
                pathLength += Vector3.Distance(currentPosition, connection.subnodes[i]);

                currentPosition = connection.subnodes[i];
            }
        }

        // Add path length to final node
        pathLength += Vector3.Distance(currentPosition, nodes[nodeIDTo].position);

        return(pathLength);
    }
예제 #2
0
파일: GwNode.cs 프로젝트: bennettapps/astar
 /// <summary>
 /// Add a new connection from this node to another.
 /// </summary>
 /// <param name="connectedNodeID">The ID of the connected node.  Same as node GameObject name.</param>
 /// <param name="disabled">Connection is enabled/disabled by default.</param>
 /// <param name="speedWeight">Speed weight score of connection.</param>
 /// <param name="subnodes">Array of subnode positions, closest to furthest.</param>
 public void AddConnection(int connectedNodeID, bool disabled, float speedWeight, Vector3[] subnodes)
 {
     if (!connections.ContainsKey(connectedNodeID))
     {
         connections[connectedNodeID] = new GwConnection(connectedNodeID, disabled, speedWeight, subnodes);
     }
     else
     {
         UpdateConnection(connectedNodeID, disabled, speedWeight, subnodes);
     }
 }
예제 #3
0
    /// <summary>
    /// Traces the path from the target node back to the start node then reverses it.
    /// </summary>
    /// <param name="targetPosition">The target end point.</param>
    /// <param name="currentNodeID">The target node ID.</param>
    /// <returns>Waypoints array for path from A to B.</returns>
    private GwWaypoint[] TracePath(Vector3 targetPosition, int currentNodeID)
    {
        // Trace path
        List <GwWaypoint> path = new List <GwWaypoint>();

        // Add waypoint to target position (if off graph)
        if (activeJob.clampToEndNode == false)
        {
            path.Add(new GwWaypoint(targetPosition));
        }

        while (currentNodeID != -1)
        {
            int parentNodeID = nodes[currentNodeID].parentNodeID;

            if (parentNodeID != -1)
            {
                // Add waypoint
                GwConnection connection = nodes[parentNodeID].connections[currentNodeID];

                path.Add(new GwWaypoint(nodes[currentNodeID].position, connection.speedWeight));

                // Add subnode waypoints
                if (connection.subnodes != null && connection.subnodes.Length > 0)
                {
                    for (int i = connection.subnodes.Length - 1; i >= 0; i--)
                    {
                        path.Add(new GwWaypoint(connection.subnodes[i], connection.speedWeight));
                    }
                }
            }
            else
            {
                // Add starting node
                path.Add(new GwWaypoint(nodes[currentNodeID].position));
            }

            // Update current node
            currentNodeID = parentNodeID;
        }

        // Reverse path from B->A to A->B
        path.Reverse();

        return(path.ToArray());
    }