コード例 #1
0
ファイル: Planner.cs プロジェクト: JCYTop/Tmp
        /// <summary>
        /// 获取所有的子节点行为
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private List <IActionHandler <TAction> > GetSubHandlers(TreeNode <TAction> node)
        {
            var handlers = new List <IActionHandler <TAction> >();

            if (node == null)
            {
                return(handlers);
            }
            //获取状态差异
            var currkeys = node.CurrentState.GetValueDifferences(node.GoalState);
            var map      = agent.ActionManager.EffectsAndActionMap;

            foreach (string key in currkeys)
            {
                if (map.ContainsKey(key))
                {
                    foreach (var handler in map[key])
                    {
                        //筛选能够执行的动作
                        if (!handlers.Contains(handler) && handler.Action.Effects.Get(key) == node.GoalState.Get(key))
                        {
                            handlers.Add(handler);
                        }
                    }
                }
                else
                {
                    DebugMsg.LogError("当前没有动作能够实现从当前状态切换到目标状态,无法实现的键值为:" + key);
                }
            }

            //进行优先级排序
            handlers = handlers.OrderByDescending(u => u.Action.Priority).ToList();
            return(handlers);
        }
コード例 #2
0
ファイル: Agent.cs プロジェクト: youmuren0613/EntitasDemo
 private void JudgeException(object obj, string name)
 {
     if (obj == null)
     {
         DebugMsg.LogError("代理中" + name + "对象为空,请在代理子类中初始化该对象");
     }
 }
コード例 #3
0
 public bool Get(string key)
 {
     if (!_dataTable.ContainsKey(key))
     {
         DebugMsg.LogError("state not contain the key:" + key);
         return(false);
     }
     return(_dataTable[key]);
 }
コード例 #4
0
        public IGoal <TGoal> GetGoal(TGoal goalLabel)
        {
            if (_goalsDic.ContainsKey(goalLabel))
            {
                return(_goalsDic[goalLabel]);
            }

            DebugMsg.LogError("Not add goal name:" + goalLabel);
            return(null);
        }
コード例 #5
0
        public IActionHandler <TAction> GetHandler(TAction actionLabel)
        {
            if (_actionHandlerDic.ContainsKey(actionLabel))
            {
                return(_actionHandlerDic[actionLabel]);
            }

            DebugMsg.LogError("Not add action name:" + actionLabel);
            return(null);
        }
コード例 #6
0
        public TClass GetGameData <Tkey, TClass>(Tkey key) where Tkey : struct where TClass : class
        {
            TClass c = GetGameData(key) as TClass;

            if (c == null)
            {
                DebugMsg.LogError("无法转换类型");
            }

            return(c);
        }
コード例 #7
0
        /// <summary>
        /// 获取目标数据
        /// </summary>
        /// <param name="goalLabel"></param>
        /// <returns></returns>
        public IGoal <TGoal> GetGoal(TGoal goalLabel)
        {
            IGoal <TGoal> goal;

            _goalsDic.TryGetValue(goalLabel, out goal);
            if (goal == null)
            {
                DebugMsg.LogError("goal:" + goalLabel + " not init");
            }
            return(goal);
        }
コード例 #8
0
        /// <summary>
        /// 获取动作数据
        /// </summary>
        /// <param name="actionLabel"></param>
        /// <returns></returns>
        public IActionHandler <TAction> GetActionHandler(TAction actionLabel)
        {
            IActionHandler <TAction> handler;

            _actionHandlerDic.TryGetValue(actionLabel, out handler);
            if (handler == null)
            {
                DebugMsg.LogError("action:" + actionLabel + " not init");
            }
            return(handler);
        }
コード例 #9
0
        public void ExcuteNewState(TLabel newState)
        {
            if (!_stateDic.ContainsKey(newState))
            {
                DebugMsg.LogError("状态机内不包含此状态对象:" + newState);
                return;
            }

            IFsmState <TLabel> state = _stateDic[newState];

            state.Enter();
        }
コード例 #10
0
 public object GetGameData <Tkey>(Tkey key)
 {
     if (_gameData.ContainsKey(key.ToString()))
     {
         return(_gameData[key.ToString()]);
     }
     else
     {
         DebugMsg.LogError("can not find key name is " + key);
         return(null);
     }
 }
コード例 #11
0
 public TValue GetGameDataValue <Tkey, TValue>(Tkey key) where Tkey : struct where TValue : struct
 {
     try
     {
         return((TValue)GetGameData(key));
     }
     catch (Exception)
     {
         DebugMsg.LogError("无法转换类型");
         return(default(TValue));
     }
 }
コード例 #12
0
        protected void AddGoal <UGoal>()
            where UGoal : class, IGoal <TGoal>
        {
            UGoal goal = _pool.Spwan <UGoal>(_agent);

            if (!_goalsDic.ContainsKey(goal.Label))
            {
                _goalsDic.Add(goal.Label, goal);
            }
            else
            {
                DebugMsg.LogError("发现具有相同目标的Goal,标签为:" + goal.Label);
            }
        }
コード例 #13
0
        protected void AddAction <THandler, UAction>() where THandler : class, IActionHandler <TAction> where UAction : class, IAction <TAction>
        {
            UAction  action  = _pool.Spwan <UAction>(_agent);
            THandler handler = _pool.Spwan <THandler>(_agent, this, action);

            if (!_actionHandlerDic.ContainsKey(handler.Label))
            {
                _actionHandlerDic.Add(handler.Label, handler);
            }
            else
            {
                DebugMsg.LogError("发现具有重复标签的Handler,标签为:" + handler.Label);
            }
        }
コード例 #14
0
 public virtual void ExcuteNewState(TAction label)
 {
     if (_actionHandlerDic.ContainsKey(label))
     {
         _actionFsm.ExcuteNewState(label);
     }
     else if (_actionStateHandlers.ContainsKey(label))
     {
         _actionStateFsm.ExcuteNewState(label);
     }
     else
     {
         DebugMsg.LogError("动作" + label + "不在当前动作缓存内");
     }
 }
コード例 #15
0
        private void AddHandler(TAction label, Dictionary <TAction, IActionHandler <TAction> > dic)
        {
            var handler = _agent.Maps.GetActionHandler(label);

            if (handler != null)
            {
                dic.Add(label, handler);
                //这里写拉姆达表达式,是为了避免初始化的时候_onActionComplete还是null的
                handler.AddFinishCallBack(() =>
                {
                    DebugMsg.Log("动作完成:   " + label);
                    _onActionComplete(label);
                });
            }
            else
            {
                DebugMsg.LogError("映射文件中未找到对应Handler,标签为:" + label);
            }
        }
コード例 #16
0
        //执行新状态
        public void ExcuteNewState(TLabel newState)
        {
            if (!_stateDic.ContainsKey(newState))
            {
                DebugMsg.LogError("状态机内不包含此状态对象:" + newState);
                return;
            }
            _previousState = _currentState;
            _currentState  = _stateDic[newState];

            if (_previousState != null)
            {
                _previousState.Exit();
            }

            if (_currentState != null)
            {
                _currentState.Enter();
            }
        }
コード例 #17
0
ファイル: ObjectPool.cs プロジェクト: JCYTop/Tmp
        public void Despawn <T>(T obj)
        {
            Type type = typeof(T);

            if (_activeDic.ContainsKey(type))
            {
                if (_activeDic[type].Contains(obj))
                {
                    _activeDic[type].Remove(obj);
                    _inactiveDic[type].Add(obj);
                }
                else
                {
                    DebugMsg.LogError(type + "此对象不在当前活跃对象缓存中");
                }
            }
            else
            {
                DebugMsg.LogError(type + "此类型不在当前活跃对象缓存中");
            }
        }
コード例 #18
0
ファイル: Planner.cs プロジェクト: youmuren0613/EntitasDemo
        public Queue <IActionHandler <TAction> > BuildPlan(IGoal <TGoal> goal)
        {
            DebugMsg.Log("制定计划");
            DebugMsg.Log("---------------当前代理状态------------");
            DebugMsg.Log(_agent.AgentState.ToString());
            DebugMsg.Log("---------------------------");

            Queue <IActionHandler <TAction> > plan = new Queue <IActionHandler <TAction> >();

            if (goal == null)
            {
                return(plan);
            }

            TreeNode <TAction> currentNode = Plan(goal);

            if (currentNode == null)
            {
                plan.Enqueue(_agent.ActionManager.GetHandler(_agent.ActionManager.GetDefaultActionLabel()));
                DebugMsg.LogError("当前节点为空,设置当前动作为默认动作");
                return(plan);
            }

            while (currentNode.ID != TreeNode <TAction> .DEFAULT_ID)
            {
                plan.Enqueue(currentNode.ActionHandler);
                currentNode = currentNode.ParentNode;
            }

            DebugMsg.Log("---------------最终生成计划------------");
            foreach (IActionHandler <TAction> handler in plan)
            {
                DebugMsg.Log("计划项:" + handler.Label);
            }
            DebugMsg.Log("---------------当前代理状态------------");
            DebugMsg.Log(_agent.AgentState.ToString());
            DebugMsg.Log("---------------------------");
            DebugMsg.Log("计划结束");
            return(plan);
        }