示例#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();
                }
            };
        }
示例#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);
                }
            }
        };
    }
示例#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();
            }
        };
    }
示例#4
0
    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();
            }
        };
    }
示例#5
0
    /// <summary>
    /// Create the move state.
    /// </summary>
    private void CreateMoveToState()
    {
        m_MoveToState = (fsm, gameObject) =>
        {
            GOAPAction action = m_CurrentActions.Peek();
            // If the current actions requires us to be in range of a target but we couldn't find a target, go to the idle state.
            if (action.RequiresInRange() && action.GetTarget() == null)
            {
                fsm.RemoveState();
                fsm.RemoveState();
                fsm.AddState(m_IdleState);
                return;
            }

            // If the agent has reached the target of their current action, move to the next state.
            if (m_DataProvider.MoveAgent(action))
            {
                fsm.RemoveState();
            }
        };
    }
示例#6
0
        /// <summary>
        /// Starts acting on the plan by moving the agen
        /// </summary>
        private void CreateMoveToState()
        {
            moveToState = (fsm, gameObj) =>
            {
                // move the game object

                GOAPAction 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();
                }
            };
        }