예제 #1
0
    public void Init()
    {
        // 条件
        BaseCondiction.ExternalFunc IsDeadFun = () => { if (mMonster.mRoleData.mHp <= 0)
                                                        {
                                                            return(true);
                                                        }
                                                        else
                                                        {
                                                            return(false);
                                                        } };
        var Alive = new PreconditionNOT(IsDeadFun, "活");
        var Dead  = new Precondition(IsDeadFun, "死");

        BaseCondiction.ExternalFunc targetFun = () =>
        {
            Role targetRole = mMonster.mRoleData.GetTargetRole();
            if (targetRole)
            {
                return(targetRole.mBuffSystem.EnableSelect());
            }
            else
            {
                return(false);
            }
        };
        var hasTarget   = new Precondition(targetFun, "发现目标");
        var hasNoTarget = new PreconditionNOT(targetFun, "无目标");

        BaseCondiction.ExternalFunc AtkRangeFun = () =>
        {
            Role targetRole = mMonster.mRoleData.GetTargetRole();
            if (targetRole)
            {
                float dis = Vector3.Distance(targetRole.transform.position, mMonster.transform.position);
                if (dis <= mMonster.mSkillInfo.mAtkDistance)
                {
                    return(true);
                }
                else
                {
                    if (dis > 5)//脱离目标
                    {
                        mMonster.mRoleData.SetTargetRole(-1);
                    }
                }
            }

            return(false);
        };
        var AtkRange   = new Precondition(AtkRangeFun, "在攻击范围内");
        var NoAtkRange = new PreconditionNOT(AtkRangeFun, "超出攻击范围");

        //BaseCondiction.ExternalFunc HpFun = () => { if (mMonster.mRoleData.mHp <= 50) return true; else return false; };
        //var HPLess = new Precondition(HpFun, "快死");
        //var HPMore = new PreconditionNOT(HpFun, "健康");

        BaseCondiction.ExternalFunc CanMoveFun = () => { return(mMonster.mBuffSystem.CanMove()); };
        var canMove  = new Precondition(CanMoveFun, "能移动");
        var cantMove = new PreconditionNOT(CanMoveFun, "不能移动");

        BaseCondiction.ExternalFunc CanAtkFun = () => { return(mMonster.mBuffSystem.CanAtk()); };
        var canAtk  = new Precondition(CanAtkFun, "能攻击");
        var cantAtk = new PreconditionNOT(CanAtkFun, "不能攻击");

        //BaseCondiction.ExternalFunc EnableSelectFun = () =>
        //{
        //    Role targetRole = mMonster.mRoleData.GetTargetRole();
        //    if (targetRole)
        //    {
        //        return targetRole.mBuffSystem.EnableSelect();
        //    }

        //    return false;
        //};
        //var enableSelect = new Precondition(EnableSelectFun, "能选");
        ////var disableSelect = new PreconditionNOT(EnableSelectFun, "不能选");

        //BT Tree
        mRoot      = new BTSelector();
        mRoot.name = "Root";
        mRoot.Activate();

        BTSequence DeadSeq = new BTSequence();
        {
            DeadSeq.AddChild(Dead);
            // 死亡Action
            DeadSeq.AddChild(new BTActionWait(5));
            DeadSeq.AddChild(new ReviveAction(mMonster));
            mRoot.AddChild(DeadSeq);
        }

        BTSelector AliveSel = new BTSelector();
        BTSequence AliveSeq = new BTSequence();
        {
            AliveSeq.AddChild(Alive);
            AliveSeq.AddChild(AliveSel);
            mRoot.AddChild(AliveSeq);
        }

        BTSequence followSubtree = new BTSequence();
        {
            followSubtree.AddChild(canMove);
            followSubtree.AddChild(hasTarget);
            followSubtree.AddChild(NoAtkRange);
            //followSubtree.AddChild(HPMore);

            // 追击Action
            followSubtree.AddChild(new FollowAction(mMonster));

            AliveSel.AddChild(followSubtree);
        }

        BTSequence atkSeq = new BTSequence();
        {
            atkSeq.AddChild(canAtk);
            atkSeq.AddChild(hasTarget);
            atkSeq.AddChild(AtkRange);
            //atkSeq.AddChild(HPMore);

            // 攻击Action
            atkSeq.AddChild(new AttackAction(mMonster));
            atkSeq.AddChild(new BTActionWaitRandom(2.0f, 3.0f));

            AliveSel.AddChild(atkSeq);
        }

        BTSequence patrolSeq = new BTSequence();
        {
            patrolSeq.AddChild(canMove);
            patrolSeq.AddChild(hasNoTarget);
            patrolSeq.AddChild(new BTActionWaitRandom(1.0f, 5.0f));

            // 巡逻Action
            patrolSeq.AddChild(new PatrolAction(mMonster));

            AliveSel.AddChild(patrolSeq);
        }

        BTSequence IdleSeq = new BTSequence();
        {
            IdleSeq.AddChild(hasNoTarget);

            // 休息Action
            IdleSeq.AddChild(new IdleAction(mMonster));

            AliveSel.AddChild(IdleSeq);
        }

        //BTSequence runAwaySeq = new BTSequence();
        //{
        //    runAwaySeq.AddChild(canMove);
        //    runAwaySeq.AddChild(hasTarget);
        //    runAwaySeq.AddChild(HPLess);
        //    // 逃跑Action
        //    runAwaySeq.AddChild(new RunAwayAction(mMonster));

        //    AliveSel.AddChild(runAwaySeq);
        //}
    }
예제 #2
0
    // Use this for initialization
    public void Start()
    {
        inputData.attribute = GetComponent <CharacterAttribute>();

        rootNode.nodeName += "根";

        //条件
        var hasNoTarget = new PreconditionNOT(() => { return(inputData.attribute.hasTarget); });

        hasNoTarget.nodeName = "无目标";
        var hasTarget = new Precondition(hasNoTarget);

        hasTarget.nodeName = "发现目标";
        var isAnger = new Precondition(() => { return(inputData.attribute.isAnger); });

        isAnger.nodeName = "愤怒状态";
        var isNotAnger = new PreconditionNOT(isAnger);

        isNotAnger.nodeName = "非愤怒状态";
        var HPLessThan500 = new Precondition(() => { return(inputData.attribute.health < 500); });

        HPLessThan500.nodeName = "血少于500";
        var HPMoreThan500 = new PreconditionNOT(HPLessThan500);

        HPMoreThan500.nodeName = "血大于500";
        var isAlert = new Precondition(() => { return(inputData.attribute.isAlert); });

        isAlert.nodeName = "警戒";
        var isNotAlert = new PreconditionNOT(isAlert);

        isNotAlert.nodeName = "非警戒";


        var patrolNode = new SequenceNode();

        patrolNode.nodeName += "巡逻";
        patrolNode.AddCondition(hasNoTarget);
        patrolNode.AddCondition(isNotAlert);
        patrolNode.AddNode(new PatrolAction());

        var alert = new SequenceNode();

        alert.nodeName += "警戒";
        alert.AddCondition(hasNoTarget);
        alert.AddCondition(isAlert);
        alert.AddNode(new AlertAction());

        var runaway = new SequenceNode();

        runaway.nodeName += "逃跑";
        runaway.AddCondition(hasTarget);
        runaway.AddCondition(HPLessThan500);
        runaway.AddNode(new RunAwayAction());

        var attack = new SelectorNode();

        attack.nodeName += "攻击";
        attack.AddCondition(hasTarget);
        attack.AddCondition(HPMoreThan500);

        var attackCrazy = new SequenceNode();

        attackCrazy.nodeName += "疯狂攻击";
        attackCrazy.AddCondition(isAnger);
        attackCrazy.AddNode(new CrazyAttackAction());
        attack.AddNode(attackCrazy);

        var attackNormal = new SequenceNode();

        attackNormal.nodeName += "普通攻击";
        attackNormal.AddCondition(isNotAnger);
        attackNormal.AddNode(new AttackAction());
        attack.AddNode(attackNormal);

        rootNode.AddNode(patrolNode);
        rootNode.AddNode(alert);
        rootNode.AddNode(runaway);
        rootNode.AddNode(attack);
        var ret = rootNode.Enter(inputData);

        if (!ret)
        {
            Debug.Log("无可执行节点!");
        }
    }