Exemplo n.º 1
0
    // Use this for initialization
    void Awake()
    {
        // This is where we build the tree
        BTNode root = new BTPrioritySelector();

        bt = new BehaviorTree(root);

        // Should we transform?
        // Decorator to make sure this never wins the priority selection
        BTNode transformCheckDec = root.AddChild(new BTDecAlwaysFail());

        // Do the actual check
        transformCheckDec.AddChild(new BTAction(delegate(){
            if (state == PlayerState.Normal && Input.GetKeyDown(KeyCode.Space))
            {
                BecomeHulk();
            }
            return(BTStatusCode.Success);
        }));

        // Normal sequence
        BTNode normalSeq = root.AddChild(new BTSequence());

        // Are we in Normal mode?
        normalSeq.AddChild(new BTCondition(delegate(){ return(state == PlayerState.Normal); }));                        // Anonymous methods are great for this!
        // Get input
        normalSeq.AddChild(new BTAction(GetInput));
        // Move player
        normalSeq.AddChild(new BTAction(NormalMove));

        // Hulk sequence
        BTNode hulkSeq = root.AddChild(new BTSequence());

        // Assume we're in hulk mode if we're here
        // Get input
        hulkSeq.AddChild(new BTAction(GetInput));                         // New instance b/c each instance has it own status
        // Move player
        hulkSeq.AddChild(new BTAction(HulkMove));
    }
Exemplo n.º 2
0
        public static BTNode CreateNode(NEData neData)
        {
            Type neDataType = neData.data.GetType();
            int  index      = Remote.lstRemoteNodeDataType.IndexOf(neDataType);

            if (index == -1)
            {
                CLog.LogError("can not find remoteNeDataType=" + neDataType + " mapping nodeType");
                return(null);
            }
            Type   neNodeType = Remote.lstRemoteNodeType[index];
            BTNode neNode     = Activator.CreateInstance(neNodeType) as BTNode;

            neNode.data = neData.data;
            if (neData.lstChild != null)
            {
                for (int i = 0; i < neData.lstChild.Count; i++)
                {
                    BTNode childNode = CreateNode(neData.lstChild[i]);
                    neNode.AddChild(childNode);
                }
            }
            return(neNode);
        }
Exemplo n.º 3
0
 public void AddChildNode(BTEditorNode _node)
 {
     mNode.AddChild(_node.mNode);
 }
Exemplo n.º 4
0
    // Use this for initialization
    void Awake()
    {
        blackboard.Put("ChasingPlayer", false);

        // Begin the tree
        BTNode root = new BTPrioritySelector();

        bt = new BehaviorTree(root);

        // Player in Hulk mode branch
        BTNode hulkModeSeq = root.AddChild(new BTSequence());

        // Is the player in hulk mode?
        hulkModeSeq.AddChild(new BTCondition(delegate(){
            if (target == null)
            {
                return(false);
            }
            else
            {
                return(target.GetComponent <BTChasePlayer>().state == BTChasePlayer.PlayerState.Hulk);
            }
        }));
        // Set "ChasingPlayer" to false in the blackboard
        hulkModeSeq.AddChild(new BTAction(delegate(){
            blackboard.Put("ChasingPlayer", false);
            return(BTStatusCode.Success);
        }));
        // Are we safe?
        BTNode botNotSafeSwitch = hulkModeSeq.AddChild(new BTSwitch());

        // Are we far enough away yet?
        botNotSafeSwitch.AddChild(new BTCondition(delegate(){
            Vector3 dist = target.transform.position - transform.position;
            if (dist.sqrMagnitude > runRadius * runRadius)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }));
        // Yes, let's move away
        botNotSafeSwitch.AddChild(new BTAction(MoveAway));
        // No, let's idle
        botNotSafeSwitch.AddChild(new BTDecAlwaysSucceed());

        // Player in Normal mode branch
        BTNode normalModeSeq = root.AddChild(new BTSequence());

        // Have we noticed the player?
        normalModeSeq.AddChild(new BTCondition(delegate(){
            if (target == null)
            {
                return(false);
            }
            else if (blackboard.Look("ChasingPlayer") != null)
            {
                return((bool)blackboard.Look("ChasingPlayer"));
            }
            else
            {
                return(false);
            }
        }));
        // Do we need to be moving closer still?
        BTNode botTooFarSwitch = normalModeSeq.AddChild(new BTSwitch());

        // Are we still outside the minimum distance?
        botTooFarSwitch.AddChild(new BTCondition(delegate(){
            Vector3 dist = target.transform.position - transform.position;
            if (dist.sqrMagnitude <= chaseRadius * chaseRadius)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }));
        // Yes, move closer
        botTooFarSwitch.AddChild(new BTAction(MoveTowards));
        // No, let's idle
        botTooFarSwitch.AddChild(new BTDecAlwaysSucceed());

        // Player in Idle mode branch
        BTNode idleModeSeq = root.AddChild(new BTSequence());

        // If the player is in range, start chasing
        idleModeSeq.AddChild(new BTAction(delegate(){
            Vector3 dist = target.transform.position - transform.position;
            if (dist.sqrMagnitude <= runRadius * runRadius)
            {
                blackboard.Put("ChasingPlayer", true);
            }
            return(BTStatusCode.Success);
        }));
        // Idle
        idleModeSeq.AddChild(new BTDecAlwaysSucceed());
    }