public void Sample()
        {
            UInt32 stackCapacity = 1;
            IStack <SequentialNode <String> > stack = new SequentialStack <String>(stackCapacity);

            Int32 cycleNumber = 10;
            Int32 popNumber   = cycleNumber / 2;
            SequentialNode <String> tmpSequentialNode = null;

            for (Int32 i = 0; i < cycleNumber; i++)
            {
                stack.Push(new SequentialNode <String>()
                {
                    Data = i.ToString()
                });
                if (i >= popNumber)
                {
                    stack.Pop();
                }
                else
                {
                    tmpSequentialNode = stack.Get();
                    Console.WriteLine($"SequentialNode data is 【{tmpSequentialNode.Data}】");
                }
            }

            Console.WriteLine($"【{MethodBase.GetCurrentMethod().DeclaringType.FullName}】 end.");
        }
Ejemplo n.º 2
0
    public EnemyBehaviour()
    {
        root = new ParralelNode <EnemyBehaviour>();

        var displacementNode = new SequentialNode <EnemyBehaviour>();

        m_findNode = new FindPlayerNode();
        m_moveNode = new MoveAtPlayerNode();
        //displacementNode.addNode(m_findNode);
        displacementNode.addNode(m_moveNode);

        root.addNode(m_findNode);
        root.addNode(displacementNode);
        root.addNode(new AimAtPlayerNode());
    }
Ejemplo n.º 3
0
    public void TestNodeSequenceSuccess()
    {
        var t1 = new TestNode();
        var t2 = new TestNode();
        var s  = new SequentialNode();

        s.children.Add(t1);
        s.children.Add(t2);

        t1.mock_status = BHS.RUNNING;
        t2.mock_status = BHS.RUNNING;

        AssertEqual(s.Run(), BHS.RUNNING);
        AssertEqual(t1.inits, 1);
        AssertEqual(t1.execs, 1);
        AssertEqual(t1.deinits, 0);
        AssertEqual(t1.defers, 0);
        AssertEqual(t2.inits, 0);
        AssertEqual(t2.execs, 0);
        AssertEqual(t2.deinits, 0);
        AssertEqual(t2.defers, 0);

        t1.mock_status = BHS.SUCCESS;
        t2.mock_status = BHS.RUNNING;

        AssertEqual(s.Run(), BHS.RUNNING);
        AssertEqual(t1.inits, 1);
        AssertEqual(t1.execs, 2);
        AssertEqual(t1.deinits, 1);
        AssertEqual(t1.defers, 0);
        AssertEqual(t2.inits, 1);
        AssertEqual(t2.execs, 1);
        AssertEqual(t2.deinits, 0);
        AssertEqual(t2.defers, 0);

        t1.mock_status = BHS.SUCCESS;
        t2.mock_status = BHS.SUCCESS;

        AssertEqual(s.Run(), BHS.SUCCESS);
        AssertEqual(t1.inits, 1);
        AssertEqual(t1.execs, 2);
        AssertEqual(t1.deinits, 1);
        AssertEqual(t1.defers, 1);
        AssertEqual(t2.inits, 1);
        AssertEqual(t2.execs, 2);
        AssertEqual(t2.deinits, 1);
        AssertEqual(t2.defers, 1);
    }
Ejemplo n.º 4
0
    protected override ITask GetBrain()
    {
        SequentialNode wander = SequentialNode.Create(2)
                                .Load(new MoveToRandom())
                                .Load(new WaitForNavStrict());

        ParallelNode wanderControl = ParallelNode.Create(2, CompletionPolicy.Any)
                                     .Load(new UntilUserControlled())
                                     .Load(wander);

        mUserControl = new ManualController();

        PrioritySelector main = PrioritySelector.Create(2)
                                .Load(IsUserControlled.Instance, mUserControl)
                                .Load(TrueCondition.Instance, wanderControl);

        SequentialNode root = SequentialNode.Create(2)
                              .Load(new SetUseCrowd(true))
                              .Load(main);

        ITask result = new KeepActive(root);

        return(result);
    }
Ejemplo n.º 5
0
        /// <summary>
        /// Add a data element to the stack.
        /// </summary>
        /// <param name="element">value of data element</param>
        /// <returns>
        /// <c>true</c> The operation was successful.
        /// <c>false</c> The operation failed.
        /// </returns>
        public bool Push(SequentialNode <T> element)
        {
            bool result = false;

            if (null != element)
            {
                if (m_dataElementNumber != m_sequentialNodes.Length)
                {
                    m_sequentialNodes[m_dataElementNumber] = element;
                    m_dataElementNumber++;
                    result = true;
                }
                else
                {
                    //扩容
                    var tmp = m_sequentialNodes;
                    m_sequentialNodes = new SequentialNode <T> [2 * m_dataElementNumber];
                    tmp.CopyTo(m_sequentialNodes, 0);
                    result = Push(element);
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        private Int32 GetValueFromPostfixExpression(String postfixExpression)
        {
            IStack <SequentialNode <Int32> > stack = new SequentialStack <Int32>();

            Int32         length    = postfixExpression.Length;
            StringBuilder sbElement = new StringBuilder();

            for (Int32 i = 0; i < length; i++)
            {
                Char tmpCharExpression = postfixExpression[i];
                //方法一(当前实现)
                //判断是否是操作数之间的分隔符,如果是则将之前的字符串认为是一个操作数(例如:110)
                //方法二
                //也可以通过栈中的元素*10,相加之和再入栈
                if (m_split.Equals(tmpCharExpression))
                {
                    if (!String.IsNullOrWhiteSpace(sbElement.ToString()))
                    {
                        stack.Push(
                            new SequentialNode <Int32>()
                        {
                            Data = Int32.Parse(sbElement.ToString())
                        }
                            );
                        sbElement = new StringBuilder();
                    }
                    continue;
                }
                //当出现运算符时,弹出栈里面的数字进行运算
                if (Char.IsNumber(tmpCharExpression))
                {
                    sbElement.Append(tmpCharExpression.ToString());
                }
                else if (stack.Count > 0)
                {
                    SequentialNode <Int32> node1 = stack.Pop();
                    SequentialNode <Int32> node2 = stack.Pop();
                    Int32 result = 0;
                    switch (tmpCharExpression)
                    {
                    case '+':
                        result = node2.Data + node1.Data;
                        break;

                    case '-':
                        result = node2.Data - node1.Data;
                        break;

                    case '*':
                        result = node2.Data * node1.Data;
                        break;

                    case '/':
                        if (0 == node1.Data)
                        {
                            throw new Exception("Zero cannot be used as a divisor.");
                        }
                        result = node2.Data / node1.Data;
                        break;
                    }
                    stack.Push(new SequentialNode <Int32>()
                    {
                        Data = result
                    });
                }
            }

            return(stack.Pop().Data);
        }