Esempio n. 1
0
    private void Start()
    {
        player = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent <UserSoldier>();

        gameManager  = GameObject.FindGameObjectWithTag(Tags.GameController).GetComponent <GameManager>();
        npc          = GetComponent <NPCSoldier>();
        npc.Damaged += Damaged;
        nextRadius   = gameManager.nextRadius;
        SetCoverPos(transform.position);

        var notInSafeZoneCond = new ConditionNode(IsInSafeZone, false);
        var haveEnemyCond     = new ConditionNode(HaveEnemyInSight, true);
        var beingShootCond    = new ConditionNode(BeingShoot, true);
        var haveWeaponCond    = new ConditionNode(HaveWeapon, true);
        var seekWeaponCond    = new ConditionNode(SeekWeapon, true);

        var equipAction         = new SetEquipmentAction(npc, true);
        var notEquipAction      = new SetEquipmentAction(npc, false);
        var crouchAction        = new SetCrouchAction(npc, true);
        var notCrouchAction     = new SetCrouchAction(npc, false);
        var toSafeZoneAction    = new MoveAction(npc, blackBoard, safeZoneKey);
        var goCoverAction       = new MoveAction(npc, blackBoard, coverPosKey);
        var FallBackAction      = new MoveAction(npc, blackBoard, fallBackPosKey);
        var goWeaponAction      = new MoveAction(npc, blackBoard, weaponPosKey);
        var pickUpWeaponAction  = new PickUpWeaponAction(npc, blackBoard, weaponKey);
        var shootAction         = new ShootAction(npc, blackBoard, enemyPosKey);
        var turnToShooterAction = new TurnAction(npc, blackBoard, shooterPosKey);
        var idleAction          = new IdleAction(npc);

        var combat = new SequenceNode();

        combat.AddCondition(haveWeaponCond);
        combat.AddNode(equipAction);
        combat.AddNode(shootAction);

        var fallBack = new SequenceNode();

        fallBack.AddNode(FallBackAction);

        haveEnemy = new SelectionNode();
        haveEnemy.AddCondition(haveEnemyCond);
        haveEnemy.AddNode(combat);
        haveEnemy.AddNode(fallBack);

        var counter = new SequenceNode();

        counter.AddCondition(haveWeaponCond);
        counter.AddNode(turnToShooterAction);

        beingShoot = new SelectionNode();
        beingShoot.AddCondition(beingShootCond);
        beingShoot.AddNode(counter);
        beingShoot.AddNode(fallBack);

        toSafeZone = new SequenceNode();
        toSafeZone.AddCondition(notInSafeZoneCond);
        toSafeZone.AddNode(notCrouchAction);
        toSafeZone.AddNode(notEquipAction);
        toSafeZone.AddNode(toSafeZoneAction);

        stayInCover = new SequenceNode();
        stayInCover.AddCondition(haveWeaponCond);
        stayInCover.AddNode(goCoverAction);
        stayInCover.AddNode(crouchAction);
        stayInCover.AddNode(idleAction);

        seekWeapon = new SequenceNode();
        seekWeapon.AddCondition(seekWeaponCond);
        seekWeapon.AddNode(goWeaponAction);
        seekWeapon.AddNode(pickUpWeaponAction);

        idle = new SelectionNode();
        idle.AddNode(stayInCover);
        idle.AddNode(seekWeapon);

        root.AddNode(haveEnemy);
        root.AddNode(beingShoot);
        root.AddNode(toSafeZone);
        root.AddNode(idle);
    }
Esempio n. 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("无可执行节点!");
        }
    }