Exemplo n.º 1
0
        /// <summary>Draw node's body GUI.</summary>
        public override void OnBodyGUI()
        {
            base.OnBodyGUI();

            _composite = target as BTCompositeNode;

            // Only work the GUI for the current exits and entries.
            int childCount = _composite.ChildCount;

            // Render children ports.
            EditorGUILayout.Space();

            for (int i = 0; i < childCount; i++)
            {
                BTConnection connection = _composite.GetChildConnection(i);
                NodePort     port       = target.GetOutputPort(connection.PortName);
                NodePort     connected  = port.Connection;

                if (connected == null)
                {
                    _composite.RemoveChildConnection(i);
                    i--;
                    childCount--;
                }
                else
                {
                    GUILayout.BeginHorizontal();
                    EditorGUILayout.Space();
                    NodeEditorGUILayout.PortField(new GUIContent((i + 1).ToString()), port, GUILayout.Width(50));
                    GUILayout.EndHorizontal();
                }
            }

            // Check if we need to create new exit.
            _newChild = target.GetOutputPort("newChild");
            if (_newChild == null)
            {
                _newChild = target.AddInstanceOutput(typeof(BTConnection), Node.ConnectionType.Override, Node.TypeConstraint.Inherited, "newChild");
            }

            // If exit connection is not empty create new exit.
            if (_newChild.Connection != null)
            {
                _composite.AddChildConnection(_newChild.Connection);
                _newChild.Disconnect(_newChild.Connection);
            }

            GUILayout.BeginHorizontal();
            EditorGUILayout.Space();
            NodeEditorGUILayout.PortField(_newChild, GUILayout.Width(80));
            GUILayout.EndHorizontal();
        }
Exemplo n.º 2
0
        /// <summary>Execute the Behavior Tree.</summary>
        protected override BTGraphResult InternalRun()
        {
            // Init loop.
            int[]  indices;
            object value;

            if (BT.Blackboard.variables.TryGetValue("CompositeIndices" + GetInstanceID(), out value))
            {
                indices = (int[])value;
            }
            else
            {
                indices = PrepareChildren();
            }
            // Traverse all children running them until break point.
            BTGraphResult result = BTGraphResult.Failure;

            for (int i = 0; i < indices.Length; i++)
            {
                // Run target child.
                BTConnection connection = _children[indices[i]];
                result = connection.Run();
                // Break if result is running.
                if (result.IsRunning)
                {
                    indices = indices.Where((a, index) => index >= i).ToArray();
                    BT.Blackboard.variables["CompositeIndices" + GetInstanceID()] = indices;
                    break;
                }
                // Check if we need to break loop.
                if (ShouldBreakLoop(result))
                {
                    break;
                }
            }

            // Reset running child index if not still running.
            if (!result.IsRunning)
            {
                BT.Blackboard.variables.Remove("CompositeIndices" + GetInstanceID());
            }

            return(result);
        }