public BehaviourInverterNode(Hashtable options, IBehaviourEmployee employee)
            : base(employee)
        {
            Hashtable table = options["child"] as Hashtable;

            mChild = BehaviourNode.CreateNode(table, employee);
        }
Esempio n. 2
0
        public BehaviourActionNode(Hashtable options, IBehaviourEmployee employee)
            : base(employee)
        {
            mActionFunctor = options["action"] as string;

            // 解析是否随机第一次启动
            string randomFirstString = options["randomfirst"] as string;

            if (randomFirstString != null && randomFirstString == "true")
            {
                mIsRandomFirstRunTime = true;
            }
            else
            {
                mIsRandomFirstRunTime = false;
            }
            mIsRandomFirstRunTimeBak = mIsRandomFirstRunTime;

            // 解析间隔时间
            string intervalTime = options["interval"] as string;

            if (!String.IsNullOrEmpty(intervalTime))
            {
                string[] timesStr = intervalTime.Split('~');

                if (timesStr.Length > 1)
                {
                    mBeginIntervalTime = (float)(Convert.ToDouble(timesStr[0]));
                    mEndIntervalTime   = (float)(Convert.ToDouble(timesStr[1]));

                    mContinuousExecutionConstIntervalTime = UnityEngine.Random.Range(mBeginIntervalTime, mEndIntervalTime);
                }
                else
                {
                    mContinuousExecutionConstIntervalTime = (float)(Convert.ToDouble(intervalTime));
                }
            }
            else
            {
                mContinuousExecutionConstIntervalTime = 0f;
            }
            mContinuousExecutionConstIntervalTimeBak = mContinuousExecutionConstIntervalTime;

            if (mIsRandomFirstRunTime)
            {
                mFirstRunTime = UnityEngine.Random.Range(0, mContinuousExecutionConstIntervalTime);
            }

            Hashtable table = options["intervalaction"] as Hashtable;

            if (table != null)
            {
                mIntervalAction = BehaviourNode.CreateNode(table, employee);
            }

            // 解析參數
            base.ParseParamters(options);
        }
Esempio n. 3
0
        public static BehaviourNode CreateNode(Hashtable options, IBehaviourEmployee employee)
        {
            if (options == null)
            {
                return(null);
            }

            BehaviourNode behaviour = null;

            string typeStr = options["type"] as string;
            string comment = options["comment"] as string;

            // Sequence
            if (typeStr == "Sequence")
            {
                behaviour = new BehaviourSequenceNode(options, employee);
            }
            else if (typeStr == "Selector")
            {
                behaviour = new BehaviourSelectorNode(options, employee);
            }
            // Action
            else if (typeStr == "Action")
            {
                behaviour = new BehaviourActionNode(options, employee);
            }
            // Conditional
            else if (typeStr == "Conditional")
            {
                behaviour = new BehaviourConditionalNode(options, employee);
            }
            else if (typeStr == "Inverter")
            {
                behaviour = new BehaviourInverterNode(options, employee);
            }
            else if (typeStr == "Random")
            {
                behaviour = new BehaviourRandomNode(options, employee);
            }
            else
            {
                return(null);
            }

            if (behaviour != null)
            {
                // 获取注释
                behaviour.Comment = comment;
            }

            return(behaviour);
        }
Esempio n. 4
0
        public BehaviourSelectorNode(Hashtable options, IBehaviourEmployee employee)
            : base(employee)
        {
            ArrayList queue = options["queue"] as ArrayList;

            if (queue == null)
            {
                return;
            }

            for (int i = 0; i < queue.Count; i++)
            {
                BehaviourNode node = BehaviourNode.CreateNode(queue[i] as Hashtable, employee);
                mChildren.Add(node);
            }
        }
Esempio n. 5
0
        public BehaviourRandomNode(Hashtable options, IBehaviourEmployee employee)
            : base(employee)
        {
            Hashtable table = options["child"] as Hashtable;

            string rawProbability = options["probability"] as string;

            if (!string.IsNullOrEmpty(rawProbability))
            {
                mProbability = System.Convert.ToSingle(rawProbability);
            }
            else
            {
                mProbability = 1.0f;
            }

            mChild = BehaviourNode.CreateNode(table, employee);
        }
Esempio n. 6
0
 public BehaviourTree(string name, Hashtable options, IBehaviourEmployee employee)
 {
     mName     = name;
     mEmployee = employee;
     mRootNode = BehaviourNode.CreateNode(options, employee);
 }
Esempio n. 7
0
        public BehaviourConditionalNode(Hashtable options, IBehaviourEmployee employee)
            : base(employee)
        {
            if (options == null)
            {
                return;
            }

            string conditionsString = options["condition"] as string;

            if (!String.IsNullOrEmpty(conditionsString))
            {
                string[] conditions = conditionsString.Split('|');
                for (int i = 0; i < conditions.Length; i++)
                {
                    string temp = conditions[i];

                    if (temp == null || temp == string.Empty)
                    {
                        continue;
                    }

                    // 試圖切割字符串,查看其是表達式還是函數
                    string[] condition = Regex.Split(conditions[i].Trim(), mSplitRegularPattern, RegexOptions.None);

                    if (condition.Length == 1)
                    {
                        Condition con = new Condition();
                        con.IsNot = false;

                        // 先判断是否取反
                        string method    = condition[0];
                        char   firstChar = method[0];

                        if (firstChar == '!')
                        {
                            con.IsNot = true;
                            method    = method.Substring(1, method.Length - 1);
                        }

                        //                     MethodInfo functor = objType.GetMethod(method);
                        //
                        //                     if (functor == null)
                        //                     {
                        //                         GameDebug.LogError(string.Format("{0} Conditional::Conditional can not get method:{1}", _Comment,  method));
                        //                     }

                        con.FunctorByString = method;
                        //con._Functor = functor;
                        con.Type = EConditionType.Function;

                        mConditions.AddLast(con);
                    }
                    else if (condition.Length >= 5)
                    {
                        Condition con = new Condition();
                        con.Type       = EConditionType.Expression;
                        con.LeftValue  = condition[1];
                        con.Operator   = condition[2];
                        con.RightValue = condition[3];

                        mConditions.AddLast(con);
                    }
                    else
                    {
                        // Error
                        //GameDebug.LogError("Conditional::Conditional unknown string:" + conditionsString);
                    }
                }
            }

            Hashtable table = options["true"] as Hashtable;

            mTrueBehavior = BehaviourNode.CreateNode(table, employee);

            table          = options["false"] as Hashtable;
            mFalseBehavior = BehaviourNode.CreateNode(table, employee);

            string operatorString = options["operator"] as string;

            if (operatorString != null && operatorString.ToLower() == "or")
            {
                mOperator = EOperator.Or;
            }
            else
            {
                mOperator = EOperator.And;
            }

            // 解析调用参数
            base.ParseParamters(options);

            // 解析缓冲模式
            string cacheModeString = options["cache"] as string;

            if (cacheModeString != null && cacheModeString.ToLower() == "true")
            {
                mIsCacheTurnOn = true;
            }
            else
            {
                mIsCacheTurnOn = false;
            }

            if (mIsCacheTurnOn)
            {
                string refreshTimeString = options["refresh"] as string;

                if (refreshTimeString == null)
                {
                    mIsCacheTurnOn = false;
                    return;
                }

                string[] timesStr = refreshTimeString.Split('~');

                if (timesStr.Length > 1)
                {
                    float begin = (float)(Convert.ToDouble(timesStr[0]));
                    float end   = (float)(Convert.ToDouble(timesStr[1]));

                    mConstRefreshTime = UnityEngine.Random.Range(begin, end);
                }
                else
                {
                    mConstRefreshTime = (float)(Convert.ToDouble(refreshTimeString));
                }
            }
        }