/// <summary> /// Function for the move state of the FSM, tells the agent how to move to its target /// </summary> /// <param name="a_NextAction">Action to do after the agent arrives at the target</param> /// <returns></returns> public bool MoveAgentToAction(CS_GOAPAction a_NextAction) { GetComponent<CS_DebugText>().ChangeCurrentActionText(a_NextAction.m_sActionName); float fDistance = Vector3.Distance(transform.position, a_NextAction.m_goTarget.transform.position);//Get distance to target if (fDistance < m_fAggroDistance)//If it is in aggro range { GetComponent<NavMeshAgent>().isStopped = false; GetComponent<NavMeshAgent>().SetDestination(a_NextAction.m_goTarget.transform.position);//Let the nav mesh do the work Vector3 v3LookDirection = a_NextAction.m_goTarget.transform.position - transform.position;//Look at the target v3LookDirection.y = 0; Quaternion qRotation = Quaternion.LookRotation(v3LookDirection); transform.rotation = Quaternion.Slerp(transform.rotation, qRotation, 0.005f); } if (m_bInterrupt) { GetComponent<CS_GOAPAgent>().GetDataProviderInterface().AbortPlan(a_NextAction); AbortPlan(a_NextAction); m_bInterrupt = false; return true; } if (fDistance <= m_fArrivalDistance)//If I have arrived { a_NextAction.SetInRange(true); return true; } else { return false; } }
public CS_GOAPAction m_Action; //The actual action /// <summary> /// Initializes a new instance of the <see cref="Node"/> class. /// </summary> /// <param name="a_nParentNode">The parent node.</param> /// <param name="a_fCost">The cost.</param> /// <param name="a_kvpState">State of a KVP.</param> /// <param name="a_Action">The action.</param> public Node(Node a_nParentNode, float a_fCost, HashSet <KeyValuePair <string, object> > a_kvpState, CS_GOAPAction a_Action) { m_nParentNode = a_nParentNode; m_fCost = a_fCost; m_kvpState = a_kvpState; m_Action = a_Action; }
/// <summary> /// Creates the state to make the agent perform an action /// </summary> private void CreatePerformActionState() { PerformActionState = (a_fFinateStateMachine, a_goObject) => { if (!HasActionPlan()) { FinateStateMachine.PopStateStack(); FinateStateMachine.PushStateToStack(IdleState); DataProviderInterface.AllActionsFinished(); return; } CS_GOAPAction Action = CurrentlyActiveActions.Peek(); if (Action.IsActionFinished()) { CurrentlyActiveActions.Dequeue(); } if (HasActionPlan()) { Action = CurrentlyActiveActions.Peek(); bool bIsInRange = Action.NeedsToBeInRange() ? Action.IsAgentInRange() : true; if (bIsInRange) { bool bActionPerformSuccess = Action.PerformAction(a_goObject); if (!bActionPerformSuccess) { FinateStateMachine.PopStateStack(); FinateStateMachine.PushStateToStack(IdleState); CreateIdleState(); DataProviderInterface.AbortPlan(Action); } } else { FinateStateMachine.PushStateToStack(MovingState); } } else { FinateStateMachine.PopStateStack(); FinateStateMachine.PushStateToStack(IdleState); DataProviderInterface.AllActionsFinished(); } }; }
/// <summary> /// Creates the state to move the agent to its destination for the FSM /// </summary> private void CreateMovingState() { MovingState = (a_fFinateStateMachine, a_goObject) => { CS_GOAPAction Action = CurrentlyActiveActions.Peek(); if (Action.NeedsToBeInRange() && Action.m_goTarget == null) { FinateStateMachine.PopStateStack(); FinateStateMachine.PopStateStack(); FinateStateMachine.PushStateToStack(IdleState); return; } if (DataProviderInterface.MoveAgentToAction(Action)) { FinateStateMachine.PopStateStack(); } }; }
public void AbortPlan(CS_GOAPAction a_FailedAction) { GetComponent <CS_GOAPAgent>().GetDataProviderInterface().AllActionsFinished(); a_FailedAction.ResetGA(); a_FailedAction.ResetAction(); }
/// <summary> /// Removes the action from the list of available actions. /// </summary> /// <param name="a_Action">an action.</param> public void RemoveAvailableAction(CS_GOAPAction a_Action) { AvailableActions.Remove(a_Action); }
/// <summary> /// Adds the given action to the list of available actions. /// </summary> /// <param name="a_Action">an action.</param> public void AddAvailableAction(CS_GOAPAction a_Action) { AvailableActions.Add(a_Action); }
/// <summary> /// Builds the branches from each action /// </summary> /// <param name="a_Actions">Usuable actions</param> /// <param name="a_ExcludedAction">The action to exclude from this branch</param> /// <returns></returns> protected HashSet <CS_GOAPAction> ActionBranches(HashSet <CS_GOAPAction> a_Actions, CS_GOAPAction a_ExcludedAction) { HashSet <CS_GOAPAction> Branches = new HashSet <CS_GOAPAction>(); foreach (CS_GOAPAction Action in a_Actions) { if (!Action.Equals(a_ExcludedAction)) { Branches.Add(Action); } } return(Branches); }