Exemple #1
0
 /*
  * Called by Unity when the editor focus changes to this window.
  */
 void OnFocus()
 {
     try {
         //if the selection has a BehaviorTree attached AND
         if (Selection.activeGameObject.GetComponent <BehaviorTree.BehaviorTree>() != null &&
             //The currentTree is null, or it is different than the current one
             (!CurrentTree || Selection.activeGameObject.GetComponent <BehaviorTree.BehaviorTree>() != CurrentTree))
         {
             CurrentTree = Selection.activeGameObject.GetComponent <BehaviorTree.BehaviorTree>();
         }
         //Force a Repaint
         Repaint();
     }
     catch (NullReferenceException) { }
 }
Exemple #2
0
        void UndoRedoCallback()
        {
            TreeNode[] allNodes  = currentTree.GetComponents <TreeNode>();
            ParentNode newParent = null;

            //Looks for the parent missing a child
            foreach (TreeNode node in allNodes)
            {
                if (node is ParentNode)
                {
                    foreach (TreeNode child in ((ParentNode)node).Children)
                    {
                        if (child == null)
                        {
                            newParent = (ParentNode)node;
                            break;
                        }
                    }
                }
            }
            if (newParent != null)
            {
                //Looks for the child who is missing and adds them! Cancel Amber alert!
                foreach (TreeNode node in allNodes)
                {
                    if (node.parent == newParent)
                    {
                        if (!(node.parent.Children.Contains(node)))
                        {
                            newParent.childrenActual()[newParent.Children.Count - 1] = node;
                            break;
                        }
                    }
                }
            }

            CurrentTree = currentTree;
        }
        public void TestCreateTree()
        {
            var config = new Config
            {
                Name = "selector",
                Id   = 1,
                Args = new List <string> {
                    "11"
                },
                SubConfigs = new List <Config>
                {
                    new Config
                    {
                        Name = "sequence",
                        Id   = 2,
                        Args = new List <string> {
                            "12"
                        },
                        SubConfigs = new List <Config>
                        {
                            new Config
                            {
                                Name = "selector",
                                Id   = 4,
                                Args = new List <string> {
                                    "14"
                                },
                            },

                            new Config
                            {
                                Name = "selector",
                                Id   = 5,
                                Args = new List <string> {
                                    "15", "17"
                                },
                            }
                        }
                    },

                    new Config
                    {
                        Name = "not",
                        Id   = 3,
                        Args = new List <string> {
                            "13"
                        },
                        SubConfigs = new List <Config>
                        {
                            new Config
                            {
                                Name = "selector",
                                Id   = 6,
                                Args = new List <string> {
                                    "16"
                                },
                            }
                        }
                    }
                }
            };

            var behaviorTreeFactory = new BehaviorTreeFactory();

            BehaviorTree.BehaviorTree behaviorTree = behaviorTreeFactory.CreateTree(config);
            var blackBoard = new BlackBoard();

            Assert.IsTrue(behaviorTree.Run(blackBoard));
        }
Exemple #4
0
        /*
         * This is the callback for the menu items
         *
         * Parameters:
         * obj: The object defining what callback to run
         */
        private void callback(object obj)
        {
            //if delete was selected the delete the node, and force a reparse of the tree
            if (obj.ToString() == "Delete")
            {
                CurrentTree.delete(selectedNode);
                CurrentTree = currentTree;
            }
            //if Reset was chosen, reparse the tree positions
            else if (obj.ToString() == "Reset")
            {
                CurrentTree = currentTree;
            }
            //if start was chosen then add a new start node and reparse
            else if (obj.ToString() == "Start")
            {
                //TODO: Allow user to pick this!
                CurrentTree.addStartNode("Origin");
                CurrentTree = currentTree;                 //BAD!
            }
            else if (obj.ToString() == "Hide")
            {
                foreach (TreeNode node in nodes)
                {
                    node.hideFlags = HideFlags.HideInInspector;
                    Repaint();
                    EditorApplication.RepaintHierarchyWindow();
                }
            }
            else if (obj.ToString() == "Show")
            {
                foreach (TreeNode node in nodes)
                {
                    node.hideFlags = HideFlags.None;
                    Repaint();
                    EditorApplication.RepaintHierarchyWindow();
                }
            }
            //if Add was selected
            else
            {
                //if the node selected was a parent node, add a
                if (selectedNode is ParentNode)
                {
                    if (selectedNode is DecoratorNode && ((DecoratorNode)selectedNode).Child != null)
                    {
                        EditorUtility.DisplayDialog("Decorator Warning!", "This decorator already has a child, \nTry deleting the current child first", "Ok");
                    }
                    Type       typeToAdd = (Type)obj;
                    ParentNode parent    = (ParentNode)selectedNode;

                    //This complicated call uses reflection to make a new instance of a node to add
                    //to the parent node with a name bsaed on its type
                    string[] split = typeToAdd.ToString().Split('.');
                    string   typeName;
                    if (split.Length > 1)
                    {
                        typeName = split[1];
                    }
                    else
                    {
                        typeName = split[0];
                    }

                    TreeNode node = (TreeNode)typeof(BehaviorTree.BehaviorTree).GetMethod("addNode")
                                    .MakeGenericMethod(typeToAdd)
                                    .Invoke(CurrentTree, new object[] { parent, "new" + typeName });
                    //Reparse the tree
                    CurrentTree = currentTree;
                }
            }
        }
    public void MakeAIBaseline()
    {
        m_behaviorTree = new BehaviorTree.BehaviorTree(
            new BehaviorTreeBuilder()
            .Selector("AI Behavior Main Selector")
            .Condition("Is Being Knocked Back", IsBeingKnockedBack)

            // Is King
            .Sequence("Is KING Sequence")
            .Condition("Check if is King", IsKing)
            .Selector("KING Selector")

            .Sequence("Is on Iminent Danger")
            .Condition("Check if is on iminent danger", IsOnIminentDanger)
            .Selector("Try to Use Power Up to Run")
            .Action("Try to use Back off", UseBackOffPowerUp)
            .Action("Try to use Super Speed", UseSuperSlamPowerUp)
            .Condition("Tried all power ups?", TriedAllPowerUps)
            .End()
            .Action("Run away from closest Player", RunAwayFromClosestPlayer)
            .End()

            .Sequence("Collect Power Up")
            .Condition("Can get power ups (power up tracker is not full", CanGetPowerUp)
            .Condition("Is there a Power Up within distance", IsThereAPowerUpWithinDistance)
            .Action("Collect a Power Up", CollectPowerUp)
            .End()

            .Sequence("King Wander Around")
            .Action("King Wander!", KingWander)
            .End()

            .End()

            .End()

            // IS NOT KING
            .Selector("Is NOT King Selector")

            .Sequence("Chase King")
            .Condition("Is King Withing Follow Distance", IsKingWithinFollowDistance)
            .Selector("AI Attack or Follow")
            .Sequence("Attack if possible")
            .Condition("Check if is within Attacking Distance", IsWithinAttackingDistance)
            .Condition("Check if AI can attack", CanAttack)
            .Action("Attack King", Attack)
            .End()

            .Sequence("Attack with Super Slam if possible")
            .Condition("Is Within Super Slam Distance?", IsWithinSuperSlamDistance)
            .Condition("SUPER SLAM", UseSuperSlamPowerUp)
            .End()

            .Sequence("Chase King")
            .Action("Chase King", ChaseKing)
            .End()
            .End()
            .End()

            .Sequence("Collect Power Up")
            .Condition("Can get power ups (power up tracker is not full", CanGetPowerUp)
            .Condition("Is there a Power Up within distance", IsThereAPowerUpWithinDistance)
            .Action("Collect a Power Up", CollectPowerUp)
            .End()

            .Sequence("I dunno man, I like my own space")
            .Condition("Check if someone is REALLY close", CheckIfClosestPlayerIsWithinAttackingDistance)
            .Condition("Checking if I can attack", CanAttack)
            .Action("Attack!!", Attack)
            .End()

            .Sequence("Walk Randomly")
            .Condition("Can we wander?", CanNotKingWander)
            .Action("Wander...", NotKingWanderRandomly)
            .End()
            .End()

            .End()
            .Build()
            );

        m_currentBehavior = EAIBehavior.BASELINE;
        InitializeAI();
    }