Пример #1
0
    void ComboAttackState()
    {
        float distanceToPlayer = Mathf.Abs(player.transform.position.x - transform.position.x);

        //If the player is within melee attack distance
        if (distanceToPlayer <= meleeAttackDistance)
        {
            //If the player is within my bounds
            if (distanceToPlayer < horizontalOffset.x + player.GetComponent <BoxCollider2D>().bounds.extents.x)
            {
                adjustTimer += Time.deltaTime;

                //Move away after a short delay
                if (adjustTimer >= adjustDelay)
                {
                    rb.velocity = new Vector2(adjustVelocity * -GetPlayerDirection(), 0.0f);
                }
            }
            //If the player is within adjacent distance, stop moving
            else if (distanceToPlayer < adjacentDistance)
            {
                adjustTimer = 0;

                rb.velocity = Vector2.zero;
            }
            //If currently doing melee warning, stop moving
            else if (distanceToPlayer >= adjacentDistance && isMeleeAttacking && !hasMeleeAttacked)
            {
                adjustTimer = 0;

                rb.velocity = new Vector2(meleeWarningVelocity * GetPlayerDirection(), 0.0f);
            }
            //If the player is out of adjacent distance, move towards the player
            else if (distanceToPlayer >= adjacentDistance)
            {
                adjustTimer = 0;

                rb.velocity = new Vector2(runVelocity * GetPlayerDirection(), 0.0f);
            }

            if (player.transform.position.y > meleeVerticalRange)
            {
                if (isMeleeAttacking && !hasMeleeAttacked)
                {
                    hasMeleeAttacked = true;

                    if (meleeTimer >= meleeCooldown)
                    {
                        isMeleeAttacking = false;
                    }
                }

                if (rangedTimer >= rangedCooldown)
                {
                    rangedTimer = 0;

                    RangedAttack();
                }
            }
            else
            {
                //If melee attack sequence started, activate melee warning
                if (!isMeleeAttacking)
                {
                    isMeleeAttacking = true;

                    hasMeleeAttacked = false;

                    meleeTimer = 0;

                    ea.MeleeWarning();
                }

                //If melee attack is off cooldown, begin melee attack sequence
                if (meleeTimer >= meleeCooldown && isMeleeAttacking)
                {
                    isMeleeAttacking = false;
                }

                //If melee warning is done, melee attack
                if (meleeTimer >= meleeWarningTime && isMeleeAttacking && !hasMeleeAttacked)
                {
                    hasMeleeAttacked = true;

                    ea.MeleeAttack();

                    MeleeAttack();
                }
            }
        }
        //If the player is out of melee attack distance and still melee attacking, wait until melee warning is done
        else if (distanceToPlayer > meleeAttackDistance && isMeleeAttacking && !hasMeleeAttacked)
        {
            rb.velocity = new Vector2(meleeWarningVelocity * GetPlayerDirection(), 0.0f);

            if (meleeTimer >= meleeWarningTime)
            {
                ea.MeleeAttack();

                hasMeleeAttacked = true;
            }
        }
        //If the player is out of melee attack distance and just finished melee attacking, stop melee attacking
        else if (distanceToPlayer > meleeAttackDistance && isMeleeAttacking && hasMeleeAttacked)
        {
            if (meleeTimer >= meleeCooldown)
            {
                isMeleeAttacking = false;
            }
        }
        //If the player is within melee engage distance, move towards the player
        else if (distanceToPlayer <= meleeEngageDistance && !isMeleeAttacking)
        {
            rb.velocity = new Vector2(runVelocity * GetPlayerDirection(), 0.0f);

            hasRangeAttacked = false;

            isTryingToEngage = false;

            canEngage = false;
        }
        //If the player is out of melee engage distance
        else if (distanceToPlayer > meleeEngageDistance && !isMeleeAttacking)
        {
            //If has not range attacked, stop moving and attack
            if (!hasRangeAttacked)
            {
                rb.velocity = Vector2.zero;

                if (rangedTimer >= rangedCooldown)
                {
                    RangedAttack();

                    hasRangeAttacked = true;

                    Invoke("EnableEngage", rangedCooldown);
                }
            }
            //If has range attacked and has started attempting to engage, move towards the player in an attempt to reach melee engage distance
            else if (isTryingToEngage)
            {
                if (transform.position.x < tryEngageRunPosition && tryEngageRunDirection == 1)
                {
                    rb.velocity = new Vector2(runVelocity, 0.0f);
                }
                else if (transform.position.x > tryEngageRunPosition && tryEngageRunDirection == -1)
                {
                    rb.velocity = new Vector2(-runVelocity, 0.0f);
                }
                else
                {
                    rb.velocity = Vector2.zero;

                    hasRangeAttacked = false;

                    isTryingToEngage = false;
                }
            }
            //If has range attacked, attempt to engage the player by moving towards them
            else if (canEngage)
            {
                isTryingToEngage = true;

                canEngage = false;

                tryEngageRunPosition = transform.position.x + (tryEngageRunDistance * GetPlayerDirection());

                if (transform.position.x < tryEngageRunPosition)
                {
                    tryEngageRunDirection = 1;
                }
                else if (transform.position.x > tryEngageRunPosition)
                {
                    tryEngageRunDirection = -1;
                }
            }
        }
    }