예제 #1
0
    void StopFiring()
    {
        for (int i = 0; i < currGuns.Length; i++)
        {
            OnStopFiring?.Invoke(currGuns[i]);
        }

        isFiring = false;
    }
예제 #2
0
    IEnumerator CoFire()
    {
        while (isFiring && state == ShipStates.controllable)
        {
            //we scan the last firing times of all current level's current weapon's guns
            for (int i = 0; i < currGuns.Length; i++)
            {
                //if it's not yet time to stop firing...
                if (Time.time <= currGuns[i].FireStartTime + currGuns[i].fireDuration)
                {
                    //...we check if it's time to fire a bullet – or to start a laser
                    if (Time.time >= currGuns[i].NextFireTime)
                    {
                        if (currGuns[i].type == GunData.GunTypes.Bullet)                         //If the gun is of Bullet type
                        {
                            //fire a new bullet from this gun and, by default, make it child of the Ship (for those that must remain children of their ships)
                            Bullet bullet = Instantiate(currGuns[i].bulletPrefab, currGuns[i].cannon.position,
                                                        currGuns[i].cannon.rotation, transform);

                            //update time for this gun
                            currGuns[i].NextFireTime = Time.time + currGuns[i].fireDelay;
                        }
                        else
                        {
                            //start a new laser from this gun
                            ShipLaser laser = Instantiate <ShipLaser>(currGuns[i].laserPrefab, currGuns[i].cannon.position,
                                                                      //currGuns[i].cannon.rotation);
                                                                      Quaternion.identity);
                            laser.Init(currGuns[i].cannon);

                            //update next fire time for this gun
                            currGuns[i].NextFireTime = Time.time + 9999f;                             //so that we won't instantiate another laser for this gun during this fire session
                        }
                    }
                }
                //if it's time to stop firing, check to avoid going through this block more than once per firing session
                else if (currGuns[i].FireStartTime > 0f)
                {
                    //negative val for above reasons, and equal to abs. same val so that we can reinit Laser gun correctly when StartFire
                    currGuns[i].FireStartTime = -currGuns[i].FireStartTime;
                    OnStopFiring?.Invoke(currGuns[i]);                     //for oscillator & laser

                    //currGuns[i].NextFireTime = Time.time + 9999f; //normally useless
                }
            }
            yield return(null);
        }
    }