Example #1
0
        protected virtual void Start()
        {
            States = new Dictionary <string, bool>();
            foreach (var item in preState)
            {
                States[item.Key] = item.Value;
            }

            IdleState idleState = new IdleState(FSM)
            {
                onStart = () => { },
                onExit  = () => { }
            };

            idleState.onUpdate = () =>
            {
                if (NextPlanTime > FSM.time)
                {
                    return;
                }
                if (T_GraphAsset == null)
                {
                    return;
                }

                NextPlanTime = FSM.time + interval;

                // 搜寻计划
                foreach (GOAPGoal goal in Goals)
                {
                    Planner.Plan(T_Graph.AvailableActions.ToArray(), States, goal, maxDepth, ref storedActionQueue);
                    if (StoredActionQueue.Count == 0)
                    {
                        CurrentGoal = goal;
                        break;
                    }
                }

                if (storedActionQueue.Count > 0)
                {
                    actionQueue.Clear();
                    foreach (var action in storedActionQueue)
                    {
                        actionQueue.Enqueue(action);
                    }

                    //通知计划找到
                    if (Provider != null)
                    {
                        Provider.PlanFound(CurrentGoal, actionQueue);
                    }
                    //转换状态
                    FSM.ChangeTo("PerformActionState");
                }
                else
                {
                    //通知计划没找到
                    if (Provider != null)
                    {
                        Provider.PlanFailed(Goals);
                    }
                    CurrentGoal = null;
                }
            };

            GOAPFSMState performActionState = new GOAPFSMState(FSM)
            {
                onStart = () => { },
                onExit  = () => { }
            };

            performActionState.onUpdate = () =>
            {
                if (HasPlan)
                {
                    // 如果当前有计划(目标尚未完成)
                    GOAPAction action = actionQueue.Peek();
                    if (CurrentAction != action)
                    {
                        CurrentAction = action;
                        action.OnPrePerform();
                    }
                    // 成功 or 失败
                    GOAPActionStatus status = action.OnPerform();

                    switch (status)
                    {
                    case GOAPActionStatus.Success:
                        foreach (var effect in action.Effects)
                        {
                            SetState(effect.Key, effect.Value);
                        }
                        action.OnPostPerform(true);
                        if (Provider != null)
                        {
                            Provider.ActionFinished(action.Effects);
                        }
                        actionQueue.Dequeue();
                        CurrentAction = null;
                        break;

                    case GOAPActionStatus.Failure:
                        if (replanOnFailed)
                        {
                            EnforceReplan();
                        }
                        else
                        {
                            AbortPlan();
                        }
                        return;

                    default:
                        break;
                    }
                }
                else
                {
                    // 如果没有计划(目标已完成)
                    // 如果目标为一次性,移除掉
                    if (CurrentGoal != null && CurrentGoal.Once)
                    {
                        Goals.Remove(CurrentGoal);
                    }

                    // 通知计划完成
                    if (Provider != null)
                    {
                        Provider.PlanFinished();
                    }

                    // 当前目标设置为空
                    CurrentGoal = null;
                    FSM.ChangeTo("IdleState");
                }
            };

            FSM.PushState("IdleState", idleState);
            FSM.PushState("PerformActionState", performActionState);
            FSM.ChangeTo("IdleState");
        }