Beispiel #1
0
        private void onPathFound(Path path, PathRequestStatus status)
        {
            if (status == PathRequestStatus.PathFound)
            {
                // Create the path session
                this.session = new PathSession(path);

                // Check if we are at the starting node - if so then skip
                if (currentIndex == session.Index)
                {
                    // Skip the starting node
                    session.advancePath();
                }

                changeState(AgentState.FollowingPath);
            }
            else if (status == PathRequestStatus.SameStartEnd)
            {
                // Make sure the agent moves to the current location (The agent may have already started moving away)
                Path temp = new Path(null);
                temp.push(searchGrid[currentIndex.X, currentIndex.Y], currentIndex);

                // Change state
                changeState(AgentState.FollowingPath);
            }
            else if (status == PathRequestStatus.PathNotFound)
            {
                // Trigger the message
                onDestinationUnreachable();

                // Go back to idle mode
                changeState(AgentState.Idle);
            }
            else
            {
                // Go back to idle mode
                changeState(AgentState.Idle);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Called by Unity.
        /// Can be overriden but be sure to call base.Update() to allow the agent to update properly.
        /// </summary>
        public virtual void Update()
        {
            // Reset flag
            isMoving = false;

            switch (state)
            {
            default:
            case AgentState.Idle:
            case AgentState.AwaitingPath:
            {
                // Store the last position
                lastPosition = transform.position;
            }
                return;

            case AgentState.FollowingPath:
            {
                // Make sure our path session is valid
                if (session == null)
                {
                    // Go back to the idle state and do nothing
                    changeState(AgentState.Idle);
                    return;
                }

                // Check if we have arrived
                if (session.hasReachedCurrentNode(transform) == true)
                {
                    // Update our agents target index
                    currentIndex = session.Index;

                    // Check if we have arrived at the last node
                    if (session.IsLastNode == true)
                    {
                        // Snap to destination
                        transform.position = session.WorldPosition;

                        // We have completed the path
                        session = null;

                        // Trigger message
                        onDestinationReached();

                        // Change to idle state
                        changeState(AgentState.Idle);
                    }
                    else
                    {
                        // Make sure we can still reach the target
                        if (session.CurrentPath.IsFullyReachable == false)
                        {
                            if (dynamicRoutes == true)
                            {
                                // Update the destination
                                setDestination(session.CurrentPath.LastNode.Index);
                                return;
                            }
                        }

                        // Get the next node in the path
                        session.advancePath();

                        // Make sure our target is walkable
                        if (dynamicRoutes == false)
                        {
                            //if(target.IsWalkable == false)
                            if (session.CurrentPath.IsFullyReachable == false)
                            {
                                Debug.LogWarning(string.Format("Agent [{0}]: I cannot reach that destination anymore. The path has been blocked", gameObject.name));

                                // Stop walking
                                changeState(AgentState.Idle);
                            }
                        }
                    }
                }
                else
                {
                    // Move towards our target
                    moveTowards();
                }
            } break;
            }
        }