/// <summary> /// Create the perform action state. /// </summary> private void CreatePerformActionState() { m_PerformActionState = (fsm, gameObject) => { // If the agent has no plan, go to the idle state and say we are doing nothing. if (!HasActionPlan()) { fsm.RemoveState(); fsm.AddState(m_IdleState); m_DataProvider.ActionsFinished(); return; } GOAPAction action = m_CurrentActions.Peek(); if (action.IsDone()) { m_CurrentActions.Dequeue(); } // If the agent has a plan. if (HasActionPlan()) { action = m_CurrentActions.Peek(); // Check if in range of action. bool inRange = action.RequiresInRange() ? action.IsInRange() : true; // If we are in range for the action. if (inRange) { // Check if the agent was able to perform the action. bool success = action.Perform(gameObject); // If not, abort plan and go back to idle. if (!success) { fsm.RemoveState(); fsm.AddState(m_IdleState); m_DataProvider.PlanAborted(action); } } // If not in range, start moving towards target. else { fsm.AddState(m_MoveToState); } } }; }
private void createPerformActionState() { performActionState = (fsm, obj) => { if (!hasActionPlan()) { fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); return; } GOAPAction action = currentActions.Peek(); if (action.IsDone()) { currentActions.Dequeue(); } if (hasActionPlan()) { action = currentActions.Peek(); bool inRange = action.RequiresInRange() ? action.isInRange() : true; if (inRange) { bool success = action.Perform(obj); if (!success) { fsm.popState(); fsm.pushState(idleState); createIdleState(); dataProvider.planAborted(action); } } else { fsm.pushState(moveToState); } } else { fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); } }; }
/* * Update */ public virtual void Update() { switch (_agentState) { case AgentState.Planning: { UpdateState(_state); foreach (var action in _actions) { action.Reset(); } if (FindPlan()) { _agentState = AgentState.Picking; } else { if (IdleAction != null) { IdleAction.Reset(); if (IdleAction.IsPossibleToPerform()) { _current = IdleAction; _agentState = AgentState.Performing; } } } } break; case AgentState.Picking: { if (_plan.Count() > 0) { _current = _plan.Dequeue(); _agentState = AgentState.Performing; } else { _agentState = AgentState.Completed; } } break; case AgentState.Performing: { if (_current.IsDone()) { ActionCompleted(_current); _agentState = AgentState.Picking; } else if (!_current.Perform()) { _agentState = AgentState.Picking; } } break; case AgentState.Completed: { _current = null; _agentState = AgentState.Planning; } break; case AgentState.Paused: break; } }