//Run this on awake, enable, or start public void DefineBehavior() { //Conditions ConditionalLambda canSeeTarget = new ConditionalLambda(() => Vector3.Distance(transform.position, Target.transform.position) <= SightDistance); ConditionalLambda tooCloseToTarget = new ConditionalLambda(() => Vector3.Distance(transform.position, Target.transform.position) <= FollowDistance); //Actions BAction followTarget = new BAction(FollowTarget); BAction getPatrolPoint = new BAction(GetRandomPatrolPoint); BAction goToPatrolPoint = new BAction(GoToPatrolPoint); BAction circleTarget = new BAction(CircleTarget); SequenceContinued checkIfShouldCircle = new SequenceContinued(tooCloseToTarget, circleTarget); //If too circle target, if not follow Target SelectorContinued following = new SelectorContinued(checkIfShouldCircle, followTarget); SequenceContinued checkIfCanFollow = new SequenceContinued(canSeeTarget, following); SequenceContinued patrol = new SequenceContinued(getPatrolPoint, goToPatrolPoint); SelectorContinued lookForTarget = new SelectorContinued(checkIfCanFollow, patrol); //NOTE: RootSelector allows for StateMachine type usage in behavior tree //setup root node, choose initialization phase or pathing/movement phase SelectorBranch root = new SelectorBranch(switchBehaviors, lookForTarget); //set a reference to the root behavior = ScriptableObject.CreateInstance <BehaviorTree>(); behavior.Init(root); CanBehave = true; }
private void DrawNodeSelected(int id) { BehaviorComponent component = BehaviorNode.Selection.BehaviorComponent; GUILayout.BeginVertical(); GUIStyle style = new GUIStyle(); style.fontStyle = FontStyle.Bold; GUILayout.Label("Behavior Type: " + component.GetType().Name, style); if (component.GetType() == typeof(ConditionalLambda)) { ConditionalLambda conditional = ((ConditionalLambda)component); if (conditional.TestAction != null) { conditional.ExpressionObjects.Clear(); conditional.TestAction.Invoke(conditional); GUILayout.Label("Conditional Description: " + conditional.ConditionalDescription); if (conditional.ExpressionObjects != null) { for (int i = 0; i < conditional.ExpressionObjects.Count; i++) { GUILayout.Label("Conditional Object Type: " + conditional.ExpressionObjects[i].GetType().Name); GUILayout.Label("Conditional Object Value: " + conditional.ExpressionObjects[i]); } } GUILayout.Label("Conditional Result: " + conditional.Result); } else { GUILayout.Label("Conditional Result: " + conditional.Result); GUILayout.Label("Conditional Body: " + conditional.Expression); GUILayout.Label("Conditional Not Using Action Constructor for Debugging"); } } else { foreach (FieldInfo field in fields) { string name = field.Name; object value = field.GetValue(component); if (value != null && field.Name != "Node" && field.Name != "NotifyOnExecute" && field.Name != "AddToHistory" && field.Name != "Action") { GUILayout.Label("Field: " + name); GUILayout.Label("Value: " + value); } } } component.NotifyOnExecute = GUILayout.Toggle(component.NotifyOnExecute, "Notify On Execute"); GUILayout.EndVertical(); }