Beispiel #1
0
        /// <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();
                }
            };
        }
Beispiel #2
0
    /// <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);
                }
            }
        };
    }
Beispiel #3
0
    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();
            }
        };
    }
Beispiel #4
0
    /*
     *  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;
        }
    }