/// <summary> /// Advance the session to the next node in the path. /// </summary> /// <returns>True if the session was advanced successfully or false if the session is at the end of the path</returns> public bool advancePath() { if (currentIndex < path.NodeCount - 1) { // Increase index currentIndex++; // Update current currentNode = path.getNode(currentIndex); // Check if we advanced successfully return(currentNode != null); } return(false); }
private void onPathFound(Path path, PathRequestStatus status) { if (status == PathRequestStatus.PathFound) { // Cache the path and change state this.path = path; this.target = path.getNextNode(); // Check if we are at the starting node - if so then skip if (currentIndex == target.Index) { this.target = path.getNextNode(); } 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(); temp.push(searchGrid[currentIndex.X, currentIndex.Y], currentIndex); // Cache the path and change state this.path = temp; this.target = temp.getNextNode(); 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); } }
/// <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 is valid if (path == null) { // Go back to the idle state and do nothing changeState(AgentState.Idle); return; } // Check if we have arrived if (target.hasArrived(transform) == true) { // Update our agents target index currentIndex = target.Index; // Check if we have arrived at the last node if (path.hasArrivedAtLast(transform) == true) { // Snap to destination transform.position = path.LastNode.WorldPosition; // We have completed the path path = null; target = null; // Trigger message onDestinationReached(); // Change to idle state changeState(AgentState.Idle); } else { // Make sure we can still reach the target if (path.IsReachable == false) { if (dynamicRoutes == true) { // Update the destination setDestination(path.LastNode.Index); return; } } // Get the next node in the path target = path.getNextNode(); // Make sure our target is walkable if (dynamicRoutes == false) { if (target.IsWalkable == 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; } }
// Methods /// <summary> /// Switch this <see cref="PathSession"/> to use the specified <see cref="Path"/>. /// </summary> /// <param name="path"></param> public void usePath(Path path) { this.path = path; this.currentIndex = 0; this.currentNode = path.StartNode; }