Example #1
0
        /// <summary>
        /// 注册Proxy
        /// </summary>
        /// <param name="classType">节点类</param>
        /// <param name="nodeType">节点类型</param>
        /// <param name="needUpdate">是否执行proxy的update(优化性能)</param>
        public void Register(string classType, ENodeType nodeType, bool needUpdate)
        {
            if (string.IsNullOrEmpty(classType))
            {
                string msg = "LuaProxyManager.Register() \n classType is null.";
                BehaviorTreeManager.Instance.LogError(msg);
                throw new Exception(msg);
            }

            if (m_ProxyDic.ContainsKey(classType))
            {
                string msg = $"LuaProxyManager.Register() \n m_ProxyDic already Contain key {classType}.";
                BehaviorTreeManager.Instance.LogError(msg);
                throw new Exception(msg);
            }

            ProxyData proxyData = new ProxyData();

            proxyData.ClassType  = classType;
            proxyData.NodeType   = nodeType;
            proxyData.NeedUpdate = needUpdate;

            m_ProxyDic.Add(classType, proxyData);
        }
        /// <summary>
        /// 构建节点
        /// </summary>
        /// <param name="nodeData">节点数据</param>
        public BaseNode CreateNode(NodeData nodeData)
        {
            if (nodeData == null)
            {
                string msg = "BehaviorTreeManager.CreateNode() \n nodeData is null.";
                LogError(msg);
                throw new Exception(msg);
            }

            ProxyData proxyData = null;

            for (int i = 0; i < m_ProxyManagers.Count; i++)
            {
                IProxyManager proxyManager = m_ProxyManagers[i];
                proxyData = proxyManager.GetProxyData(nodeData.ClassType);
                if (proxyData != null)
                {
                    break;
                }
            }

            if (proxyData == null)
            {
                string msg = "BehaviorTreeManager.CreateNode() \n proxyData is null.";
                LogError(msg);
                throw new Exception(msg);
            }

            BaseNode baseNode = null;

            switch (proxyData.NodeType)
            {
            case ENodeType.Action:
                ActionNode actionNode = new ActionNode();
                actionNode.NodeType = ENodeType.Action;
                baseNode            = actionNode;
                break;

            case ENodeType.Composite:
                CompositeNode compositeNode = new CompositeNode();
                compositeNode.NodeType = ENodeType.Composite;
                baseNode = compositeNode;
                break;

            case ENodeType.Condition:
                ConditionNode conditionNode = new ConditionNode();
                conditionNode.NodeType = ENodeType.Condition;
                baseNode = conditionNode;
                break;

            case ENodeType.Decorator:
                DecorateNode decorateNode = new DecorateNode();
                decorateNode.NodeType = ENodeType.Condition;
                baseNode = decorateNode;
                break;
            }

            if (baseNode == null)
            {
                //组合节点必须有子节点
                string msg = $"CreateNode {proxyData.ClassType} Failed";
                LogError(msg);
                throw new Exception(msg);
            }

            baseNode.SetData(nodeData);
            baseNode.SetProxyData(proxyData);

            if (baseNode is CompositeNode)
            {
                if (nodeData.Childs == null || nodeData.Childs.Count == 0)
                {
                    //组合节点必须有子节点
                    string msg = $"{proxyData.NodeType} node must have child nodes";
                    LogError(msg);
                    throw new Exception(msg);
                }

                CompositeNode compositeNode = baseNode as CompositeNode;
                for (int i = 0; i < nodeData.Childs.Count; i++)
                {
                    NodeData childNodeData = nodeData.Childs[i];
                    BaseNode childNode     = CreateNode(childNodeData);
                    compositeNode.AddChild(childNode);
                }
            }

            return(baseNode);
        }
Example #3
0
 internal void SetProxyData(ProxyData proxyData)
 {
     ProxyData = proxyData;
 }