Exemplo n.º 1
0
    void Patrol()
    {
        navAgent.isStopped = false;
        navAgent.speed     = walkSpeed;

        patrolTimer += Time.deltaTime;

        if (patrolTimer > patrolForThisTime)
        {
            SetNewRandomDestination();

            //Reset Patrol timer
            patrolTimer = 0f;
        }

        if (navAgent.velocity.sqrMagnitude > 0)
        {
            enemyAnim.Walk(true);
            enemyAnim.Run(false);
            enemyAnim.Idle(false);
        }
        else
        {
            enemyAnim.Walk(false);
            enemyAnim.Run(false);
        }

        if (Vector3.Distance(transform.position, target.position) <= chaseDistance)
        {
            enemyAnim.Walk(false);
            enemyAnim.Idle(false);
            enemyAnim.Run(true);

            enemyState = EnemyState.CHASE;

            //PLAY SPOTTED AUDIO
        }

        if (Vector3.Distance(transform.position, target.position) <= attackDistance)
        {
            enemyAnim.Walk(false);
            enemyAnim.Idle(false);
            enemyAnim.Run(false);

            enemyState = EnemyState.ATTACK;

            if (chaseDistance != currentChaseDistance)
            {
                chaseDistance = currentChaseDistance;
            }
            else if (Vector3.Distance(transform.position, target.position) > chaseDistance)
            {
                enemyAnim.Run(false);
                enemyState = EnemyState.PATROL;

                patrolTimer = patrolForThisTime;
                if (chaseDistance != currentChaseDistance)
                {
                    chaseDistance = currentChaseDistance;
                }
            }
        }
    }
Exemplo n.º 2
0
    protected virtual void StateMachine()
    {
        #region STATE CONFIGS
        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        var idle      = new State <Inputs>(CommonState.IDLE);
        var onSigth   = new State <Inputs>(CommonState.ONSIGHT);
        var pursuit   = new State <Inputs>(CommonState.PURSUIRT);
        var searching = new State <Inputs>(CommonState.SEARCHING);
        var attack    = new State <Inputs>(CommonState.ATTACKING);
        var die       = new State <Inputs>(CommonState.DIE);

        StateConfigurer.Create(idle)
        .SetTransition(Inputs.ON_LINE_OF_SIGHT, onSigth)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(onSigth)
        .SetTransition(Inputs.PROBOCATED, pursuit)
        .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(pursuit)
        .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(Inputs.IN_RANGE_TO_ATTACK, attack)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(searching)
        .SetTransition(Inputs.TIME_OUT, idle)
        .SetTransition(Inputs.ON_LINE_OF_SIGHT, pursuit)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(attack)
        .SetTransition(Inputs.OUT_RANGE_TO_ATTACK, pursuit)
        .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(die).Done();


        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        #endregion
        #region STATES
        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////

        //******************
        //*** IDLE
        //******************
        idle.OnEnter += x =>
        {
            myRb.velocity = Vector2.zero;
            anim.Idle();
        };
        idle.OnUpdate += () =>
        {
            Deb_Estado = "IDLE";
            if (LineOfSight())
            {
                Debug.Log("Line of sigth");
                SendInputToFSM(Inputs.ON_LINE_OF_SIGHT);
            }

            CheckBullet();
        };

        //******************
        //*** ON SIGHT
        //******************
        onSigth.OnEnter += x =>
        {
            feedbackstate.SetStateGraphic(preset.sprite_OnSight);
        };
        onSigth.OnUpdate += () =>
        {
            Deb_Estado = "ON SIGTH";
            if (LineOfSight())
            {
                LookSmooth();
                OnSight_CountDownForProbocate();
            }
            else
            {
                timer_to_probocate = 0; SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT);
            }
        };
        onSigth.OnExit += x =>
        {
            feedbackstate.SetStateGraphic();
        };

        //******************
        //*** PURSUIT
        //******************
        pursuit.OnEnter += x =>
        {
            anim.Run();
        };

        pursuit.OnUpdate += () =>
        {
            Deb_Estado = "PURSUIT";
            if (LineOfSight())
            {
                FollowPlayer();

                if (IsInDistanceToAttack())
                {
                    SendInputToFSM(Inputs.IN_RANGE_TO_ATTACK);
                }
            }
            else
            {
                timer_to_probocate = 0; SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT);
            }
        };

        pursuit.OnExit += x =>
        {
            myRb.velocity = Vector2.zero;
        };

        //******************
        //*** SEARCH
        //******************
        searching.OnEnter += x =>
        {
            anim.Walk();
            feedbackstate.SetStateGraphic(preset.sprite_Search);
        };

        searching.OnUpdate += () =>
        {
            CheckBullet();

            Deb_Estado = "SEARCH";
            if (LineOfSight())
            {
                SendInputToFSM(Inputs.ON_LINE_OF_SIGHT);
            }
            else
            {
                transform.Rotate(0, 0, 2);
                OutSight_CountDownForIgnore();
            }
        };

        searching.OnExit += x =>
        {
            feedbackstate.SetStateGraphic();
        };

        //******************
        //*** ATTACK
        //******************
        attack.OnUpdate += () =>
        {
            Deb_Estado = "ATTACK";
            if (LineOfSight())
            {
                LookSmooth();

                if (IsInDistanceToAttack())
                {
                    anim.Attack();
                    Attack();
                }
                else
                {
                    SendInputToFSM(Inputs.OUT_RANGE_TO_ATTACK);
                }
            }
            else
            {
                SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT);
            }
        };

        attack.OnExit += x =>
        {
            timer = 0;
        };

        //******************
        //*** DEATH
        //******************
        die.OnEnter += x =>
        {
            Deb_Estado = "DEATH";
            canMove    = false;
            gameObject.GetComponent <Collider2D>().enabled = false;
            anim.Die();
            lifebar.Off();
            del_to_remove(this);
        };

        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        #endregion

        _myFsm = new EventFSM <Inputs>(idle);
    }
Exemplo n.º 3
0
    //=====================================================

    private IEnumerator Gathering(Action onComplete)
    {
        var wayPointIndex = Random.Range(0, _wayPoints.Count);

        while (true)
        {
            // Select new target for gem-gathering
            var lastIndex = wayPointIndex;
            do
            {
                wayPointIndex = Random.Range(0, _wayPoints.Count);
            }while(wayPointIndex == lastIndex);

            // Target gem if available
            var isTargetingGem = IsGemAvailable();

            // Else target next waypoint for hunting / gathering gems
            if (isTargetingGem == false)
            {
                _thisAgent.SetDestination(_wayPoints[wayPointIndex].transform.position);
            }

            // Set walk speed
            _thisAgent.speed = _walkSpeed;
            _animation.Walk();

            // Move toward target
            while ((_thisTransform.position - _thisAgent.destination).sqrMagnitude > 0.5f)
            {
                // Check for any new gems found
                if (isTargetingGem == false)
                {
                    isTargetingGem = IsGemAvailable();
                }

                //guard_motor.facingDirection = patrolPoints[i].position - _transform.position;
                yield return(null);
            }

            // Arrived at target ( gem or gathering point )
            if (isTargetingGem == true)
            {
                // *** Gem gathered -> EAT gem ***
                if (onComplete != null)
                {
                    onComplete();
                }
            }

            //guard_motor.facingDirection = patrolPoints[i].position - _transform.position;

            // Stop movement
            _thisAgent.speed = 0.0f;
            _animation.Stop();

            // Play audio fx
            _audioSource.PlayOneShot(_clipChatter);

            yield return(new WaitForSeconds(1.0f));
        }
    }