예제 #1
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUILayout.Button("Refresh behavior options") || !_initialized)
            {
                types       = BehaviorTreeUtil.GetClasses();
                listOptions = new string[types.Length];
                for (int i = 0; i < types.Length; i++)
                {
                    if (!_initialized)
                    {
                        if (target is GraphLeaf)
                        {
                            GraphLeaf leaf = target as GraphLeaf;
                            if (leaf.sourceNode.NodeBehavior != null)
                            {
                                if (types[i] == leaf.sourceNode.NodeBehavior.GetType())
                                {
                                    _choiceIndex = i;
                                }
                            }
                        }
                    }
                    listOptions[i] = types[i].ToString();
                }

                if (!_initialized)
                {
                    _initialized = true;
                }
            }

            if (listOptions != null)
            {
                if (listOptions.Length > 0)
                {
                    _choiceIndex = EditorGUILayout.Popup(_choiceIndex, listOptions);
                }
            }

            if (target is GraphLeaf && 0 <= _choiceIndex && _choiceIndex < listOptions.Length && types != null)
            {
                GraphLeaf leaf = target as GraphLeaf;

                if (leaf.sourceNode.NodeBehavior == null)
                {
                    SetBehaviorType(ref leaf.sourceNode, types[_choiceIndex]);
                }
                else if (leaf.sourceNode.NodeBehavior.GetType() != types[_choiceIndex])
                {
                    SetBehaviorType(ref leaf.sourceNode, types[_choiceIndex]);
                }
            }
        }
예제 #2
0
        public static NodeInfo NewLeaf()
        {
            GraphLeaf leaf = ScriptableObject.CreateInstance <GraphLeaf>();

            leaf.Label = "Leaf";
            Slot i = leaf.AddInputSlot("in");

            //leaf.AddProperty(new Property(typeof(AI.Behavior), "Behavior"));

            return(new NodeInfo(leaf, i));
        }
예제 #3
0
        protected virtual Slot CreateNode(AI.Node n, Vector2 pos, bool saveToDatabase = true)
        {
            if (n is AI.Root)
            {
                AI.Root   r        = n as AI.Root;
                GraphRoot dispNode = BehaviorNode.NewRoot().node as GraphRoot;
                if (saveToDatabase)
                {
                    SaveGraphNodeAsset(dispNode, n.name);
                }
                dispNode.sourceNode        = r;
                dispNode.position.position = pos;

                if (r.NextNode)
                {
                    Slot nextInputSlot = CreateNode(r.NextNode, pos + new Vector2(150, 0), saveToDatabase);

                    Connect(dispNode.slots[0], nextInputSlot);
                }

                if (_rootNode)
                {
                    Debug.LogWarning("Tree Graph seems to already have root node? Overwriting.");
                }
                _rootNode = dispNode;

                AddNode(dispNode);
                return(null);
            }
            else if (n is AI.Selector)
            {
                AI.Selector   sel      = n as AI.Selector;
                NodeInfo      nodeInfo = BehaviorNode.NewSelector();
                GraphSelector dispNode = nodeInfo.node as GraphSelector;
                if (saveToDatabase)
                {
                    SaveGraphNodeAsset(dispNode, n.name);
                }
                dispNode.sourceNode        = sel;
                dispNode.position.position = pos;

                if (sel.Logic != null)
                {
                    dispNode.PropertyOneToEvaluate = sel.Logic.PropertyOneToEvaluate;

                    string propertyTwo = sel.Logic.PropertyTwoToEvaluate;
                    if (propertyTwo[0] == '(')
                    {
                        int typeEndIndex = propertyTwo.IndexOf(')');
                        switch (propertyTwo.Substring(0, typeEndIndex + 1))
                        {
                        case "(bool)":
                        {
                            dispNode.PropertyTwoToEvaluate = propertyTwo.Substring(typeEndIndex + 1);
                            dispNode.PropertyTwoType       = GraphSelector.PropertyType.BOOL;
                            break;
                        }

                        case "(float)":
                        {
                            dispNode.PropertyTwoToEvaluate = propertyTwo.Substring(typeEndIndex + 1);
                            dispNode.PropertyTwoType       = GraphSelector.PropertyType.FLOAT;
                            break;
                        }

                        case "(int)":
                        {
                            dispNode.PropertyTwoToEvaluate = propertyTwo.Substring(typeEndIndex + 1);
                            dispNode.PropertyTwoType       = GraphSelector.PropertyType.INT;
                            break;
                        }

                        default:
                        {
                            Debug.LogWarning("Logic property 2 starts with \'(\' but isn't a type. Try not to use parantheses in property names.");
                            dispNode.PropertyTwoToEvaluate = propertyTwo;
                            dispNode.PropertyTwoType       = GraphSelector.PropertyType.BLACKBOARD;
                            break;
                        }
                        }
                    }
                    else
                    {
                        dispNode.PropertyTwoType       = GraphSelector.PropertyType.BLACKBOARD;
                        dispNode.PropertyTwoToEvaluate = sel.Logic.PropertyTwoToEvaluate;
                    }

                    dispNode.Mode = sel.Logic.Mode;
                }

                Slot o = dispNode.AddOutputSlot("true:");
                if (sel.nodeOnTrue)
                {
                    Slot nextInputSlot = CreateNode(sel.nodeOnTrue, pos + new Vector2(150, -50), saveToDatabase);
                    Connect(o, nextInputSlot);
                }

                o = dispNode.AddOutputSlot("false:");
                if (sel.nodeOnFalse)
                {
                    Slot nextInputSlot = CreateNode(sel.nodeOnFalse, pos + new Vector2(150, 50), saveToDatabase);
                    Connect(o, nextInputSlot);
                }

                AddNode(dispNode);
                return(nodeInfo.inSlot);
            }
            else if (n is AI.Sequence)
            {
                AI.Sequence   seq      = n as AI.Sequence;
                NodeInfo      nodeInfo = BehaviorNode.NewSequence();
                GraphSequence dispNode = nodeInfo.node as GraphSequence;
                if (saveToDatabase)
                {
                    SaveGraphNodeAsset(dispNode, n.name);
                }
                dispNode.sourceNode        = seq;
                dispNode.position.position = pos;

                int outName = 0;
                if (seq.sequenceNodes != null)
                {
                    float verticalOffset = 50.0f - ((seq.sequenceNodes.Length * 100.0f) / 2.0f);
                    foreach (AI.Node child in seq.sequenceNodes)
                    {
                        Slot o             = dispNode.AddOutputSlot("out:" + outName);
                        Slot nextInputSlot = CreateNode(child, pos + new Vector2(150, verticalOffset), saveToDatabase);

                        Connect(o, nextInputSlot);

                        outName++;
                        verticalOffset += 100.0f;
                    }
                }

                AddNode(dispNode);
                return(nodeInfo.inSlot);
            }
            else if (n is AI.Leaf)
            {
                AI.Leaf   l        = n as AI.Leaf;
                NodeInfo  nodeInfo = BehaviorNode.NewLeaf();
                GraphLeaf dispNode = nodeInfo.node as GraphLeaf;
                if (saveToDatabase)
                {
                    SaveGraphNodeAsset(dispNode, n.name);
                }
                dispNode.sourceNode = l;
                //dispNode.node.SetPropertyValue("Behavior Phase", l.nodeBehavior.CurrentPhase);
                dispNode.position.position = pos;

                AddNode(dispNode);
                return(nodeInfo.inSlot);
            }

            Debug.LogWarning("Something went wrong in TreeGraph.CreateNode()");
            return(null);
        }