Ejemplo n.º 1
0
    // coroutine responsible to if we keep spacebar pressed then the ship will
    // continuously firing
    IEnumerator ShootContinuously()
    {
        // put a while here just to make sure the coroutine will continuously happening
        while (true)
        {
            // instantites 3 differents shoots depending how much power up you collected
            if (!playerPU.GetPowerUpCollected())
            {
                GameObject laser = Instantiate(laserPrefab,
                                               new Vector2(transform.position.x, transform.position.y + projectileMiddleOffset),
                                               Quaternion.identity) as GameObject;
                laser.GetComponent <Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
            }


            if (playerPU.GetPowerUpCollected() && playerPU.GetPowerUpLaserCount() == 1)
            {
                GameObject laser = Instantiate(laserPrefab,
                                               new Vector2(transform.position.x, transform.position.y + projectileMiddleOffset),
                                               Quaternion.identity) as GameObject;
                laser.GetComponent <Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);

                GameObject laser1 = Instantiate(laserPrefab,
                                                new Vector2(transform.GetChild(0).transform.position.x,
                                                            transform.GetChild(0).transform.position.y + projectileSideOffset),
                                                Quaternion.identity) as GameObject;
                laser1.GetComponent <Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
            }
            else if (playerPU.GetPowerUpCollected() && playerPU.GetPowerUpLaserCount() >= 2)
            {
                GameObject laser = Instantiate(laserPrefab,
                                               new Vector2(transform.position.x, transform.position.y + projectileMiddleOffset),
                                               Quaternion.identity) as GameObject;
                laser.GetComponent <Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);

                GameObject laser1 = Instantiate(laserPrefab,
                                                new Vector2(transform.GetChild(0).transform.position.x,
                                                            transform.GetChild(0).transform.position.y + projectileSideOffset),
                                                Quaternion.identity) as GameObject;
                laser1.GetComponent <Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);

                GameObject laser2 = Instantiate(laserPrefab,
                                                new Vector2(transform.GetChild(1).transform.position.x,
                                                            transform.GetChild(1).transform.position.y + projectileSideOffset),
                                                Quaternion.identity) as GameObject;
                laser2.GetComponent <Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
            }

            AudioSource.PlayClipAtPoint(laserAudio, Camera.main.transform.position, laserVolume);

            // here we set the time to wait after we finish execution the coroutine
            // before starting again, to simplify it's define the fire rate of the ship
            yield return(new WaitForSeconds(playerPU.GetIncreaseFiringRate()));
        }
    }