Esempio n. 1
0
    void Update()
    {
        if (m_currentTargetNode)
        {
            // Do target node look at if target is valid
            PerformLookAt(m_currentTargetNode.transform);

            m_totalDuration += Time.deltaTime;

            if (!IsWaiting)
            {
                // Move Truck towards next connection
                PerformMovement();

                // If nearly reached destination
                float nextNodeDistance = Vector3.Distance(transform.position, m_currentTargetNode.transform.position);
                if (nextNodeDistance < DESTINATION_TOLERANCE)
                {
                    // Increment and set new target game object
                    m_currentTargetNodeIndex++;

                    // Move target node to next node index
                    if (m_currentTargetNodeIndex > 0 && m_currentTargetNodeIndex < m_connectionDrivePath.Count)
                    {
                        Connection currentTargetConnection = m_connectionDrivePath[m_currentTargetNodeIndex];
                        m_currentTargetNode = currentTargetConnection.ToNode;

                        OnTravelNewConnection?.Invoke(this, currentTargetConnection);
                    }
                    else
                    {
                        //Debug.LogError("TargetNodeIndex is out of bounds!");
                    }
                }

                // Check if reached final node yet
                float finalNodeDistance = Vector3.Distance(transform.position, m_connectionDrivePath[m_connectionDrivePath.Count - 1].ToNode.transform.position);
                if (finalNodeDistance < DESTINATION_TOLERANCE)
                {
                    // Invoke event for reached path end and remove target
                    Connection finalConnection = m_connectionDrivePath[m_connectionDrivePath.Count - 1];
                    OnReachedPathEnd?.Invoke(this, finalConnection.ToNode);

                    string infoStr = $"Finished path to '{finalConnection.ToNode.name}', duration of '{Math.Round(m_totalDuration, 2)}s'";
                    m_ui.SetStatusText(infoStr);
                    Debug.Log($"Agent '{this.gameObject.name}' " + infoStr);

                    ResetPath();
                }
            }
        }
    }
Esempio n. 2
0
    private void NavigateACO()
    {
        if (m_currentTargetACOConn != null)
        {
            /// Look at next target node
            GameObject targetNodeObj = m_currentTargetACOConn.Route[m_currentACOConnRouteIndex].ToNode;
            PerformLookAt(targetNodeObj.transform);

            if (!IsWaiting)
            {
                /// if not waiting, move toward next route node
                PerformMovementTo(targetNodeObj.transform.position);

                /// Check if agent reached next route node
                float nextNodeDistance = Vector3.Distance(transform.position, targetNodeObj.transform.position);
                if (nextNodeDistance < DESTINATION_TOLERANCE)
                {
                    /// Reached next route node, increment to next route node or to new ACOConnection
                    m_currentACOConnRouteIndex++;

                    /// Check RouteIndex is within route bounds
                    if (m_currentACOConnRouteIndex >= m_currentTargetACOConn.Route.Count)
                    {
                        /// If index is more than route, we've reached end and can move to next ACOConnection
                        m_currentACOConnRouteIndex = 0;

                        m_currentTargetACOConnIndex++;
                        /// Check if reached end of ACOConnection path
                        if (m_currentTargetACOConnIndex >= m_acoConnectionPath.Count)
                        {
                            ACOConnection finalConnection = m_acoConnectionPath[m_acoConnectionPath.Count - 1];
                            OnReachedPathEnd?.Invoke(this, finalConnection.ToNode);

                            m_ui.SetStatusText($"Finished path to '{finalConnection.ToNode.name}'");

                            m_currentDrivePathTarget = NavigationTarget.ACOToStart;
                            Debug.Log($"Agent '{this.name}' finished ACO path. Navigating A* path to Start");

                            return;
                        }
                        else
                        {
                            /// Move to next node in Route
                            //Debug.Log($"Reached ACO goal {m_currentTargetACOConn.ToNode.name}");

                            OnReachedGoal?.Invoke(this, m_currentTargetACOConn.ToNode);

                            /// Continue moving through ACOConnection path if not at end
                            m_currentTargetACOConn = m_acoConnectionPath[m_currentTargetACOConnIndex];

                            OnTravelNewConnection?.Invoke(this, m_currentTargetACOConn);
                            return;
                        }
                    }
                    else
                    {
                        /// Still more Route nodes to travel to, invoke event to specify the connection
                        Connection nextRouteConnection = m_currentTargetACOConn.Route[m_currentACOConnRouteIndex];
                        OnTravelNewConnection?.Invoke(this, nextRouteConnection);
                    }
                }
            }
        }
    }
Esempio n. 3
0
    void Update()
    {
        if (m_currentTargetACOConn != null)
        {
            GameObject targetNodeObj = m_currentTargetACOConn.Route[m_currentACOConnRouteIndex].ToNode;
            /// Look at next target node
            PerformLookAt(targetNodeObj.transform);

            if (!IsWaiting)
            {
                /// if not waiting, move toward next route node
                PerformMovementTo(targetNodeObj.transform.position);

                /// Check if agent reached next route node
                float nextNodeDistance = Vector3.Distance(transform.position, targetNodeObj.transform.position);
                if (nextNodeDistance < DESTINATION_TOLERANCE)
                {
                    /// Reached next route node, increment to next route node or to new ACOConnection
                    m_currentACOConnRouteIndex++;
                    if (m_currentACOConnRouteIndex >= m_currentTargetACOConn.Route.Count)
                    {
                        m_currentACOConnRouteIndex = 0;

                        m_currentTargetACOConnIndex++;
                        /// Check if reached end of ACOConnection path
                        if (m_currentTargetACOConnIndex >= m_connectionPath.Count)
                        {
                            ACOConnection finalConnection = m_connectionPath[m_connectionPath.Count - 1];
                            OnReachedPathEnd?.Invoke(this, finalConnection.ToNode);

                            m_ui.SetStatusText($"Finished path to '{finalConnection.ToNode.name}'");
                            ResetPath();

                            return;
                        }
                        else
                        {
                            /// Continue moving through ACOConnection path if not at end
                            m_currentTargetACOConn = m_connectionPath[m_currentTargetACOConnIndex];

                            OnTravelNewConnection?.Invoke(this, m_currentTargetACOConn);
                            return;
                        }
                    }
                }


                // Check if agent reached final ACOConnection node
                float finalNodeDistance = Vector3.Distance(transform.position, m_connectionPath[m_connectionPath.Count - 1].ToNode.transform.position);
                if (finalNodeDistance < DESTINATION_TOLERANCE && m_currentTargetACOConnIndex >= m_connectionPath.Count)
                {
                    // Invoke event for reached path end and remove target
                    ACOConnection finalConnection = m_connectionPath[m_connectionPath.Count - 1];
                    OnReachedPathEnd?.Invoke(this, finalConnection.ToNode);

                    m_ui.SetStatusText($"Finished path to '{finalConnection.ToNode.name}'");

                    ResetPath();
                }
            }
        }
    }