Exemplo n.º 1
0
    public void Init()
    {
        _filePath         = "Assets/Resources/Data/";
        _fileName         = string.Empty;
        _behaviorTreeData = new BehaviorTreeData();

        BehaviorReadWrite readWrite = new BehaviorReadWrite();

        _globalParameter = readWrite.ReadGlobalParameter(GetGlobalParameterPath());

        behaviorChangeSelectId      += ChangeSelectId;
        behaviorAddNode             += AddNode;
        behaviorDeleteNode          += DeleteNode;
        behaviorLoadFile            += LoadFile;
        behaviorSaveFile            += SaveFile;
        behaviorDeleteFile          += DeleteFile;
        behaviorNodeAddChild        += NodeAddChild;
        behaviorRemoveParentNode    += RemoveParentNode;
        behaviorNodeParameter       += NodeParameterChange;
        globalParameterChange       += GlobalParameterChange;
        behaviorNodeChangeParameter += NodeChangeParameter;
        behaviorRuntimePlay         += RuntimePlay;

        _playState = BehaviorPlayType.STOP;
    }
Exemplo n.º 2
0
        public static void TestDoubleField()
        {
            BehaviorTreeData behaviorTree = new BehaviorTreeData();

            behaviorTree.StartNode = new NodeData();

            DoubleField doubleField = new DoubleField();

            doubleField.FieldName = "DoubleField";
            doubleField.Value     = 100.001d;
            behaviorTree.StartNode.Fields.Add(doubleField);

            RepeatDoubleField repeatDoubleField = new RepeatDoubleField();

            repeatDoubleField.FieldName = "RepeatDoubleField";
            repeatDoubleField.Value     = new List <double>();
            repeatDoubleField.Value.Add(1.1d);
            repeatDoubleField.Value.Add(100.11d);
            repeatDoubleField.Value.Add(1000.11d);
            repeatDoubleField.Value.Add(10000.1119d);
            behaviorTree.StartNode.Fields.Add(repeatDoubleField);

            byte[]           bytes = Serializer.Serialize(behaviorTree);
            BehaviorTreeData deBehaviorTreeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);
        }
Exemplo n.º 3
0
    private void LoadFile(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
        {
            return;
        }

        _configDataDic.Clear();

        BehaviorReadWrite readWrite        = new BehaviorReadWrite();
        BehaviorTreeData  behaviorTreeData = ReadFile(fileName, true);

        if (null == behaviorTreeData)
        {
            //ProDebug.Logger.LogError("file is null:" + fileName);
            return;
        }

        _playState = BehaviorPlayType.STOP;
        NodeNotify.SetPlayState((int)_playState);

        _fileName             = fileName;
        _behaviorTreeData     = behaviorTreeData;
        _currentSelectId      = -1;
        _currentOpenSubTreeId = -1;

        BehaviorRunTime.Instance.Reset(behaviorTreeData);
    }
Exemplo n.º 4
0
        public static void TestIntField()
        {
            BehaviorTreeData behaviorTree = new BehaviorTreeData();

            behaviorTree.StartNode = new NodeData();

            IntField intField = new IntField();

            intField.FieldName = "IntField";
            intField.Value     = 100;
            behaviorTree.StartNode.Fields.Add(intField);

            RepeatIntField repeatIntField = new RepeatIntField();

            repeatIntField.FieldName = "RepeatIntField";
            repeatIntField.Value     = new List <int>();
            repeatIntField.Value.Add(1);
            repeatIntField.Value.Add(100);
            repeatIntField.Value.Add(-1000000);
            repeatIntField.Value.Add(10000);
            behaviorTree.StartNode.Fields.Add(repeatIntField);

            byte[]           bytes = Serializer.Serialize(behaviorTree);
            BehaviorTreeData deBehaviorTreeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);
        }
        /// <summary>
        /// 根据类型获取AgentData
        /// </summary>
        /// <param name="behaviorTreeType">行为树类型</param>
        /// <param name="id">行为树id</param>
        /// <returns></returns>
        public BehaviorTreeElement GetBehaviroTreeData(int behaviorTreeType, string id)
        {
            BehaviorTreeData treeData = null;

            if (!TreeDataDic.TryGetValue(behaviorTreeType, out treeData))
            {
                throw new Exception($"There is no treeData,BehaviorTreeType:{behaviorTreeType}.");
            }

            if (treeData == null)
            {
                return(null);
            }

            for (int i = 0; i < treeData.BehaviorTrees.Count; i++)
            {
                BehaviorTreeElement behaviorTreeElement = treeData.BehaviorTrees[i];
                if (behaviorTreeElement != null && behaviorTreeElement.ID == id)
                {
                    return(behaviorTreeElement);
                }
            }

            string msg = $"There is no AgentData,BehaviorTreeType:{behaviorTreeType} id:{id}.";

            LogError(msg);
            throw new Exception(msg);
        }
Exemplo n.º 6
0
    private void LoadFile(string fileName)
    {
        string path = GetFilePath(fileName);

        if (!File.Exists(path))
        {
            if (!EditorUtility.DisplayDialog("提示", "文件不存在", "yes"))
            {
            }
            return;
        }

        _playState = BehaviorPlayType.STOP;
        BehaviorReadWrite readWrite        = new BehaviorReadWrite();
        BehaviorTreeData  behaviorTreeData = readWrite.ReadJson(path);

        if (null == behaviorTreeData)
        {
            Debug.LogError("file is null:" + fileName);
            return;
        }

        _fileName         = fileName;
        _behaviorTreeData = behaviorTreeData;
    }
Exemplo n.º 7
0
    private BehaviorTreeData StandardID(BehaviorTreeData data)
    {
        data.rootNodeId = IdTransition(data.fileName, data.rootNodeId);
        for (int i = 0; i < data.nodeList.Count; ++i)
        {
            NodeValue nodeValue = data.nodeList[i];

            nodeValue.id = IdTransition(data.fileName, nodeValue.id);
            if (nodeValue.parentNodeID >= 0)
            {
                nodeValue.parentNodeID = IdTransition(data.fileName, nodeValue.parentNodeID);
            }

            if (nodeValue.parentSubTreeNodeId >= 0)
            {
                nodeValue.parentSubTreeNodeId = IdTransition(data.fileName, nodeValue.parentSubTreeNodeId);
            }

            for (int j = 0; j < nodeValue.childNodeList.Count; ++j)
            {
                nodeValue.childNodeList[j] = IdTransition(data.fileName, nodeValue.childNodeList[j]);
            }

            for (int j = 0; j < nodeValue.ifJudgeDataList.Count; ++j)
            {
                nodeValue.ifJudgeDataList[j].nodeId = IdTransition(data.fileName, nodeValue.ifJudgeDataList[j].nodeId);
            }
        }

        return(data);
    }
Exemplo n.º 8
0
    public void SetData(BehaviorTreeData behaviorTreeData)
    {
        BehaviorAnalysis analysis = new BehaviorAnalysis();

        _iconditionCheck = new ConditionCheck();
        _rootNode        = analysis.Analysis(behaviorTreeData, ref _iconditionCheck);
    }
Exemplo n.º 9
0
    private BehaviorTreeData ReadFile(string fileName, bool warningWhenExist)
    {
        BehaviorTreeData behaviorTreeData = null;

        if (_configDataDic.TryGetValue(fileName, out behaviorTreeData))
        {
            return(behaviorTreeData);
        }

        string path = GetFilePath(fileName);

        if (warningWhenExist && !File.Exists(path))
        {
            if (!EditorUtility.DisplayDialog("提示", "文件不存在", "yes"))
            {
            }
            return(null);
        }

        BehaviorReadWrite readWrite = new BehaviorReadWrite();

        behaviorTreeData         = readWrite.ReadJson(path);
        _configDataDic[fileName] = behaviorTreeData;

        LoadAllSubConfig(behaviorTreeData);

        return(behaviorTreeData);
    }
Exemplo n.º 10
0
    private void Analysis(byte[] byteData)
    {
        string           content          = System.Text.Encoding.Default.GetString(byteData);
        BehaviorTreeData behaviorTreeData = JsonMapper.ToObject <BehaviorTreeData>(content);

        _behaviorDic[behaviorTreeData.fileName] = behaviorTreeData;
    }
Exemplo n.º 11
0
    public void Init()
    {
        _filePath         = "Assets/SubAssets/GameData/BehaviorTree/";
        _fileName         = string.Empty;
        _behaviorTreeData = new BehaviorTreeData();
        _runTimeInvalidSubTreeHash.Clear();

        behaviorChangeRootNode         += ChangeRootNode;
        behaviorChangeSelectId         += ChangeSelectId;
        behaviorAddNode                += AddNode;
        behaviorDeleteNode             += DeleteNode;
        behaviorLoadFile               += LoadFile;
        behaviorSaveFile               += SaveFile;
        behaviorDeleteFile             += DeleteFile;
        behaviorNodeAddChild           += NodeAddChild;
        behaviorRemoveParentNode       += RemoveParentNode;
        behaviorNodeParameter          += NodeParameterChange;
        parameterChange                += ParameterChange;
        behaviorNodeChangeParameter    += NodeChangeParameter;
        behaviorRuntimePlay            += RuntimePlay;
        behaviorAddDelConditionGroup   += NodeAddDelConditionGroup;
        behaviorOpenSubTree            += OpenSubTree;
        behaviorChangeSubTreeEntryNode += ChangeSubTreeEntryNode;
        behaviorDeleteSubTreeChild     += DeleteSubTreeChild;
        behaviorReadFile               += ReadFile;
        behaviorStandardID             += StandardID;
        behaviorSelectFile             += SelectFile;
        behaviorSaveSubTree            += SaveSubTree;
        behaviorDebugNodeParentInfo    += DebugNodeParentInfo;

        _currentSelectId      = -1;
        _currentOpenSubTreeId = -1;

        _playState = BehaviorPlayType.STOP;
    }
Exemplo n.º 12
0
        public static void TestFloatField()
        {
            BehaviorTreeData behaviorTree = new BehaviorTreeData();

            behaviorTree.StartNode = new NodeData();

            FloatField floatField = new FloatField();

            floatField.FieldName = "FloatField";
            floatField.Value     = 100.001f;
            behaviorTree.StartNode.Fields.Add(floatField);

            RepeatFloatField repeatFloatField = new RepeatFloatField();

            repeatFloatField.FieldName = "RepeatIntField";
            repeatFloatField.Value     = new List <float>();
            repeatFloatField.Value.Add(1.1f);
            repeatFloatField.Value.Add(100.11f);
            repeatFloatField.Value.Add(1000.11f);
            repeatFloatField.Value.Add(10000.1119f);
            behaviorTree.StartNode.Fields.Add(repeatFloatField);

            byte[]           bytes = Serializer.Serialize(behaviorTree);
            BehaviorTreeData deBehaviorTreeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);
        }
Exemplo n.º 13
0
        public static void TestLongField()
        {
            BehaviorTreeData behaviorTree = new BehaviorTreeData();

            behaviorTree.StartNode = new NodeData();

            LongField longField = new LongField();

            longField.FieldName = "LongField";
            longField.Value     = 100;
            behaviorTree.StartNode.Fields.Add(longField);

            RepeatLongField repeatLongField = new RepeatLongField();

            repeatLongField.FieldName = "RepeatLongField";
            repeatLongField.Value     = new List <long>();
            repeatLongField.Value.Add(1);
            repeatLongField.Value.Add(100);
            repeatLongField.Value.Add(1000);
            repeatLongField.Value.Add(10000);
            behaviorTree.StartNode.Fields.Add(repeatLongField);

            byte[]           bytes = Serializer.Serialize(behaviorTree);
            BehaviorTreeData deBehaviorTreeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);
        }
Exemplo n.º 14
0
    public NodeValue GetNode(int nodeId)
    {
        for (int i = 0; i < NodeList.Count; ++i)
        {
            NodeValue nodeValue = NodeList[i];
            if (nodeValue.id == nodeId)
            {
                return(nodeValue);
            }
        }

        foreach (var kv in _configDataDic)
        {
            BehaviorTreeData treeData = kv.Value;

            for (int i = 0; i < treeData.nodeList.Count; ++i)
            {
                NodeValue nodeValue = treeData.nodeList[i];
                if (nodeValue.id == nodeId)
                {
                    return(nodeValue);
                }
            }
        }

        return(null);
    }
Exemplo n.º 15
0
 /// <summary>
 /// 不要主动调用,通过CharacterMgr.Instance.GetNewCharacte()  创建
 /// </summary>
 public CharacterBase()
 {
     _move              = new CharacterMoveUnit(this);
     _view              = new CharacterView();
     _action            = new CharacterActionUnit(this);
     _dataUnit          = new CharacterDataUnit();
     m_BehaviorTreeData = new BehaviorTreeData();
 }
Exemplo n.º 16
0
        /// <summary>
        /// Generate Code for BehaviorTree
        /// </summary>
        /// <param name="bTree">BehaviorTree </param>
        public void Generate(BehaviorTreeData bTree)
        {
            Reset();
            BehaviorTreeClass btClass = new BehaviorTreeClass(bTree);

            _Document.AddUsingSkillAI();
            _Document.Add(btClass);
            HasPartial = bTree.ExpandMethods;
        }
        /// <summary>
        /// 加载指定类型的行为树
        /// </summary>
        /// <param name="behaviorTreeType">行为树类型</param>
        public void LoadBehaviorData(int behaviorTreeType, byte[] bytes)
        {
            BehaviorTreeData treeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);

            if (treeData == null)
            {
                throw new Exception($"Load {behaviorTreeType} treeData failed.");
            }
            TreeDataDic[behaviorTreeType] = treeData;
        }
Exemplo n.º 18
0
 public BTConcrete(BaseSprite owner, long aiFunction, string aiConfig)
 {
     _owner = owner;
     _data  = DataCenter.behaviorData.GetBehaviorInfo(aiConfig);
     if (null == _data)
     {
         Debug.LogError("AIConfig not find:" + aiConfig);
     }
     SetData(aiFunction, _data, DataCenter.behaviorData.GetBehaviorInfo);
     Init(_owner);
 }
Exemplo n.º 19
0
    public BehaviorTreeData GetBehaviorInfo(string handleFile)
    {
        BehaviorTreeData skillHsmData = null;

        if (_behaviorDic.TryGetValue(handleFile, out skillHsmData))
        {
            return(skillHsmData);
        }

        return(skillHsmData);
    }
Exemplo n.º 20
0
 private static NodeValue GetNode(BehaviorTreeData treeData, int nodeId)
 {
     for (int i = 0; i < treeData.nodeList.Count; ++i)
     {
         NodeValue nodeValue = treeData.nodeList[i];
         if (nodeValue.id == nodeId)
         {
             return(nodeValue);
         }
     }
     return(null);
 }
Exemplo n.º 21
0
 public BehaviorTreeStateNamesClass(BehaviorTreeData tree)
     : base("StateNames")
 {
     base.ClassModifier = ClassModifiers.Static;
     base.IsPartial     = false;
     foreach (var s in tree.States)
     {
         Add(new Variable("string", s.Name, string.Format("\"{0}\"", s.Name))
         {
             IsStatic = true, Modifier = Modifiers.Public
         });
     }
 }
Exemplo n.º 22
0
    private void Analysis(byte[] byteData)
    {
        string           content          = System.Text.Encoding.Default.GetString(byteData);
        BehaviorTreeData behaviorTreeData = JsonMapper.ToObject <BehaviorTreeData>(content);

        _behaviorDic[behaviorTreeData.fileName] = behaviorTreeData;

        for (int i = 0; i < behaviorTreeData.nodeList.Count; ++i)
        {
            NodeValue nodeValue = behaviorTreeData.nodeList[i];
            behaviorTreeData.nodeDic.Add(nodeValue.id, nodeValue);
        }
    }
Exemplo n.º 23
0
    private void SaveFile(string fileName, BehaviorTreeData data)
    {
        if (data == null)
        {
            //ProDebug.Logger.LogError("rootNode is null");
            return;
        }

        if (string.IsNullOrEmpty(fileName))
        {
            if (EditorUtility.DisplayDialog("警告", "文件名不能为空", "确定"))
            {
                return;
            }
        }

        if (fileName.Length > 30)
        {
            if (EditorUtility.DisplayDialog("警告", "文件名过长,不能大于20个字符", "确定"))
            {
                return;
            }
        }

        string path = GetFilePath(fileName);

        if (File.Exists(path))
        {
            if (!EditorUtility.DisplayDialog("已存在文件", "是否替换新文件", "替换", "取消"))
            {
                return;
            }
        }

        // 如果项目总不包含该路径,创建一个
        string directory = Path.GetDirectoryName(path);

        if (!Directory.Exists(directory))
        {
            Directory.CreateDirectory(directory);
        }

        data.nodeDic.Clear();

        StandardID(data);
        data.fileName = fileName;

        BehaviorReadWrite readWrite = new BehaviorReadWrite();

        readWrite.WriteJson(data, path);
    }
Exemplo n.º 24
0
    private void SaveSubTree(string subTreeConfigName, int subTreeNodeId)
    {
        //ProDebug.Logger.LogError("SaveSubTree:" + subTreeConfigName + "     " + subTreeNodeId);

        NodeValue subTreeNode = GetNode(subTreeNodeId);

        if (null == subTreeNode || subTreeNode.NodeType != (int)NODE_TYPE.SUB_TREE)
        {
            return;
        }
        if (subTreeNode.subTreeType != (int)SUB_TREE_TYPE.NORMAL)
        {
            return;
        }

        BehaviorTreeData subTreeData = new BehaviorTreeData();

        subTreeData.fileName = subTreeConfigName;

        List <NodeValue> nodeList = new List <NodeValue>();

        FindChild(_behaviorTreeData, subTreeNodeId, ref nodeList);

        List <NodeValue> newNodeList = new List <NodeValue>();

        for (int i = 0; i < nodeList.Count; ++i)
        {
            NodeValue childNode    = nodeList[i];
            NodeValue newNodeValue = childNode.Clone();

            if (newNodeValue.subTreeEntry)
            {
                newNodeValue.isRootNode   = true;
                newNodeValue.parentNodeID = -1;
                subTreeData.rootNodeId    = newNodeValue.id;
            }
            newNodeValue.parentSubTreeNodeId = -1;

            subTreeData.nodeList.Add(newNodeValue);
        }

        for (int i = 0; i < _behaviorTreeData.parameterList.Count; ++i)
        {
            BehaviorParameter parameter = _behaviorTreeData.parameterList[i];
            subTreeData.parameterList.Add(parameter.Clone());
        }

        subTreeData = StandardID(subTreeData);

        SaveFile(subTreeConfigName, subTreeData);
    }
Exemplo n.º 25
0
    private void LoadAllSubConfig(BehaviorTreeData treeData)
    {
        for (int i = 0; i < treeData.nodeList.Count; ++i)
        {
            NodeValue nodeVlaue = treeData.nodeList[i];
            if (nodeVlaue.NodeType == (int)NODE_TYPE.SUB_TREE && nodeVlaue.subTreeType == (int)SUB_TREE_TYPE.CONFIG)
            {
                BehaviorTreeData subTreeData = ReadFile(nodeVlaue.subTreeConfig, false);
                _configDataDic[nodeVlaue.subTreeConfig] = subTreeData;

                LoadAllSubConfig(subTreeData);
            }
        }
    }
Exemplo n.º 26
0
        public static void TestBooleanField()
        {
            BehaviorTreeData behaviorTree = new BehaviorTreeData();

            behaviorTree.StartNode = new NodeData();

            BooleanField booleanField = new BooleanField();

            booleanField.FieldName = "BooleanField";
            booleanField.Value     = true;
            behaviorTree.StartNode.Fields.Add(booleanField);

            byte[]           bytes = Serializer.Serialize(behaviorTree);
            BehaviorTreeData deBehaviorTreeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);
        }
Exemplo n.º 27
0
        public static void TestEnumField()
        {
            BehaviorTreeData behaviorTree = new BehaviorTreeData();

            behaviorTree.StartNode = new NodeData();

            EnumField enumField = new EnumField();

            enumField.FieldName = "EnumField";
            enumField.Value     = 100;
            behaviorTree.StartNode.Fields.Add(enumField);

            byte[]           bytes = Serializer.Serialize(behaviorTree);
            BehaviorTreeData deBehaviorTreeData = Serializer.DeSerialize <BehaviorTreeData>(bytes);
        }
Exemplo n.º 28
0
    private IEnumerator Delay()
    {
        yield return(new WaitForSeconds(0.5f));

        GameObject target = GameObject.CreatePrimitive(PrimitiveType.Capsule);

        target.name = "Human";
        target.transform.position = Vector3.zero;

        kitchen     = Create("厨房", new Vector3(0, 0, 0), new Vector3(90, 0, 0));
        diningTable = Create("餐桌", new Vector3(5, 0, 0), new Vector3(90, 0, 0));
        TV          = Create("电视", new Vector3(0, 0, 5), new Vector3(90, 0, 0));

        _human = new Human(target.transform, kitchen.gameObject, diningTable.gameObject, TV.gameObject);
        BehaviorTreeData behaviorTreeData = behaviorData.GetBehaviorInfo("Human");

        _human.SetData(behaviorTreeData);
    }
Exemplo n.º 29
0
    private static void FindChild(BehaviorTreeData treeData, int nodeId, ref List <NodeValue> nodeList)
    {
        NodeValue nodeValue = GetNode(treeData, nodeId);

        if (null == nodeValue)
        {
            return;
        }

        for (int i = 0; i < nodeValue.childNodeList.Count; ++i)
        {
            int       childId   = nodeValue.childNodeList[i];
            NodeValue childNode = GetNode(treeData, childId);
            if (null != childNode)
            {
                nodeList.Add(childNode);
            }
            FindChild(treeData, childNode.id, ref nodeList);
        }
    }
Exemplo n.º 30
0
    public void Init()
    {
        _filePath         = "Assets/SubAssets/GameData/BehaviorTree/";
        _fileName         = string.Empty;
        _behaviorTreeData = new BehaviorTreeData();

        behaviorChangeSelectId       += ChangeSelectId;
        behaviorAddNode              += AddNode;
        behaviorDeleteNode           += DeleteNode;
        behaviorLoadFile             += LoadFile;
        behaviorSaveFile             += SaveFile;
        behaviorDeleteFile           += DeleteFile;
        behaviorNodeAddChild         += NodeAddChild;
        behaviorRemoveParentNode     += RemoveParentNode;
        behaviorNodeParameter        += NodeParameterChange;
        parameterChange              += ParameterChange;
        behaviorNodeChangeParameter  += NodeChangeParameter;
        behaviorRuntimePlay          += RuntimePlay;
        behaviorAddDelConditionGroup += NodeAddDelConditionGroup;

        _playState = BehaviorPlayType.STOP;
    }