示例#1
0
    // This method is called when the user clicks the remove behaviour button
    void RemoveBehaviour(CompositeBehaviour cb)
    {
        // We know the array wont be null, as the button would not appear
        int oldCount = cb.behaviours.Length;

        // If there is only one behaviour, we can just clear it out:
        if (oldCount == 1)
        {
            cb.behaviours = null;
            cb.weights    = null;
            return;
        }

        // If array is empty we will start with 1 item
        FlockBehaviour[] newBehaviours = new FlockBehaviour[oldCount - 1];
        // Create new array
        float[] newWeights = new float[oldCount - 1];
        // Iterate through and add the original values
        for (int i = 0; i < oldCount - 1; i++)
        {
            newBehaviours[i] = cb.behaviours[i];
            newWeights[i]    = cb.weights[i];
        }
        // We do not need to assign anything to newWeights here because it has already been assigned
        cb.behaviours = newBehaviours;
        cb.weights    = newWeights;
    }
示例#2
0
    private void RemoveBehavior(CompositeBehaviour cb)

    {
        int oldCount = cb.behaviours.Length;

        if (oldCount == 1)
        {
            cb.behaviours = null;
            cb.weights    = null;
            return;
        }

        else

        {
            FlockBehaviour[] newBehaviors = new FlockBehaviour[oldCount - 1];

            float[] newWeights = new float[oldCount - 1];

            for (int i = 0; i < oldCount - 1; i++)

            {
                newBehaviors[i] = cb.behaviours[i];

                newWeights[i] = cb.weights[i];
            }

            cb.behaviours = newBehaviors;

            cb.weights = newWeights;
        }
    }
        private void RenderBehaviourTree(Graphics graphics, int depth, CompositeBehaviour <BtContext> obj)
        {
            RenderInternal(graphics, depth, obj);

            var childDepth = depth + 1;

            foreach (var child in obj.Children)
            {
                RenderBehaviourTree(graphics, childDepth, child);
            }
        }
        private void VisitChildren(CompositeBehaviour <TContext> obj)
        {
            _depth++;

            foreach (var child in obj.Children)
            {
                child.Accept(this);
            }

            _depth--;
        }
示例#5
0
    public override void OnInspectorGUI()
    {
        CompositeBehaviour cb = (CompositeBehaviour)target;

        if (cb.behaviours == null || cb.behaviours.Length == 0)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.HelpBox("No behaviours in array.", MessageType.Warning);
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.LabelField("Element", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
            EditorGUILayout.LabelField("Behaviours", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
            EditorGUILayout.LabelField("Weights", GUILayout.MinWidth(180f), GUILayout.MaxWidth(240f));

            EditorGUILayout.EndHorizontal();

            EditorGUI.BeginChangeCheck();

            for (int i = 0; i < cb.behaviours.Length; i++)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(i.ToString(), GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                cb.behaviours[i] = (FlockBehaviour)EditorGUILayout.ObjectField(cb.behaviours[i], typeof(FlockBehaviour), false, GUILayout.MinWidth(60f));
                cb.weights[i]    = EditorGUILayout.FloatField(cb.weights[i], GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                EditorGUILayout.EndHorizontal();
            }

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(cb);
            }
        }

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("Add Behaviour"))
        {
            AddBehaviour(cb);
            EditorUtility.SetDirty(cb);
        }

        if (cb.behaviours != null && cb.behaviours.Length > 0)
        {
            if (GUILayout.Button("Remove Behaviour"))
            {
                RemoveBehaviour(cb);
                EditorUtility.SetDirty(cb);
            }
        }
        EditorGUILayout.EndHorizontal();
    }
示例#6
0
        private static string GetExpression(CompositeBehaviour <TContext> obj, int depth)
        {
            var expression = InternalGetExpression(obj, depth);

            var childDepth = depth + 1;

            foreach (var child in obj.Children)
            {
                expression += GetExpression(child, childDepth);
            }

            return(expression);
        }
示例#7
0
    // 重置子节点
    public virtual void Reset()
    {
        childIndex = 0;
        CompositeBehaviour node = null;

        for (int i = 0; i < children.Count; i++)
        {
            node = children[i] as CompositeBehaviour;
            if (node != null)
            {
                node.Reset();
            }
        }
    }
    void AddBehavior(CompositeBehaviour cb)
    {
        int oldCount = (cb.behaviours != null) ? cb.behaviours.Length : 0;

        FlockBehaviour[] newBehaviors = new FlockBehaviour[oldCount + 1];
        float[]          newWeights   = new float[oldCount + 1];
        for (int i = 0; i < oldCount; i++)
        {
            newBehaviors[i] = cb.behaviours[i];
            newWeights[i]   = cb.weights[i];
        }
        newWeights[oldCount] = 1f;
        cb.behaviours        = newBehaviors;
        cb.weights           = newWeights;
    }
示例#9
0
    // This method is called when the user clicks the add behaviour button
    void AddBehaviour(CompositeBehaviour cb)
    {
        // Get original size of the array
        int oldCount = (cb.behaviours != null) ? cb.behaviours.Length : 0;

        // If array is empty we will start with 1 item
        FlockBehaviour[] newBehaviours = new FlockBehaviour[oldCount + 1];
        // Create new array
        float[] newWeights = new float[oldCount + 1];
        // Iterate through and add the original values
        for (int i = 0; i < oldCount; i++)
        {
            newBehaviours[i] = cb.behaviours[i];
            newWeights[i]    = cb.weights[i];
        }
        // newWeights can't be zero otherwise new behaviours can't take effect
        // The user would think something has gone wrong if we don't set it to 1f:
        newWeights[oldCount] = 1f;
        cb.behaviours        = newBehaviours;
        cb.weights           = newWeights;
    }
示例#10
0
        void RemoveBehaviour(CompositeBehaviour compositeBehaviour)
        {
            int oldCount = compositeBehaviour.behaviour.Length;

            if (oldCount == 1)
            {
                compositeBehaviour.behaviour = null;
                compositeBehaviour.weights   = null;
                return;
            }

            FlockBehaviour[] newBehaviours = new FlockBehaviour[oldCount - 1];
            float[]          newWeights    = new float[oldCount - 1];

            for (int i = 0; i < oldCount - 1; i++)
            {
                newBehaviours[i] = compositeBehaviour.behaviour[i];
                newWeights[i]    = compositeBehaviour.weights[i];
            }

            compositeBehaviour.behaviour = newBehaviours;
            compositeBehaviour.weights   = newWeights;
        }
示例#11
0
        /// <summary>
        /// Editor stuff for each array field and allows for array changes
        /// </summary>
        public override void OnInspectorGUI()
        {
            CompositeBehaviour cb = (CompositeBehaviour)target;

            if (cb.behaviours == null || cb.behaviours.Length == 0)
            {
                if (GUILayout.Button("Add Behaviour"))
                {
                    //Add behaviour
                    AddBehaviour(cb);
                    //ensures changes are saved
                    EditorUtility.SetDirty(cb);
                }
            }
            else
            {
                //sets up the fields for each behaviour
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Number", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                EditorGUILayout.LabelField("Behaviors", GUILayout.MinWidth(60f));
                EditorGUILayout.LabelField("Weights", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                EditorGUILayout.EndHorizontal();

                //check for changes
                EditorGUI.BeginChangeCheck();

                //sets up the visuals for each array
                for (int i = 0; i < cb.behaviours.Length; i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(i.ToString(), GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                    cb.behaviours[i] = (FlockBehaviour)EditorGUILayout.ObjectField(cb.behaviours[i],
                                                                                   typeof(FlockBehaviour), false, GUILayout.MinWidth(60f));
                    cb.weights[i] =
                        EditorGUILayout.FloatField(cb.weights[i], GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                    EditorGUILayout.EndHorizontal();
                }

                if (EditorGUI.EndChangeCheck())
                {
                    EditorUtility.SetDirty(cb);
                }

                EditorGUILayout.EndHorizontal();


                //adding move behaviours
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Add Behaviour"))
                {
                    //Add behaviour
                    AddBehaviour(cb);
                    //ensures changes are saved
                    EditorUtility.SetDirty(cb);
                }


                //removes behaviour
                if (cb.behaviours != null || cb.behaviours.Length > 0)
                {
                    if (GUILayout.Button("Remove Behaviour"))
                    {
                        //Remove Behaviour
                        RemoveBehaviour(cb);
                        //ensures changes are saved
                        EditorUtility.SetDirty(cb);
                    }
                }
            }
        }
    public override void OnInspectorGUI()
    {
        CompositeBehaviour cb = (CompositeBehaviour)target;

        Rect r = EditorGUILayout.BeginHorizontal();

        r.height = EditorGUIUtility.singleLineHeight;

        //check for behaviors
        if (cb.behaviours == null || cb.behaviours.Length == 0)
        {
            EditorGUILayout.HelpBox("No behaviors in array.", MessageType.Warning);
            EditorGUILayout.EndHorizontal();
            r        = EditorGUILayout.BeginHorizontal();
            r.height = EditorGUIUtility.singleLineHeight;
        }
        else
        {
            r.x     = 30f;
            r.width = EditorGUIUtility.currentViewWidth - 95f;
            EditorGUI.LabelField(r, "Behaviors");
            r.x     = EditorGUIUtility.currentViewWidth - 65f;
            r.width = 60f;
            EditorGUI.LabelField(r, "Weights");
            r.y += EditorGUIUtility.singleLineHeight * 1.2f;

            EditorGUI.BeginChangeCheck();
            for (int i = 0; i < cb.behaviours.Length; i++)
            {
                r.x     = 5f;
                r.width = 20f;
                EditorGUI.LabelField(r, i.ToString());
                r.x              = 30f;
                r.width          = EditorGUIUtility.currentViewWidth - 95f;
                cb.behaviours[i] = (FlockBehaviour)EditorGUI.ObjectField(r, cb.behaviours[i], typeof(FlockBehaviour), false);
                r.x              = EditorGUIUtility.currentViewWidth - 65f;
                r.width          = 60f;
                cb.weights[i]    = EditorGUI.FloatField(r, cb.weights[i]);
                r.y             += EditorGUIUtility.singleLineHeight * 1.1f;
            }
            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(cb);
            }
        }

        EditorGUILayout.EndHorizontal();
        r.x     = 5f;
        r.width = EditorGUIUtility.currentViewWidth - 10f;
        r.y    += EditorGUIUtility.singleLineHeight * 0.5f;
        if (GUI.Button(r, "Add Behavior"))
        {
            AddBehavior(cb);
            EditorUtility.SetDirty(cb);
        }

        r.y += EditorGUIUtility.singleLineHeight * 1.5f;
        if (cb.behaviours != null && cb.behaviours.Length > 0)
        {
            if (GUI.Button(r, "Remove Behavior"))
            {
                RemoveBehavior(cb);
                EditorUtility.SetDirty(cb);
            }
        }
    }
示例#13
0
    public override void OnInspectorGUI()
    {
        #region Setup
        // Here we cast target, this is an object (in the inspector), directly to composite behaviour so we can access it's variables etc.
        CompositeBehaviour cb = (CompositeBehaviour)target;



        #endregion

        EditorGUILayout.BeginHorizontal();

        // Check for behaviours
        // If there are no behaviours or the array is empty
        if (cb.behaviours == null || cb.behaviours.Length == 0)
        {
            // Display error message on it's own line
            EditorGUILayout.HelpBox("No behaviours in array.", MessageType.Warning);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
        }



        else
        {
            // If there are behaviours (whole reason of this script!)
            // Each one of these simple creates a label field with a min width and max width
            // These two widths mean that even if the inspector window is resized, the text will remain where we it want
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Number", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
            EditorGUILayout.LabelField("Behaviours", GUILayout.MinWidth(60f));
            EditorGUILayout.LabelField("Weights", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
            EditorGUILayout.EndHorizontal();



            // Set save checker
            // This checks for any inputs
            EditorGUI.BeginChangeCheck();


            // Display each behaviour:
            for (int i = 0; i < cb.behaviours.Length; i++)
            {
                EditorGUILayout.BeginHorizontal();
                // Make each item a string
                EditorGUILayout.LabelField(i.ToString(), GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                //EditorGUILayout.BeginHorizontal();
                // Show a field for the behaviours
                // False means we wont take any objects that come from the scene - we want only scriptable objects
                cb.behaviours[i] = (FlockBehaviour)EditorGUILayout.ObjectField(cb.behaviours[i], typeof(FlockBehaviour), false, GUILayout.MinWidth(60f));
                // Do the same for the weights:
                cb.weights[i] = EditorGUILayout.FloatField(cb.weights[i], GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                EditorGUILayout.EndHorizontal();
            }


            // End the change check
            // This just means that any altered behaviours/weights will be updated as appropriate
            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(cb);
            }
        }

        EditorGUILayout.EndHorizontal();



        if (GUILayout.Button("Add Behaviour"))
        {
            // Add behaviour
            AddBehaviour(cb);
            // Let Unity know this scriptable object has been changed and needs to be saved
            EditorUtility.SetDirty(cb);
        }



        // We only want the add button to appear if there are behaviours to remove
        if (cb.behaviours != null && cb.behaviours.Length > 0)
        {
            if (GUILayout.Button("Remove Behaviour"))
            {
                // Remove behaviour
                RemoveBehaviour(cb);
                // Let unity know this scriptable object has been changed and needs to be saved
                EditorUtility.SetDirty(cb);
            }
        }
    }
 public void Visit(CompositeBehaviour <TContext> obj)
 {
     PrintNode(obj);
     VisitChildren(obj);
 }