void Start()
    {
        //INIT
        playerCtrl = player.gameObject.GetComponent <CharacterController>();

        //ACTIONS
        BTAction block        = new BTAction(Block);
        BTAction meleeAttack  = new BTAction(MeleeAttack);
        BTAction rangedAttack = new BTAction(RangedAttack);
        BTAction getCloser    = new BTAction(GetCloser);

        //CONDITIONS
        BTCondition playerAttacking = new BTCondition(PlayerAttacking);
        BTCondition playerMelee     = new BTCondition(PlayerMelee);
        BTCondition playerFar       = new BTCondition(PlayerFar);

        //TREE
        BTSequence       seqBlock           = new BTSequence(new IBTTask[] { playerAttacking, block });
        BTSequence       seqMelee           = new BTSequence(new IBTTask[] { playerMelee, meleeAttack });
        BTSequence       seqGetCloser       = new BTSequence(new IBTTask[] { playerFar, getCloser });
        BTDecorator      untilFailGetCloser = new BTDecoratorUntilFail(seqGetCloser);
        BTRandomSelector rselRanged         = new BTRandomSelector(new IBTTask[] { untilFailGetCloser, rangedAttack });
        BTSelector       selAttack          = new BTSelector(new IBTTask[] { seqMelee, rselRanged });

        root = new BTSelector(new IBTTask[] { seqBlock, selAttack });
    }
Exemple #2
0
    private void Start()
    {
        var a    = true;  // swich false
        var b    = false; // switch true
        var root = new BTSelector("Root");

        {
            var selectA = new BTSequencer("A");
            {
                var isA = new BTCondition("Is A", () => a);
                selectA.AddState(isA);

                var console = new BTAction("Print A", () => Debug.Log("Is A"));
                selectA.AddState(console);
            }
            root.AddState(selectA);

            var selectB = new BTSequencer("B");
            {
                var isB = new BTCondition("Is B", () => b);
                selectB.AddState(isB);

                var console = new BTAction("Print B", () => Debug.Log("Is B"));
                selectB.AddState(console);
            }
            root.AddState(selectB);
        }

        BehTree.AddState(root);
    }
Exemple #3
0
        public static BTAction CreateAction(Type type, BTState parentState)
        {
            BTAction action = XScriptableObject.CreateInstance(type) as BTAction;

            action.Name  = type.Name;
            action.Owner = parentState;
            parentState.totalActions.Add(action);
            AddActionToState(parentState, action);
            return(action);
        }
        public static BTInfo CreateBtInfo(BTAction bt)
        {
            var offsets = bt.GetTotalOffsets();
            var memSize = bt.GetTotalMemSize();

            return(new BTInfo()
            {
                MemSize = memSize,
                Offsets = offsets,
                RootNode = bt,
            });
        }
    private void InitializePlayerPhaseBehaviourTree()
    {
        BTAction BTAct_CheckEndTurn = new BTAction(
            () =>
        {
            if (mPlayerToEndPhase)
            {
                SwitchPhase(GamePhase.EnemyPhase);
                return(BTStatus.Running);
            }
            return(BTStatus.Success);
        }
            );
        BTAction BTAct_MoveCard = new BTAction(
            () =>
        {
            if (ControlAreaManager.CardIsBeingDragged)
            {
                return(BTStatus.Running);
            }

            return(BTStatus.Success);
        }
            );
        BTAction BTAct_ExecuteCard = new BTAction(
            () =>
        {
            if (ControlAreaManager.IsPanelOpen)
            {
                ControlAreaManager.Instance.SetControlBlockerEnabled(true);
                return(BTStatus.Running);
            }
            // Check player turn status.
            if (Player.TurnStatus == PlayerTurnStatus.Running)
            {
                ControlAreaManager.Instance.SetControlBlockerEnabled(true);
                return(BTStatus.Running);
            }

            if (EventAnimationController.Instance.IsAnimating == false)
            {
                ControlAreaManager.Instance.SetControlBlockerEnabled(false);
            }

            return(BTStatus.Success);
        }
            );
        BTSequence BT_Root = new BTSequence(BTAct_CheckEndTurn, BTAct_MoveCard, BTAct_ExecuteCard);

        mPlayerPhaseBehaviourTree = new BehaviourTree(BT_Root);
    }
        public void Init(BaseEntity owner, int id)
        {
            this.owner = owner;

            BTInfo                     = AIEntityBehaviorTreeFactory.GetBTInfo(id);
            _behaviorTree              = BTInfo.RootNode;
            BehaviorWorkingData        = new AIEntityWorkingData();
            BehaviorWorkingData.entity = this;
            BehaviorWorkingData.Init(BTInfo.Offsets, BTInfo.MemSize);
            BehaviorWorkingData.entityTF           = owner.transform;
            BehaviorWorkingData.EntityAnimatorView = new CAnimatorView().Init(owner);
            _blackboard             = new BlackBoard();
            _lastTriggeredAnimation = string.Empty;
        }
Exemple #7
0
        public static void AddActionToState(BTState Owner, BTAction action)
        {
            var Fsm = Owner.Owner;

            if (BTEditorWindow.HasPrefab(Fsm))
            {
                if (Fsm.template == null)
                {
                    AddTemplateToFsm(Fsm);
                    Fsm.template.startEvent = Fsm.startEvent;
                    Fsm.template.totalEvent = Fsm.totalEvent;
                }
                BTEditorWindow.AddObjectToAsset(action, Owner);
            }
            EditorUtility.SetDirty(Fsm);
        }
Exemple #8
0
        public static void AddActionToState(BTState Owner, BTAction action)
        {
            var Fsm = Owner.Owner;

            if (BTEditorWindow.HasPrefab(Fsm))
            {
                if (Fsm.template == null)
                {
                    Fsm.template = XScriptableObject.CreateInstance <BTTemplate> ();
                    BTEditorWindow.AddObjectToAsset(Fsm.template, Fsm.gameObject);
                    EditorUtility.SetDirty(Fsm);
                    Fsm.template.startEvent = Fsm.startEvent;
                    Fsm.template.totalEvent = Fsm.totalEvent;
                }
                BTEditorWindow.AddObjectToAsset(action, Owner);
            }
            EditorUtility.SetDirty(Fsm);
        }
        public AIEntity Init()
        {
            var btInfo = AIEntityBehaviorTreeFactory.GetBehaviorTreeDemo1();

            _behaviorTree = btInfo.RootNode;

            _behaviorWorkingData                = new AIEntityWorkingData();
            _behaviorWorkingData.entity         = this;
            _behaviorWorkingData.entityTF       = this.transform;
            _behaviorWorkingData.entityAnimator = GetComponent <Animator>();
            _behaviorWorkingData.Init(btInfo.Offsets, btInfo.MemSize);

            _blackboard = new BlackBoard();

            _nextTimeToGenMovingTarget = 0f;
            _lastTriggeredAnimation    = string.Empty;

            _isDead = false;

            _targetDummyObject = GameResourceManager.instance.LoadResource("AttackTarget");

            return(this);
        }
Exemple #10
0
 public void Main()
 {
     var root = new BTAction();
 }
    private void InitializeEnemyPhaseBehaviourTree()
    {
        // Returns fail when there are enemies. Success if no enemies found.
        BTAction BTAct_NoEnemyCheck = new BTAction(
            () =>
        {
            // If there are not even one enemy. Nothing to do here.
            if (EnemyList.Count < 1)
            {
                return(BTStatus.Success);
            }

            return(BTStatus.Failure);
        }
            );

        BTAction BTAct_EnemyPhaseStartAnimations = new BTAction(
            () =>
        {
            if (EPdoneStartAnimation == false)
            {
                EventAnimationController.Instance.ExecutePhaseAnimation(GamePhase.EnemyPhase);
                EPdoneStartAnimation = true;
                return(BTStatus.Running);
            }
            else if (EventAnimationController.Instance.IsAnimating == false)
            {
                EPdoneStartAnimation = false;
                ControlAreaManager.Instance.SetControlBlockerEnabled(true);
                return(BTStatus.Success);
            }

            return(BTStatus.Running);
        }
            );

        BTAction BTAct_MoveEnemyPieces = new BTAction(
            () =>
        {
            EnemyPiece curEnemy = null;

            if (EPcurEnemyIndex < EnemyList.Count)
            {
                curEnemy = EnemyList[EPcurEnemyIndex];
            }

            switch (curEnemy.TurnStatus)
            {
            case EnemyTurnStatus.Unprocessed:
                curEnemy.ExecuteTurn();
                break;

            case EnemyTurnStatus.Running:
                return(BTStatus.Running);               // Do nothing, just let it run.

            default:                                    // Both processed and waiting, move on to the next EnemyPiece.
                EPcurEnemyIndex++;
                if (EPcurEnemyIndex >= EnemyList.Count) // If went through the whole thing once already.
                {
                    EPcurEnemyIndex = 0;
                    BoardScroller.Instance.FocusCameraToPos(
                        DungeonManager.Instance.GridPosToWorldPos(GameManager.Instance.Player.PosX, GameManager.Instance.Player.PosY),
                        0.2f,
                        Graph.InverseExponential);
                    return(BTStatus.Success);
                }
                break;
            }

            return(BTStatus.Running);
        }
            );
        BTAction BTAct_AttackPlayer = new BTAction(
            () =>
        {
            EnemyPiece curEnemy = null;

            if (EPcurEnemyIndex < EnemyList.Count)
            {
                curEnemy = EnemyList[EPcurEnemyIndex];
            }

            switch (curEnemy.TurnStatus)
            {
            case EnemyTurnStatus.Waiting:
                curEnemy.ExecuteTurn();
                break;

            case EnemyTurnStatus.Running:
                return(BTStatus.Running);               // Do nothing, just let it run.

            default:                                    // Move on to the next one. Only search for those that are Waiting status.
                EPcurEnemyIndex++;
                if (EPcurEnemyIndex >= EnemyList.Count) // If went finish second pass.
                {
                    return(BTStatus.Success);
                }
                break;
            }

            return(BTStatus.Running);
        }
            );

        // Reduce the player's shield by 1 point after end of every enemy turn.
        BTAction BTAct_ReducePlayersShield = new BTAction(
            () =>
        {
            Player.DeductShieldPoints(1);
            if (Player.TurnStatus == PlayerTurnStatus.Running)
            {
                return(BTStatus.Running);
            }
            else
            {
                EndEnemyPhase();
                return(BTStatus.Success);
            }
        }
            );

        // BT_Sequence only runs when there are enemy pieces.
        // Refer to BT_NullCheckSelector below.
        BTSequence BT_EnemyMovementSequence = new BTSequence(BTAct_MoveEnemyPieces, BTAct_AttackPlayer);
        // Root is Selector that checks for enemies.
        // If no enemies, immediately stops (BTAct_NoEnemyCheck returns success).
        BTSelector BT_NullCheckSelector = new BTSelector(BTAct_NoEnemyCheck, BT_EnemyMovementSequence);
        BTSequence BT_Root = new BTSequence(BTAct_EnemyPhaseStartAnimations, BT_NullCheckSelector, BTAct_ReducePlayersShield);

        mEnemyPhaseBehaviourTree = new BehaviourTree(BT_Root);
    }
Exemple #12
0
 public Task(BTAction act)
 {
     act_ = act;
 }