Beispiel #1
0
        /// <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();
            }
        }
Beispiel #2
0
        private void CreatePerformState()
        {
            _performState = (fsm, fsmGameObject) =>
            {
                // all actions done, plan over...
                if (_plan.Count == 0)
                {
                    _dataProvider.PlanFinished();
                    fsm.PopState();
                    fsm.PushState(_idleState);
                    return;
                }

                // if action done (in other words, has exited)
                var action = _plan.Peek();
                if (action.IsDone())
                {
                    _plan.Pop();
                    return;
                }

                // action go!
                if (!action.Go(this))
                {
                    _dataProvider.PlanAborted(action);
                    fsm.PopState();
                    fsm.PushState(_idleState);
                }
            };
        }
Beispiel #3
0
    private void ActionState()
    {
        act = (fsm, gameObj) => {
            if (currentActions.Count == 0)
            {
                fsm.popState();
                fsm.pushState(idle);
                return;
            }

            GoapAction action = currentActions.Peek();

            if (!action.init)
            {
                if (!action.inWait)
                {
                    bool success = action.IsSucc();
                    if (!success)
                    {
                        fsm.popState();
                        fsm.pushState(idle);
                        goapAgent.PlanAborted(action);
                        action.DoReset();
                        return;
                    }

                    if (action.IsDone())
                    {
                        action.DoReset();
                        goapAgent.ActionFinished(action);
                        currentActions.Dequeue();
                    }
                }
                else
                {
                    return;
                }
            }

            if (currentActions.Count > 0)
            {
                action = currentActions.Peek();

                if (action.IsInRange())
                {
                    action.DoAction(gameObj);
                    action.init = false;
                }
                else
                {
                    fsm.pushState(move);
                }
            }
            else
            {
                fsm.popState();
                fsm.pushState(idle);
            }
        };
    }
Beispiel #4
0
    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();
            }
        };
    }
Beispiel #5
0
        private void CreatePerformActionState()
        {
            _performState = (fsm, gameObject) =>
            {
                Debug.Log("Performing --");

                //  Perform the action
                if (!HasActionPlan())
                {
                    //  no actions left
                    _fsm.PopState();
                    _fsm.PushState(_idleState);
                    _dataProvider.ActionsCompleted();
                    return;
                }

                var action = _currentActions.Peek();
                if (action.IsDone())
                {
                    //  action completed, remove and continue w/ sequence
                    _currentActions.Dequeue();
                }

                if (HasActionPlan())
                {
                    action = _currentActions.Peek();

                    bool inRange = !action.RequiresInRange || action.IsInRange;
                    if (inRange)
                    {
                        bool actionSuccess = action.Perform(gameObject);

                        if (!actionSuccess)
                        {
                            //  action failed, plan again
                            _fsm.PopState();
                            _fsm.PushState(_idleState);
                            _dataProvider.PlanAborted(action);
                        }
                    }
                    else
                    {
                        //  we ened to move there first?
                        //  what?

                        _fsm.PushState(_moveState);
                    }
                }
                else
                {
                    //  no actions left
                    _fsm.PopState();
                    _fsm.PushState(_idleState);
                    _dataProvider.ActionsCompleted();
                }
            };
        }
Beispiel #6
0
    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();
            }
        };
    }
Beispiel #7
0
    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();
            }
        };
    }
Beispiel #8
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 #9
0
    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();
            }
        };
    }
Beispiel #10
0
    /// <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();
            }
        };
    }