示例#1
0
    private void InitStateMachine()
    {
        blackboard = new StateBlackboard();
        blackboard.Add <bool>(BlackboardKey.DamageFlag, false);
        blackboard.Add <bool>(BlackboardKey.RepeatDamageFlag, false);
        blackboard.Add <Transform>(BlackboardKey.Transform, transform);
        blackboard.Add <float>(BlackboardKey.AttackRange, 0f);

        EnemyIdleState idleState = EnemyIdleState.Create(blackboard, animator, "Idle", 0);
        StateNode      idleNode  = new StateNode(idleState, new Condition(() => { return(true); }));

        SetAnimTriggerAction walkAction       = new SetAnimTriggerAction(blackboard, animator, "Run");
        MeleeAttackState     meleeAttackState = MeleeAttackState.Create(blackboard, animator, navAgent, attackValidator.MeleeAttacks, transform, BlackboardKey.AttackRange, BlackboardKey.AnimTrigger, walkAction, 300);
        StateNode            attackNode       = new StateNode(meleeAttackState, new Condition(() => { return(target != null); }));

        DamageState damageState = DamageState.Create(blackboard, animator, "Damage", 500);
        StateNode   damageNode  = new StateNode(damageState, new Condition(() => { return(blackboard.Get <bool>(BlackboardKey.DamageFlag)); }));

        DeathState deathState = DeathState.Create(blackboard, animator, "Death", int.MaxValue);
        StateNode  deathNode  = new StateNode(deathState, new Condition(() => { return(!self.IsAlive); }));

        List <StateNode> nodes = new List <StateNode>();

        nodes.Add(idleNode);
        nodes.Add(attackNode);
        nodes.Add(damageNode);
        nodes.Add(deathNode);

        stateMachine = new StateMachine(blackboard, nodes, idleState);
    }
    private void InitStateMachine()
    {
        blackboard = new StateBlackboard();
        blackboard.Add <bool>(BlackboardKey.DamageFlag, false);
        blackboard.Add <bool>(BlackboardKey.RepeatDamageFlag, false);
        blackboard.Add <Transform>(BlackboardKey.Transform, transform);
        blackboard.Add <float>(BlackboardKey.RunRange, 3f);

        EnemyIdleState idleState = EnemyIdleState.Create(blackboard, animator, "Idle", 0);
        StateNode      idleNode  = new StateNode(idleState, new Condition(() => { return(true); }));

        SetAnimTriggerAction       idleAction        = new SetAnimTriggerAction(blackboard, animator, "Idle");
        SetAnimTriggerAction       runAction         = new SetAnimTriggerAction(blackboard, animator, "Run");
        TimerAction                timerAction       = new TimerAction(blackboard, 0.5f, 1.5f);
        SetRandomDestinationAction destinationAction = new SetRandomDestinationAction(blackboard, transform.position, 6f);
        MoveToDestinationAction    moveToAction      = new MoveToDestinationAction(blackboard, navAgent);
        SetSkillAction             setSkillAction    = new SetSkillAction(blackboard, skillController);
        LookAtTargetAction         lookAtAction      = new LookAtTargetAction(blackboard, transform);
        SetAnimTriggerAction       attackAction      = new SetAnimTriggerAction(blackboard, animator, BlackboardKey.AnimTrigger, true);
        RangedAttackState          attackState       = new RangedAttackState(blackboard, idleAction, timerAction, destinationAction, runAction, moveToAction, setSkillAction, lookAtAction, attackAction, 300);
        StateNode attackNode = new StateNode(attackState, new Condition(() => target != null));

        DamageState damageState = DamageState.Create(blackboard, animator, "Damage", 500);
        StateNode   damageNode  = new StateNode(damageState, new Condition(() => { return(blackboard.Get <bool>(BlackboardKey.DamageFlag)); }));

        DeathState deathState = DeathState.Create(blackboard, animator, "Death", int.MaxValue);
        StateNode  deathNode  = new StateNode(deathState, new Condition(() => { return(!self.IsAlive); }));

        List <StateNode> nodes = new List <StateNode>();

        nodes.Add(idleNode);
        nodes.Add(attackNode);
        nodes.Add(damageNode);
        nodes.Add(deathNode);

        stateMachine = new StateMachine(blackboard, nodes, idleState);
    }