Exemplo n.º 1
0
    void Attack()
    {
        navAgent.velocity  = Vector3.zero;
        navAgent.isStopped = true;

        attackTimer += Time.deltaTime;

        if (attackTimer > waitBeforeAttack)
        {
            enemyAnim.Attack();

            attackTimer = 0f;

            //play attack sound
        }

        if (Vector3.Distance(transform.position, target.position) > attackDistance + chaseAfterAttackDistance)
        {
            enemyState = EnemyState.CHASE;
        }
    }
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);
    }