/// <summary> /// Implementation of <see cref="IAction"/> interface. Executes the <see cref="GoapAction"/> in the <see cref="GoapAgent"/> current plan. /// </summary> public void Execute() { if (_agent.NeedNewPlan) // there is no more actions in the current plan, needs a new plan { Debug.Log("<color=red>Done actions</color>"); _dataProvider.ActionsFinished(); return; } // check if the current action has finish its execution var currentActions = _agent.GetCurrentActions(); var action = currentActions.Peek(); if (action.IsDone()) { // the action is done. Remove it so we can perform the next one var currAction = currentActions.Dequeue(); IGoapAction nextAction = null; if (currentActions.Count > 0) { nextAction = currentActions.Dequeue(); } _dataProvider.CurrentActionFinished(currAction, nextAction); // todo } if (_agent.NeedNewPlan == false) // in case the previous action was complete, we need to check again if we stil have a plan { // perform the next action action = currentActions.Peek(); // check if we are in range of the next action or if we need to move var inRange = action.RequiresInRange() == false || action.InRange; if (inRange) { // we are in range, so perform the action var success = action.Perform(_agent.gameObject); if (success) { return; } // soemthing went wrong // action failed, we need to plan again currentActions.Clear(); // we need a new plan _dataProvider.PlanAborted(action); // call plan aborted to perform clean up if required } else // we need to move { _dataProvider.MoveAgent(action); } } else // _agent.NeedNewPlan == true { // all actions are completed. Perform clean up code of the current plan. No need to change state, since the NeedNewPlanCondition will be true every time the NeedNewPlan property is true _dataProvider.ActionsFinished(); } }
public override void Update(float dt) { if (!hasActionPlan()) { Debug.Log("<color=red>Done actions</color>"); controller.Change("idle"); goapData.ActionsFinished(); return; } GoapAction action = currentActions.Peek(); if (!action.IsDone()) { bool success = action.PerformAction(agent); if (!success) { controller.Change("idle"); goapData.ActionsFinished(); } } if (action.IsDone()) { currentActions.Dequeue(); } }
private void CreatePerformActionState() { performActionState = (fsm, gameObj) => { // perform the action if (!HasActionPlan()) { // no actions to perform Debug.Log("<color=red>Done actions</color>"); fsm.PopState(); fsm.PushState(idleState); dataProvider.ActionsFinished(); return; } GoapAction action = currentActions.Peek(); if (action.IsDone()) { // the action is done. Remove it so we can perform the next one currentActions.Dequeue(); } if (HasActionPlan()) { // perform the next action action = currentActions.Peek(); bool inRange = action.RequiresInRange() ? action.isInRange() : true; if (inRange) { // we are in range, so perform the action bool success = action.Perform(gameObj); if (!success) { // action failed, we need to plan again fsm.PopState(); fsm.PushState(idleState); dataProvider.PlanAborted(action); } } else { // we need to move there first // push moveTo state fsm.PushState(moveToState); } } else { // no actions left, move to Plan state fsm.PopState(); fsm.PushState(idleState); dataProvider.ActionsFinished(); } }; }
void CreatePreformActionState() { performActionState = (fsm, gameObj) => { if (!HasActionPlan()) { Debug.Log("<color=red>Done actions</color>"); 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 succes = action.Preform(gameObj); if (!succes) { fsm.PopState(); fsm.PushState(idleState); dataProvider.PlanAborted(action); } } else { fsm.PushState(moveToState); } } else { fsm.PopState(); fsm.PushState(idleState); dataProvider.ActionsFinished(); } }; }
private void CreatePerformActionState() { performActionState = (fsm, gameObj) => { if (!HasActionPlan()) { Debug.Log("<color=red>Done actions</color>"); fsm.PopState(); fsm.PushState(idleState); agent.ActionsFinished(); return; } GoapAction action = currentPlan.Peek(); if (action.IsDone()) { currentPlan.Dequeue(); } if (HasActionPlan()) { action = currentPlan.Peek(); bool inRange = !action.RequiresInRange() || action.IsInRange(); if (inRange) { bool success = action.Perform(gameObj); if (!success) { fsm.PopState(); fsm.PushState(idleState); agent.PlanAborted(action); } } else { fsm.PushState(moveToState); } } else { fsm.PopState(); fsm.PushState(idleState); agent.ActionsFinished(); } }; }
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(); } }; }
private void CreatePerformActionState() { performActionState = (fsm, gagent) => { if (!HasActionPlan()) { Debug.Log("Done with actions, planning new actions."); fsm.PopState(); fsm.PushState(planningState); worldDataProvider.ActionsFinished(); return; } GAction action = currentActions.Peek(); if (action.IsDone()) { currentActions.Dequeue(); } if (HasActionPlan()) { action = currentActions.Peek(); bool success = action.Perform(gagent); if (!success) { fsm.PopState(); fsm.PushState(planningState); worldDataProvider.PlanAborted(action); } } else { fsm.PopState(); fsm.PushState(planningState); worldDataProvider.ActionsFinished(); } }; }
/// <summary> /// 创建执行操作状态 /// </summary> private void CreatePerformActionState() { performActionState = (fsm, gameObj) => { // perform the action //执行动作 if (!HasActionPlan()) { // no actions to perform //没有要执行的操作 Debug.Log("<color=red>Done actions</color>"); fsm.popState(); fsm.pushState(idleState); dataProvider.ActionsFinished(); return; } var action = currentActions.Peek(); if (action.IsDone()) { // the action is done. Remove it so we can perform the next one //动作完成 删除它,以便我们可以执行下一个 currentActions.Dequeue(); } if (HasActionPlan()) { // perform the next action //执行下一个操作 action = currentActions.Peek(); var inRange = action.RequiresInRange() ? action.IsInRange() : true; if (inRange) { // we are in range, so perform the action //我们在范围内,所以执行操作 var success = action.Perform(gameObj, dataProvider.GetBlackBoard()); if (!success) { // action failed, we need to plan again //操作失败,我们需要再次计划 fsm.popState(); fsm.pushState(idleState); dataProvider.PlanAborted(action); } } else { // we need to move there first // push moveTo state //我们需要先移动到那里 //推动移动到状态 fsm.pushState(moveToState); } } else { // no actions left, move to Plan state //没有动作,转移到计划状态 fsm.popState(); fsm.pushState(idleState); dataProvider.ActionsFinished(); } }; }