Beispiel #1
0
 private void JudgeException(object obj, string name)
 {
     if (obj == null)
     {
         DebugMsg.LogError("代理中" + name + "对象为空,请在代理子类中初始化该对象");
     }
 }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
 public bool Get(string key)
 {
     if (!_dataTable.ContainsKey(key))
     {
         DebugMsg.LogError("state not contain the key:" + key);
         return(false);
     }
     return(_dataTable[key]);
 }
        public IActionHandler <TAction> GetHandler(TAction actionLabel)
        {
            if (_actionHandlerDic.ContainsKey(actionLabel))
            {
                return(_actionHandlerDic[actionLabel]);
            }

            DebugMsg.LogError("Not add action name:" + actionLabel);
            return(null);
        }
Beispiel #5
0
        public IGoal <TGoal> GetGoal(TGoal goalLabel)
        {
            if (_goalsDic.ContainsKey(goalLabel))
            {
                return(_goalsDic[goalLabel]);
            }

            DebugMsg.LogError("Not add goal name:" + goalLabel);
            return(null);
        }
Beispiel #6
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);
        }
Beispiel #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);
        }
Beispiel #8
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);
        }
Beispiel #9
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));
     }
 }
Beispiel #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);
     }
 }
Beispiel #11
0
 //更新CurrentGoal对象
 private void UpdateCurrentGoal()
 {
     CurrentGoal = FindGoal();
     if (CurrentGoal != null)
     {
         DebugMsg.Log("CurrentGoal:" + CurrentGoal.Label.ToString());
     }
     else
     {
         DebugMsg.Log("CurrentGoal is null");
     }
 }
Beispiel #12
0
        public void ExcuteNewState(TLabel newState)
        {
            if (!_stateDic.ContainsKey(newState))
            {
                DebugMsg.LogError("状态机内不包含此状态对象:" + newState);
                return;
            }

            IFsmState <TLabel> state = _stateDic[newState];

            state.Enter();
        }
 //判断是否有能够打断计划的动作执行
 private void JudgeInterruptibleHandler()
 {
     foreach (var handler in _interruptibleHandlers)
     {
         if (handler.CanPerformAction())
         {
             DebugMsg.Log(handler.Label + "打断计划");
             _agent.Performer.Interruptible();
             break;
         }
     }
 }
Beispiel #14
0
        public void UpdateData()
        {
            DebugMsg.Log("----" + Label + "激活条件:" + ActiveCondition());

            if (ActiveCondition())
            {
                _onActivate(this);
            }
            else
            {
                _onInactivate(this);
            }
        }
Beispiel #15
0
 public void HandlerAction()
 {
     if (IsComplete)
     {
         _onComplete();
     }
     else
     {
         _currentActionHandler = _plan.Dequeue();
         DebugMsg.Log("----当前执行动作:" + _currentActionHandler.Label);
         _actionManager.ExcuteHandler(_currentActionHandler.Label);
     }
 }
Beispiel #16
0
        private void ChangeValue(string key, bool value)
        {
            if (_onChange != null)
            {
                DebugMsg.Log("-----state值被改变了" + key + "  " + value);
            }

            _dataTable[key] = value;
            if (_onChange != null)
            {
                _onChange();
            }
        }
Beispiel #17
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);
            }
        }
Beispiel #18
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);
            }
        }
 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 + "不在当前动作缓存内");
     }
 }
Beispiel #20
0
        protected async void OnComplete(float delayTime = 0)
        {
            if (ExcuteState == ActionExcuteState.EXIT)
            {
                return;
            }
            await Task.Delay(TimeSpan.FromSeconds(delayTime));

            ExcuteState = ActionExcuteState.EXIT;
            DebugMsg.Log("------设置" + Label + "影响");
            if (Action.Effects != null)
            {
                SetAgentData(Action.Effects);
            }
            if (_onFinishAction != null)
            {
                _onFinishAction();
            }
        }
        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);
            }
        }
Beispiel #22
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();
            }
        }
Beispiel #23
0
        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 + "此类型不在当前活跃对象缓存中");
            }
        }
Beispiel #24
0
        public IGoal <TGoal> FindGoal()
        {
            //查找优先级最大的那个
            _activeGoals = _activeGoals.OrderByDescending(u => u.GetPriority()).ToList();

            DebugMsg.Log("-----------active goal-----------");
            foreach (IGoal <TGoal> goal in _activeGoals)
            {
                DebugMsg.Log(goal.Label + " 优先级:" + goal.GetPriority());
            }
            DebugMsg.Log("---------------------------------");

            if (_activeGoals.Count > 0)
            {
                return(_activeGoals[0]);
            }
            else
            {
                return(null);
            }
        }
Beispiel #25
0
        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);
        }
Beispiel #26
0
        public TreeNode <TAction> Plan(IGoal <TGoal> goal)
        {
            Tree <TAction> tree = new Tree <TAction>();
            //初始化树的头节点
            TreeNode <TAction> topNode = CreateTopNode(tree, goal);

            TreeNode <TAction> cheapestNode = null;
            TreeNode <TAction> currentNode  = topNode;
            TreeNode <TAction> subNode      = null;

            while (!IsEnd(currentNode))
            {
                //获取所有的子行为
                List <IActionHandler <TAction> > handlers = GetSubHandlers(currentNode);

                DebugMsg.Log("---------------currentNode:" + currentNode.ID + "-----------------");
                foreach (IActionHandler <TAction> handler in handlers)
                {
                    DebugMsg.Log("计划子行为:" + handler.Label + "  优先级:" + handler.Action.Priority);
                }
                DebugMsg.Log("--------------------------------");

                foreach (IActionHandler <TAction> handler in handlers)
                {
                    subNode = tree.CreateNode(handler);
                    SetNodeState(currentNode, subNode);
                    subNode.Cost       = GetCost(subNode);
                    subNode.ParentNode = currentNode;
                    cheapestNode       = GetCheapestNode(subNode, cheapestNode);
                }

                currentNode  = cheapestNode;
                cheapestNode = null;
            }


            return(currentNode);
        }
Beispiel #27
0
 public void UpdateData()
 {
     DebugMsg.Log("GoalManager UpdateData");
     UpdateGoals();
     UpdateCurrentGoal();
 }