Esempio n. 1
0
    protected override IEnumerator Attacking()
    {
        isAttacking          = true;
        agent.updateRotation = true;
        List <Move> availableAttacks = GetAvailableMoves();

        if (availableAttacks.Count > 0)
        {
            //choose random attack
            currentMove = availableAttacks[Random.Range(0, availableAttacks.Count)];
            battleManager.CurrentMove = currentMove;
            Vector3    startPos = transform.position;
            Quaternion startRot = transform.rotation;

            EnemyMelee      melee      = currentMove as EnemyMelee;
            EnemySelfBuff   selfBuff   = currentMove as EnemySelfBuff;
            EnemySpell      spell      = currentMove as EnemySpell;
            SpellProjectile projectile = null;

            if (melee)
            {
                hurtbox.enabled = false;
                agent.SetDestination(player.GetAttackPosition().position);
                agent.stoppingDistance = attackPositionOffset;
                yield return(new WaitUntil(() => !agent.pathPending));

                yield return(new WaitUntil(() => agent.pathStatus == NavMeshPathStatus.PathComplete && agent.remainingDistance <= attackPositionOffset));
            }

            animator.SetTrigger(currentMove.animationName);
            if (melee)
            {
                if (melee.hitboxDelay > 0)
                {
                    StartCoroutine(HandleHitbox(melee));
                }
                else
                {
                    hitboxes[melee.hitboxID].enabled = true;
                }
            }
            yield return(new WaitUntil(() => animator.GetCurrentAnimatorClipInfo(0)[0].clip.name.Contains(currentMove.animationName)));

            PayForAttack();

            if (spell != null)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation,
                                                     Quaternion.LookRotation(player.transform.position - transform.position),
                                                     Time.deltaTime);
                for (int i = 0; i < spell.projectile.Length; i++)
                {
                    yield return(new WaitForSeconds(spell.delay[i]));

                    projectile = Instantiate(spell.projectile[i], spellSpawnTransforms[spell.spawnTransformID].position,
                                             transform.rotation, transform);
                    projectile.LockOnTarget(player.GetProjectileTarget());
                }
            }

            if (selfBuff != null)
            {
                perks.Add(selfBuff.buff);
            }

            yield return(new WaitUntil(() => !animator.GetCurrentAnimatorClipInfo(0)[0].clip.name.Contains(currentMove.animationName)));

            if (melee != null)
            {
                agent.SetDestination(startPos);
                agent.stoppingDistance           = 0f;
                hitboxes[melee.hitboxID].enabled = false;
                yield return(new WaitUntil(() => !agent.pathPending));

                yield return(new WaitUntil(() => agent.pathStatus == NavMeshPathStatus.PathComplete && agent.remainingDistance == 0));

                hurtbox.enabled = true;

                while (transform.rotation != startRot)
                {
                    transform.rotation = Quaternion.RotateTowards(transform.rotation, startRot, 10f);
                    yield return(null);
                }
            }
            yield return(new WaitUntil(() => projectile == null));
        }
        yield return(base.Attacking());
    }