/// <summary> /// Preforms the action if the agent is in range /// </summary> private void CreatePerformActionState() { prefromActionState = (fsm, gameObj) => { // perform the action if (!HasActionPlan()) { // no actions to perform Debug.Log("<color=red>Done actions</color>"); fsm.PopState(); fsm.PushState(idleState); dataProvider.ActionFinished(); 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.Preform(gameObj); if (!success) { // action failed, we need to plan again fsm.PopState(); fsm.PushState(idleState); Debug.LogWarning(gameObj.name + " could not prefrom the action, " + action + ". The agent (" + gameObject.name + ") is now going to abort plan."); 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.ActionFinished(); } }; }
public virtual bool MoveAgent(GOAPAction nextAction) { if (PathToTarget == null || !PathToTarget.Finished || PathToTarget.State != PathState.Ready) { if (PathToTarget == null) { PathToTarget = Path.Calculate(Map.Instance.AStarNetwork, transform.position, nextAction.Targets.Select(t => new Vector3I(t)).ToList(), true); } if (PathToTarget != null && PathToTarget.State == PathState.Invalid) { PathToTarget.Dispose(); PathToTarget = null; } return(false); } if (nextAction.IsInRange(_agent)) { return(true); } if (_currentNode == null) { _currentNode = PathToTarget.GetNode(0); if (_currentNode == null) { PathToTarget = null; return(true); } _pathIndex = 0; } PathToTarget.Visualize(Color.red, _pathIndex); var moveDist = MoveSpeed * Time.deltaTime; while (moveDist > 0) { if ((transform.position - _currentNode.Position).magnitude > moveDist) { transform.Translate((_currentNode.Position - transform.position).normalized * moveDist, Space.Self); transform.Find("Scale").LookAt(_currentNode.Position); return(false); } else { moveDist -= (transform.position - _currentNode.Position).magnitude; transform.position = _currentNode.Position; _currentNode = PathToTarget.GetNode(++_pathIndex); if (_currentNode == null) { PathToTarget = null; return(false); } } } return(true); }
/// <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); } } }; }