/// <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 void MoveToState() { State = AgentState.Moving; if (!HasActionPlan()) { return; } if (!currentAction.IsTargetAcquired) { Debug.LogError("Action Failed: " + currentAction.ActionName + " Error: Target not accquired!"); ChangeState(FSMKeys.IDLE_STATE); } else { if (currentAction.IsInRange) { // Destination Reached - Change State ChangeState(FSMKeys.PERFORM_STATE); } else { // Get the agent to move itself agentImplementation.MoveAgent(currentAction); // Destination Not Reached - Loop ChangeState(FSMKeys.MOVETO_STATE); } } }
private void MoveState() { move = (fsm, gameObj) => { GoapAction action = currentActions.Peek(); if (goapAgent.MoveAgent(action)) { fsm.popState(); } }; }
private void CreateMoveToState() { // move the game object moveToState = (fsm, gameObj) => { GoapAction action = currentActions.Peek(); // get the agent to move itself if (dataProvider.MoveAgent(action)) { fsm.PopState(); } }; }
/// <summary> /// Implementation of <see cref="IAction"/> interface. Calls the MoveAgent of the <see cref="IGoap"/> interface. /// </summary> public void Execute() { // get the action we need to move to var action = _agent.PeekNextAction(); // get the agent to move itself _dataProvider.MoveAgent(action); if (action.RequiresInRange() == false || action.Target != null) { return; } // something went wrong. we need a new plan Debug.Log("<color=red>Fatal error:</color> Action requires a target but has none. Planning failed. You did not assign the target in your Action.checkProceduralPrecondition()"); _agent.GetCurrentActions().Clear(); // set state to PlanState }
private void createMoveToState() { moveToState = (fsm, gameObject) => { GoapAction action = currentActions.Peek(); if (action.requiresInRange() && action.target == null) { fsm.popState(); fsm.popState(); fsm.pushState(idleState); return; } if (dataProvider.MoveAgent(action)) { fsm.popState(); } }; }
private void CreateMoveToState() { moveToState = (fsm, gameObj) => { GoapAction action = currentPlan.Peek(); if (action.RequiresInRange() && action.target == null) { Debug.Log("<color=red>Fatal error:</color> Action requires a target but has none. Planning failed. You did not assign the target in your Action.CheckProceduralPrecondition()"); fsm.PopState(); // move fsm.PopState(); // perform fsm.PushState(idleState); return; } if (agent.MoveAgent(action)) { fsm.PopState(); } }; }
/// <summary> /// 创建移动状态 /// </summary> private void CreateMoveToState() { moveToState = (fsm, gameObj) => { // move the game object //移动游戏对象 var action = currentActions.Peek(); // if (action.requiresInRange() && action.target == null) // { // Debug.Log( // "<color=red>Fatal error:</color> Action requires a target but has none. Planning failed. You did not assign the target in your Action.checkProceduralPrecondition()"); // fsm.popState(); // move // fsm.popState(); // perform // fsm.pushState(idleState); // return; // } // get the agent to move itself if (dataProvider.MoveAgent(action)) { fsm.popState(); } /*MovableComponent movable = (MovableComponent) gameObj.GetComponent(typeof(MovableComponent)); * if (movable == null) { * Debug.Log("<color=red>Fatal error:</color> Trying to move an Agent that doesn't have a MovableComponent. Please give it one."); * fsm.popState(); // move * fsm.popState(); // perform * fsm.pushState(idleState); * return; * } * * float step = movable.moveSpeed * Time.deltaTime; * gameObj.transform.position = Vector3.MoveTowards(gameObj.transform.position, action.target.transform.position, step); * * if (gameObj.transform.position.Equals(action.target.transform.position) ) { * // we are at the target location, we are done * action.setInRange(true); * fsm.popState(); * }*/ }; }
private void CreateMoveToState() { moveToState = (fsm, gameObj) => { GoapAction action = currentActions.Peek(); if (action.RequiresInRange() && action.target == null) { Debug.Log("Fatal error: Action requires a target but has none. Planning failed. You did not assign the target in your Action.checkProceduralPrecondition()"); fsm.popState(); fsm.popState(); fsm.pushState(idleState); return; } Debug.Log("Move to do: " + action.Name); if (dataProvider.MoveAgent(action)) { fsm.popState(); } }; }
private void CreateMoveToState() { moveToState = (fsm, obj) => { GoapAction action = actionQueue.Peek(); if (action.RequiresInRange() && action.target == null) { Debug.Log("<color=red>Fatal error:</color> Action requires a target but has none." + "Planning failed. You did not assign the target in your" + "Action.checkProceduralPrecondition()"); fsm.PopState(); //move fsm.PopState(); //action fsm.PushState(idleState); return; } if (dataProvider.MoveAgent(action)) { fsm.PopState(); } }; }
private void CreateMoveToState() { _moveState = (fsm, gameObject) => { Debug.Log("Moving --"); var action = _currentActions.Peek(); if (action.RequiresInRange && action.Target == null) { // error? requires target, but target not found // return to IDLE _fsm.PopOff(); _fsm.PushState(_idleState); return; } // get agent to move itself if (_dataProvider.MoveAgent(action)) { _fsm.PopState(); } }; }
private void CreateMoveToState() { moveToState = (fsm, gameObj) => { // move the game object GoapAction action = currentActions.Peek(); if (action.RequiresInRange() && action.target == null) { Debug.Log("Fatal error: Action requires a target but has none. Planning failed. You did not assign the target in your Action.checkProceduralPrecondition()"); fsm.PopState(); // move fsm.PopState(); // perform fsm.PushState(idleState); return; } // get the agent to move itself Debug.Log("Move to do: " + action.name); if (enemyTypeDataProvider.MoveAgent(action)) { fsm.PopState(); } }; }