行为树 Behaviourtree

using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
using UnityEditor;

[CreateAssetMenu()]
public class BehaviourTree : ScriptableObject
{

    public Node rootNode;

    public Node.State treeState = Node.State.Running;
    public List<Node> nodes = new List<Node>();
    public Blackboard blackboard = new Blackboard();

    public Node.State Update()
    {
        if (rootNode.state==Node.State.Running)
        {
            treeState = rootNode.Update();
        }
        return treeState;
    }
# if UNITY_EDITOR
    public Node CreateNode(System.Type type)
    {

        Node node=ScriptableObject.CreateInstance(type) as Node;
        node.name = type.Name;
        node.guid = GUID.Generate().ToString();

        Undo.RecordObject(this, "Behaviour Tree (CreateNode)");

        nodes.Add(node);
        if (!Application.isPlaying)
        {
            AssetDatabase.AddObjectToAsset(node, this);
        }
        //AssetDatabase.AddObjectToAsset(node, this);
        Undo.RegisterCreatedObjectUndo(node, "Behaviour Tree (CreateNode)");


        AssetDatabase.SaveAssets();

        return node;
    }
    public void DeleteNode(Node node)
    {
        Undo.RecordObject(this, "Behaviour Tree (DeleteNode)");

        nodes.Remove(node);
        //AssetDatabase.RemoveObjectFromAsset(node);
        Undo.DestroyObjectImmediate(node);
        AssetDatabase.SaveAssets();
    }

    public void AddChild(Node parent,Node child)
    {
        DecotatorNode decotator = parent as DecotatorNode;
        if (decotator)
        {
            Undo.RecordObject(decotator, "Behaviour Tree (AddChild)");
            decotator.child = child;
            EditorUtility.SetDirty(decotator);
        }
        RootNode rootNode = parent as RootNode;
        if (rootNode)
        {
            Undo.RecordObject(rootNode, "Behaviour Tree (AddChild)");

            rootNode.child = child;
            EditorUtility.SetDirty(rootNode);

        }
        CompositeNode composite  = parent as CompositeNode;
        if (composite)
        {
            Undo.RecordObject(composite, "Behaviour Tree (AddChild)");
            composite.children.Add (child);
            EditorUtility.SetDirty(composite);

        }

    }
    public void RemoveChild(Node parent, Node child)
    {
        DecotatorNode decotator = parent as DecotatorNode;
        if (decotator)
        {
            Undo.RecordObject(decotator, "Behaviour Tree (RemoveChild)");
            decotator.child = null;
            EditorUtility.SetDirty(decotator);
        }
        RootNode rootNode = parent as RootNode;
        if (rootNode)
        {
            Undo.RecordObject(rootNode, "Behaviour Tree (RemoveChild)");
            rootNode.child = null;
            EditorUtility.SetDirty(rootNode);
        }
        CompositeNode composite = parent as CompositeNode;
        if (composite)
        {
            Undo.RecordObject(composite, "Behaviour Tree (RemoveChild)");
            composite.children.Remove(child);
            EditorUtility.SetDirty(composite);

        }

    }
    public List<Node> GetChildren(Node parent)
    {
        List<Node> children = new List<Node>();

        DecotatorNode decotator = parent as DecotatorNode;
        if (decotator&& decotator.child!=null)
        {
            children.Add(decotator.child);
        }
        RootNode rootNode = parent as RootNode;
        if (rootNode&&rootNode.child!=null)
        {
            children.Add(rootNode.child);
        }

        CompositeNode composite = parent as CompositeNode;
        if (composite)
        {
            return composite.children;
        }

        return children;
    }
#endif

    public void Traverse(Node node,System.Action<Node> visiter)
    {
        if (node)
        {
            visiter.Invoke(node);
            var children = GetChildren(node);
            children.ForEach((n) => Traverse(n, visiter));
        }
    }
    public BehaviourTree Clone()
    {
        BehaviourTree behaviourTree = Instantiate(this);
        behaviourTree.rootNode = behaviourTree.rootNode.Clone();
        behaviourTree.nodes = new List<Node>();
        Traverse(behaviourTree.rootNode, (n) =>
        {
            behaviourTree.nodes.Add(n);
        });
        behaviourTree.rootNode = behaviourTree.rootNode.Clone();
        return behaviourTree;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值