Beispiel #1
0
        /// <summary>
        /// Construct a state mahcine to provide the behaviour outlined in the class comment.
        /// Individual states are created and transitions added to them. These transitions are
        /// used in the states Reason method.
        /// Once all states and transitions have been created the states are added to the state machine,
        /// which will handle transitions.
        /// </summary>
        protected override void ConstructFSM()
        {
            // Get necessary components.
            var movementHandler = GetComponent <MovementHandler> ();
            var animator        = GetComponent <Animator> ();
            var waypointManager = GetComponent <WaypointManager> ();

            // Build FSM actions.
            var followPathAction   = new FollowPathToRandomAction(transform, followPathSpeed, avoidanceMask, waypointManager, movementHandler);
            var seekPlayerAction   = new SeekTargetAction(transform, player, movementHandler, seekSpeed, avoidanceMask, attackRange * 0.8f);
            var attackPlayerAction = new MeleeTowardsTargetAction(transform, player, delayBetweenAttacks,
                                                                  attackDamage, attackKnockbackForce, attackRange, hitMask, animator);

            // Build FSM reasons.
            var playerInSightReason    = new TargetInSightReason(this, player, obstacleMask, GlobalStateData.FSMStateID.SeekTarget);
            var playerNotInSightReason = new TargetNotInSightReason(this, player, obstacleMask, GlobalStateData.FSMStateID.FollowPathToTarget);
            var playerInRangeReason    = new TargetInRangeReason(this, player, attackRange, GlobalStateData.FSMStateID.AttackTarget);
            var playerNotInRangeReason = new TargetNotInRangeReason(this, player, attackRange, GlobalStateData.FSMStateID.SeekTarget);
            //var hitObstacleReason = new HitObstacleReason (this, movementHandler, obstacleMask, GlobalStateData.FSMStateID.FollowPathToTarget);


            // Create states.
            // Follow path ro random tile.
            var findPlayerActions = new List <FSMAction> ()
            {
                followPathAction
            };
            var findPlayerReasons = new List <FSMReason> ()
            {
                playerInSightReason
            };

            stateMachine.AddState(new State(GlobalStateData.FSMStateID.FollowPathToTarget, findPlayerActions, findPlayerReasons));

            // Seek to players position.
            var seekPlayerActions = new List <FSMAction> ()
            {
                seekPlayerAction
            };
            var seekPlayerReasons = new List <FSMReason> ()
            {
                playerInRangeReason, playerNotInSightReason
            };

            stateMachine.AddState(new State(GlobalStateData.FSMStateID.SeekTarget, seekPlayerActions, seekPlayerReasons));

            // Attack player.
            var attackActions = new List <FSMAction> ()
            {
                seekPlayerAction, attackPlayerAction
            };
            var attackPlayerReasons = new List <FSMReason> ()
            {
                playerNotInRangeReason
            };

            stateMachine.AddState(new State(GlobalStateData.FSMStateID.AttackTarget, attackActions, attackPlayerReasons));
        }
Beispiel #2
0
        protected override void ConstructFSM()
        {
            // Get necessary components.
            var movementHandler = GetComponent <MovementHandler> ();

            // Build FSM actions.
            var idleAction = new IdleAction();

            // Build FSM reasons.
            var playerInSightReason = new TargetInSightReason(this, player, obstacleMask, GlobalStateData.FSMStateID.SeekTarget);

            // Create states.
            // Idle action: do nothing until player in sight
            stateMachine.AddState(new State(GlobalStateData.FSMStateID.Idle, idleAction, playerInSightReason));

            // Seek player.
        }