Exemple #1
0
    public void SecondaryAttack(int shotsLeft)
    {
        if (!secondaryOnCooldown && !primaryOnCooldown || (followUpShot && numShots > 0)) //Fire if primary/secondary attacks are not on cooldown or if followup shot is the one being fired
        {
            Vector3 mousePosition = Input.mousePosition;
            float   radIncrement  = spread / numProjectilesPerShot; //Degrees apart each shot should be from another if firing multiple projectiles

            for (int i = 0; i < numProjectilesPerShot; i++)
            {
                projectileInstance = Instantiate(projectilePrefab, attackHitbox.position, Quaternion.identity) as Rigidbody2D;

                if (projectileInstance.GetComponent <ElementalEffects>() != null) //make sure projectile can have an element
                {
                    projectileInstance.GetComponent <ElementalEffects>().element = projectileInstance.GetComponent <ElementalEffects>().element | projectileElement;
                }

                float angle = GetMouseAngleToPlayer();

                if (numProjectilesPerShot > 1 && numProjectilesPerShot % 2 == 1)                                      //If we shoot more than one projectile, offset their angle and direction fired by the number of shots
                {
                    angle = angle - spread + (radIncrement * (i + 1)) + (radIncrement * (numProjectilesPerShot / 2)); //There is a different formula for even/odd number of projectiles
                }
                else if (numProjectilesPerShot > 1 && numProjectilesPerShot % 2 == 0)
                {
                    angle = angle - spread + (radIncrement * (i + 1)) + ((radIncrement / 2) * (numProjectilesPerShot / 2)); //TODO maybe fix this formula up later, seems way to complex for what it does
                }
                float y = Mathf.Sin(angle);
                float x = Mathf.Cos(angle);
                //print("i: " + i + " Angle: " + Mathf.Rad2Deg*angle + " RadIncrement: " + Mathf.Rad2Deg * radIncrement);
                projectileInstance.AddForce(new Vector3(x, y, 0) * projectileSpeed);
                projectileInstance.transform.Rotate(new Vector3(0, 0, Mathf.Rad2Deg * angle + 90));

                PlayerProjectile projectileScript = projectileInstance.GetComponent <PlayerProjectile>();
                if (isHoming && projectileScript != null) //Pass on homing projectile
                {
                    projectileScript.SetHomingShots(true);
                }

                if (maxBounces > 0) //Pass on bounces
                {
                    projectileScript.SetBounces(maxBounces);
                }

                if (isExplosive) //Pass on explosive
                {
                    projectileScript.ShouldExplode(isExplosive);
                }

                //Set width of projectile to the modifier
                projectileInstance.transform.localScale = new Vector3(projectileInstance.transform.localScale.x * projectileWidthModifier, projectileInstance.transform.localScale.y, projectileInstance.transform.localScale.z);
                //print(projectileInstance.transform.localScale);
            }

            secondaryOnCooldown = true;
            StartCoroutine(SecondaryCooldown(shotsLeft - 1)); //Start routine to put a cooldown on secondary atk
        }
    }