Example #1
0
        public void Debug(AgentDesigner agent)
        {
            if (agent == null)
            {
                MainForm.Instance.ShowMessage("Agent为空");
                return;
            }

            m_Nodes.Clear();
            VerifyInfo verifyAgent = agent.VerifyAgent();

            if (verifyAgent.HasError)
            {
                MainForm.Instance.ShowMessage("确保行为树编辑正确后才能调试\n" + verifyAgent.Msg);
                return;
            }

            for (int i = 0; i < agent.Nodes.Count; i++)
            {
                NodeDesigner node = agent.Nodes[i];
                if (node.StartNode)
                {
                    m_DebugNode = CreateDebugNode(agent, node);
                    break;
                }
            }

            State = DebugState.Running;
            ContentUserControl.Instance.OnDebugStart();
            MainForm.Instance.ShowInfo("播放成功 时间:" + DateTime.Now);
        }
        public override void OnRunning(float deltatime)
        {
            DebugNode runningNode = Childs[RunningNodeIndex];

            runningNode.Update(deltatime);

            if (runningNode.Status == DebugNodeStatus.Error)
            {
                Status = DebugNodeStatus.Error;
                return;
            }

            if (runningNode.Status == DebugNodeStatus.Failed || runningNode.Status == DebugNodeStatus.Success)
            {
                CurTimes++;
            }

            if (LoopTimes != -1 && CurTimes >= LoopTimes)
            {
                Status = DebugNodeStatus.Success;
                return;
            }

            if (runningNode.Status == DebugNodeStatus.Failed || runningNode.Status == DebugNodeStatus.Success)
            {
                SetChildState(DebugNodeStatus.None);
            }
        }
        public override void OnRunning(float deltatime)
        {
            DebugNode runningNode = Childs[RunningNodeIndex];

            runningNode.Update(deltatime);

            switch (runningNode.Status)
            {
            case DebugNodeStatus.Success:
                RunningNodeIndex++;
                if (RunningNodeIndex >= Childs.Count)
                {
                    Status = DebugNodeStatus.Success;
                }
                break;

            case DebugNodeStatus.Failed:
                Status = DebugNodeStatus.Failed;
                break;

            case DebugNodeStatus.Error:
                Status = DebugNodeStatus.Error;
                break;
            }
        }
Example #4
0
        public override void OnRunning(float deltatime)
        {
            DebugNode runningNode = Children[RunningNodeIndex];

            runningNode.Update(deltatime);

            if (runningNode.Status == DebugNodeStatus.Error)
            {
                Status = DebugNodeStatus.Error;
                return;
            }

            if (runningNode.Status == DebugNodeStatus.Failed)
            {
                RunningNodeIndex++;
                if (RunningNodeIndex >= Childs.Count)
                {
                    Status = DebugNodeStatus.Failed;
                    return;
                }
            }
            else if (runningNode.Status == DebugNodeStatus.Success)
            {
                Status = DebugNodeStatus.Success;
            }
        }
        public void Debug(BehaviorTreeDesigner behaviorTree)
        {
            if (behaviorTree == null)
            {
                MainForm.Instance.ShowMessage("行为树为空");
                return;
            }

            m_Nodes.Clear();
            VerifyInfo verifyBehaviorTree = behaviorTree.VerifyBehaviorTree();

            if (verifyBehaviorTree.HasError)
            {
                MainForm.Instance.ShowMessage("确保行为树编辑正确后才能调试\n" + verifyBehaviorTree.Msg);
                return;
            }

            for (int i = 0; i < behaviorTree.Nodes.Count; i++)
            {
                NodeDesigner node = behaviorTree.Nodes[i];
                if (node.StartNode)
                {
                    m_DebugNode = CreateDebugNode(behaviorTree, node);
                    break;
                }
            }

            State = DebugState.Running;
            ContentUserControl.Instance.OnDebugStart();
            MainForm.Instance.ShowInfo("播放成功 时间:" + DateTime.Now);
        }
Example #6
0
        public override void OnRunning(float deltatime)
        {
            CurFrames++;
            DebugNode runningNode = Childs[RunningNodeIndex];

            //子节点跳过Transition状态
            if (runningNode.Status == DebugNodeStatus.None)
            {
                runningNode.Status = DebugNodeStatus.Transition;
                runningNode.TransitionElapsedTime = DebugManager.TransitionTime;
            }

            runningNode.Update(deltatime);

            if (runningNode.Status == DebugNodeStatus.Error)
            {
                Status = DebugNodeStatus.Error;
                return;
            }

            if (CurFrames >= Frames)
            {
                Status = DebugNodeStatus.Success;
                return;
            }

            if (runningNode.Status == DebugNodeStatus.Failed || runningNode.Status == DebugNodeStatus.Success)
            {
                SetChildState(DebugNodeStatus.Transition);
            }
        }
        public override void OnRunning(float deltatime)
        {
            DebugNode runningNode = RandList[RunningNodeIndex];

            runningNode.Update(deltatime);

            if (runningNode.Status == DebugNodeStatus.Success)
            {
                Status = DebugNodeStatus.Success;
            }
            else if (runningNode.Status == DebugNodeStatus.Failed)
            {
                Status = DebugNodeStatus.Failed;
                RunningNodeIndex++;
                //所有运行失败将返回失败
                if (RunningNodeIndex >= Children.Count)
                {
                    Status = DebugNodeStatus.Failed;
                }
            }
            else if (runningNode.Status == DebugNodeStatus.Error)
            {
                Status = DebugNodeStatus.Error;
            }
        }
Example #8
0
        /// <summary>
        /// 绘制节点
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="graphics">graphics</param>
        /// <param name="offset">偏移</param>
        /// <param name="on">是否选中</param>
        public static void Draw_Debug(DebugNode node, Graphics graphics, float deltatime)
        {
            Rect rect = GetRect(node.Node);

            switch (node.Status)
            {
            case DebugNodeStatus.Transition:
                if (node.TransitionElapsedTime >= DebugManager.TransitionTime)
                {
                    graphics.DrawRectangle(Debug_Running_NodePen, rect);
                }
                break;

            case DebugNodeStatus.Error:
                graphics.DrawRectangle(Debug_Error_NodePen, rect);
                break;

            case DebugNodeStatus.Failed:
                graphics.DrawRectangle(Debug_Failed_NodePen, rect);
                break;

            case DebugNodeStatus.Success:
                if (node.SuccessAlpha <= 0.9f)
                {
                    Pen   successed_NodePen = Debug_Successed_NodePen.Clone() as Pen;
                    Color successColor      = successed_NodePen.Color;
                    node.SuccessAlpha       = Mathf.Lerp(node.SuccessAlpha, 1, RunningAlphaSpeed * deltatime);
                    node.SuccessAlpha       = Mathf.Clamp01(node.SuccessAlpha);
                    successColor            = Color.FromArgb((int)(node.SuccessAlpha * 255), successColor);
                    successed_NodePen.Color = successColor;
                    graphics.DrawRectangle(successed_NodePen, rect);
                }
                else
                {
                    graphics.DrawRectangle(Debug_Successed_NodePen, rect);
                }
                break;

            case DebugNodeStatus.Running:

                if (node.SuccessAlpha <= 0.9f)
                {
                    Pen   runningPen = Debug_Running_NodePen.Clone() as Pen;
                    Color color      = runningPen.Color;
                    node.RunningAlpha = Mathf.Lerp(node.RunningAlpha, 1, RunningAlphaSpeed * deltatime);
                    node.RunningAlpha = Mathf.Clamp01(node.RunningAlpha);
                    color             = Color.FromArgb((int)(node.RunningAlpha * 255), color);
                    runningPen.Color  = color;
                    graphics.DrawRectangle(runningPen, rect);
                }
                else
                {
                    graphics.DrawRectangle(Debug_Running_NodePen, rect);
                }
                break;
            }
        }
        public override void OnEnter()
        {
            if (Childs.Count != 3)
            {
                Status = DebugNodeStatus.Error;
            }

            CurrentRunningIndex = 0;
            ConditionNode       = Childs[0];
        }
        public override void OnRunning(float deltatime)
        {
            DebugNode runningNode = Childs[RunningNodeIndex];

            runningNode.Update(deltatime);

            if (runningNode.Status != DebugNodeStatus.Transition)
            {
                Status = runningNode.Status;
            }
        }
Example #11
0
        public void Stop()
        {
            if (State == DebugState.None)
            {
                return;
            }

            m_DebugNode = null;
            m_Nodes.Clear();
            State = DebugState.None;
            MainForm.Instance.ShowInfo("停止成功 时间:" + DateTime.Now);
        }
Example #12
0
        public void DoNodes(Graphics graphics, float deltatime)
        {
            if (State == DebugState.None)
            {
                return;
            }

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                DebugNode debugNode = m_Nodes[i];
                EditorUtility.Draw_Debug(debugNode, graphics, deltatime);
            }
        }
Example #13
0
        public override void OnEnter()
        {
            Children.Clear();
            Children.AddRange(Childs.ToArray());
            Random random = new Random();

            //打乱子节点
            int count = Children.Count;

            for (int index = 0; index < count; index++)
            {
                int       randIndex = random.Next(index, count);
                DebugNode node      = Children[randIndex];
                Children[randIndex] = Children[index];
                Children[index]     = node;
            }
        }
Example #14
0
        public override void OnRunning(float deltatime)
        {
            int failCount    = 0;
            int successCount = 0;

            for (int i = 0; i < Childs.Count; i++)
            {
                DebugNode childNode = Childs[i];
                childNode.Update(deltatime);

                if (childNode.Status == DebugNodeStatus.Failed)
                {
                    failCount++;

                    if (FailurePolicy == FAILURE_POLICY.FAIL_ON_ONE)
                    {
                        Status = DebugNodeStatus.Failed;
                        break;
                    }
                    else if (FailurePolicy == FAILURE_POLICY.FAIL_ON_ALL && failCount == Childs.Count)
                    {
                        Status = DebugNodeStatus.Failed;
                    }
                }
                else if (childNode.Status == DebugNodeStatus.Success)
                {
                    successCount++;

                    if (SuccessPolicy == SUCCESS_POLICY.SUCCEED_ON_ONE)
                    {
                        Status = DebugNodeStatus.Success;
                    }
                    else if (SuccessPolicy == SUCCESS_POLICY.SUCCEED_ON_ALL && successCount == Childs.Count)
                    {
                        Status = DebugNodeStatus.Success;
                    }
                }
                else if (childNode.Status == DebugNodeStatus.Error)
                {
                    Status = DebugNodeStatus.Error;
                }
            }
        }
Example #15
0
        public override void OnRunning(float deltatime)
        {
            DebugNode runningNode = Childs[RunningNodeIndex];

            runningNode.Update(deltatime);

            if (runningNode.Status == DebugNodeStatus.Error)
            {
                Status = DebugNodeStatus.Error;
            }
            else if (runningNode.Status == DebugNodeStatus.Failed)
            {
                Status = DebugNodeStatus.Failed;
            }
            else if (runningNode.Status == DebugNodeStatus.Success)
            {
                Status = DebugNodeStatus.Failed;
            }
        }
Example #16
0
        public void DoTransitions(Graphics graphics, Vec2 offset)
        {
            if (State == DebugState.None)
            {
                return;
            }

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                DebugNode node = m_Nodes[i];
                if (node.Node.ParentNode == null)
                {
                    continue;
                }
                if (node.Status == DebugNodeStatus.None)
                {
                    continue;
                }
                BezierLink.DrawNodeToNode_Debug(graphics, node.ParentNode, node, offset);
            }
        }
        public override void OnRunning(float deltatime)
        {
            if (CurrentRunningIndex == 0)
            {
                ConditionNode.Update(deltatime);

                if (ConditionNode.Status == DebugNodeStatus.Error)
                {
                    Status = DebugNodeStatus.Error;
                    return;
                }
            }

            if (ConditionNode.Status == DebugNodeStatus.Success)
            {
                CurrentRunningIndex = 1;
            }
            else if (ConditionNode.Status == DebugNodeStatus.Failed)
            {
                CurrentRunningIndex = 2;
            }

            if (CurrentRunningIndex != 0)
            {
                DebugNode debugNode = Childs[CurrentRunningIndex];
                debugNode.Update(deltatime);

                if (debugNode.Status == DebugNodeStatus.Error)
                {
                    Status = DebugNodeStatus.Error;
                    return;
                }

                if (debugNode.Status >= DebugNodeStatus.Running)
                {
                    Status = debugNode.Status;
                }
            }
        }
Example #18
0
 private DebugNode FindByID(DebugNode node, int ID)
 {
     if (node.Node.ID == ID)
     {
         return(node);
     }
     else
     {
         if (node.Childs.Count > 0)
         {
             for (int i = 0; i < node.Childs.Count; i++)
             {
                 DebugNode debugNode = node.Childs[i];
                 DebugNode result    = FindByID(debugNode, ID);
                 if (result != null)
                 {
                     return(result);
                 }
             }
         }
     }
     return(null);
 }
Example #19
0
        private DebugNode CreateDebugNode(AgentDesigner agent, NodeDesigner node)
        {
            DebugNode debugNode = null;

            //组合节点
            if (node.NodeType == NodeType.Composite)
            {
                //顺序节点
                if (node.ClassType == "Sequence")
                {
                    Debug__Sequence debug__Sequence = new Debug__Sequence();
                    debugNode = debug__Sequence;
                }
                //并行节点
                else if (node.ClassType == "Parallel")
                {
                    EnumFieldDesigner successPolicy = node.Fields[0].Field as EnumFieldDesigner;
                    EnumFieldDesigner failedPolicy  = node.Fields[0].Field as EnumFieldDesigner;

                    Debug__Parallel debug__Parallel = new Debug__Parallel();
                    debug__Parallel.SuccessPolicy = (Debug__Parallel.SUCCESS_POLICY)successPolicy.ValueInt;
                    debug__Parallel.FailurePolicy = (Debug__Parallel.FAILURE_POLICY)failedPolicy.ValueInt;

                    debugNode = debug__Parallel;
                }
                //随机节点
                else if (node.ClassType == "Random")
                {
                    Debug__Random debug__Random = new Debug__Random();
                    debugNode = debug__Random;
                }
                //随机序列
                else if (node.ClassType == "RandomSequence")
                {
                    Debug__RandomSequence__Node debug__RandomSequence__Node = new Debug__RandomSequence__Node();
                    debugNode = debug__RandomSequence__Node;
                }
                //随机选择节点
                else if (node.ClassType == "RandomSelector")
                {
                    Debug__RandomSelector debug__RandomSelector = new Debug__RandomSelector();
                    debugNode = debug__RandomSelector;
                }
                //ifelse节点
                else if (node.ClassType == "IfElse")
                {
                    Debug__IfElse debug__IfElse = new Debug__IfElse();
                    debugNode = debug__IfElse;
                }
                //选择节点
                else if (node.ClassType == "Selector")
                {
                    Debug__Selector debug__Selector = new Debug__Selector();
                    debugNode = debug__Selector;
                }
                //概率选择
                else if (node.ClassType == "RateSelector")
                {
                    Debug__RateSelector debug__RateSelector = new Debug__RateSelector();
                    debugNode = debug__RateSelector;
                }
                else
                {
                    //统一用默认的组合节点,(顺序节点)
                    Debug__Composite debug__Composite = new Debug__Composite();
                    debugNode = debug__Composite;
                }
            }
            //装饰节点
            else if (node.NodeType == NodeType.Decorator)
            {
                //失败节点
                if (node.ClassType == "Failure")
                {
                    Debug__Failure debug__Failure = new Debug__Failure();
                    debugNode = debug__Failure;
                }
                //成功节点
                else if (node.ClassType == "Success")
                {
                    Debug__Success debug__Success = new Debug__Success();
                    debugNode = debug__Success;
                }
                //执行帧节点
                else if (node.ClassType == "Frames")
                {
                    Debug__Frames debug__Frames = new Debug__Frames();
                    debugNode = debug__Frames;
                }
                //输出log节点
                else if (node.ClassType == "Log")
                {
                    Debug__Log debug__Log = new Debug__Log();
                    debugNode = debug__Log;
                }
                //循环节点
                else if (node.ClassType == "Loop")
                {
                    Debug__Loop debug__Loop = new Debug__Loop();
                    debugNode = debug__Loop;
                }
                //取反节点
                else if (node.ClassType == "Not")
                {
                    Debug__Not debug__Not = new Debug__Not();
                    debugNode = debug__Not;
                }
                //指定时间内运行
                else if (node.ClassType == "Time")
                {
                    Debug__Time debug__Time = new Debug__Time();
                    debugNode = debug__Time;
                }
                //等待直到子节点返回成功
                else if (node.ClassType == "WaitUntil")
                {
                    Debug__WaitUntil debug__WaitUntil = new Debug__WaitUntil();
                    debugNode = debug__WaitUntil;
                }
                else
                {
                    Debug__Decorator debug__Decorator = new Debug__Decorator();
                    debug__Decorator.CanChangeStatus = true;
                    debugNode = debug__Decorator;
                }
            }
            //条件节点
            else if (node.NodeType == NodeType.Condition)
            {
                Debug__Condition debug__Condition = new Debug__Condition();
                debug__Condition.CanChangeStatus = true;
                debugNode = debug__Condition;
            }
            //动作节点
            else if (node.NodeType == NodeType.Action)
            {
                //等待一段时间
                if (node.ClassType == "Wait")
                {
                    Debug__Wait debug__Wait = new Debug__Wait();
                    debugNode = debug__Wait;
                }
                //赋值节点Int
                else if (node.ClassType == "AssignmentInt")
                {
                    Debug__AssignmentInt debug__AssignmentInt = new Debug__AssignmentInt();
                    debugNode = debug__AssignmentInt;
                }
                //赋值节点Float
                else if (node.ClassType == "AssignmentFloat")
                {
                    Debug__AssignmentFloat debug__AssignmentFloat = new Debug__AssignmentFloat();
                    debugNode = debug__AssignmentFloat;
                }
                //赋值节点String
                else if (node.ClassType == "AssignmentString")
                {
                    Debug__AssignmentString debug__AssignmentString = new Debug__AssignmentString();
                    debugNode = debug__AssignmentString;
                }
                //空操作节点
                else if (node.ClassType == "Noop")
                {
                    Debug_Noop debug_Noop = new Debug_Noop();
                    debugNode = debug_Noop;
                }
                else
                {
                    Debug__Action debug__Action = new Debug__Action();
                    debugNode = debug__Action;
                    debugNode.CanChangeStatus = true;
                }
            }

            debugNode.Node = node;
            m_Nodes.Add(debugNode);

            for (int i = 0; i < node.Transitions.Count; i++)
            {
                Transition   transition     = node.Transitions[i];
                NodeDesigner childNode      = agent.FindByID(transition.ToNodeID);
                DebugNode    childDebugNode = CreateDebugNode(agent, childNode);
                childDebugNode.ParentNode = debugNode;
                debugNode.Childs.Add(childDebugNode);
            }

            return(debugNode);
        }
        //调试
        public static void DrawNodeToNode_Debug(Graphics graphics, DebugNode fromNode, DebugNode toNode, Vec2 offset)
        {
            Rect fromTitleRect = EditorUtility.GetTitleRect(fromNode.Node);
            Rect toTitleRect   = EditorUtility.GetContentRect(toNode.Node);

            Vec2 fromPoint = EditorUtility.GetRightLinkPoint(fromNode.Node);
            Vec2 toPoint   = EditorUtility.GetLeftLinkPoint(toNode.Node);

            float linkPointHalfSize = EditorUtility.NodeLinkPointSize / 2.0f;

            fromPoint.x += linkPointHalfSize;
            toPoint.x   -= linkPointHalfSize;

            int fromTangentDir = 1;
            int toTangentDir   = -1;

            //求出两点距离
            double distance = (toPoint - fromPoint).magnitude;
            int    num      = (int)Math.Min(distance * 0.5f, 40);

            Vec2 fromTangent = fromPoint;

            fromTangent.x = fromTangent.x + (int)(fromTangentDir * num);

            Vec2 tempPoint = toPoint;

            toPoint.x = toPoint.x + (int)(toTangentDir * EditorUtility.ArrowWidth);
            Vec2 toTangent = toPoint;

            toTangent.x = toTangent.x + (int)(toTangentDir * num);

            float t = toNode.TransitionElapsedTime / DebugManager.TransitionTime;

            Pen   pen   = EditorUtility.Debug_TransitionPen;
            Brush brush = EditorUtility.Debug_TransitionBrush;

            if (toNode.Status == DebugNodeStatus.Transition)
            {
                pen   = EditorUtility.Debug_TransitionPen;
                brush = EditorUtility.Debug_TransitionBrush;
            }
            else if (toNode.Status == DebugNodeStatus.Error)
            {
                pen   = EditorUtility.Debug_Error_TransitionPen;
                brush = EditorUtility.Debug_Error_TransitionBrush;
            }
            else if (toNode.Status == DebugNodeStatus.Running)
            {
                pen   = EditorUtility.Debug_Running_TransitionPen;
                brush = EditorUtility.Debug_Running_TransitionBrush;
            }
            else if (toNode.Status == DebugNodeStatus.Failed)
            {
                pen   = EditorUtility.Debug_Failed_TransitionPen;
                brush = EditorUtility.Debug_Failed_TransitionBrush;
            }
            else if (toNode.Status == DebugNodeStatus.Success)
            {
                pen   = EditorUtility.Debug_Successed_TransitionPen;
                brush = EditorUtility.Debug_Successed_TransitionBrush;
            }

            if (toNode.Status == DebugNodeStatus.Transition && t < 1f)
            {
                Vec2 point = BezierPoint(t, fromPoint, fromTangent, toTangent, toPoint);
                toNode.TransitionPoints.Add(point);

                for (int i = 1; i < toNode.TransitionPoints.Count; i++)
                {
                    graphics.DrawLine(pen, toNode.TransitionPoints[i - 1], toNode.TransitionPoints[i]);
                }
            }
            else
            {
                graphics.DrawBezier(pen, fromPoint, fromTangent, toTangent, toPoint);
                //画箭头
                ArrowPoint[0] = tempPoint;
                ArrowPoint[1] = new PointF(tempPoint.x + (toTangentDir * EditorUtility.ArrowWidth), tempPoint.y + 5);
                ArrowPoint[2] = new PointF(tempPoint.x + (toTangentDir * EditorUtility.ArrowWidth), tempPoint.y - 5);
                graphics.FillPolygon(brush, (PointF[])ArrowPoint);
            }
        }