Example #1
0
    private IEnumerator SpawnEnemiesCoroutine()
    {
        yield return(new WaitForSeconds(spawnBeginningWait));

        while (numEnemiesLeft > 0)
        {
            if (!recalculationFinished)
            {
                yield return(new WaitForSeconds(0.5f));

                continue;
            }

            //Chose an amount of enemies to spawn at a time
            int numEnemiesToSpawn = (int)Random.Range(1, maxEnemiesSpawnAtTime);

            for (int index = 0; index < numEnemiesToSpawn; index++)
            {
                //Choose enemy to spawn
                GameObject enemyToSpawn = enemyClass.NextObject();
                yield return(null);

                //Spawn enemy
                GameObject newEnemy = SpawnInWave(enemyToSpawn);
                numEnemiesLeft--;
                yield return(null);

                //Set different enemy guns if alien
                if (newEnemy.CompareTag("Alien"))
                {
                    AIWeapons       weapons = newEnemy.GetComponent <AIWeapons>();
                    GunDefinition[] guns    = null;
                    if (weapons != null)
                    {
                        guns = weapons.guns;
                    }
                    else
                    {
                        weapons = newEnemy.GetComponentInChildren <AIWeapons>();
                        guns    = weapons.guns;
                    }
                    //TODO: correct implementation
                    for (int gunIndex = 0; gunIndex < guns.Length; gunIndex++)
                    {
                        //Choose random gun
                        GameObject gunToSet = enemyWeaponClass.NextObject();
                        yield return(null);

                        guns[gunIndex].ChangeWeapon(gunToSet);
                        yield return(null);
                    }
                }

                yield return(new WaitForSeconds(enemyClass.NextWaitTime())); //pause
            }
        }

        //advance when no more enemies exist
        continueSpawningAsteroids = false;
        yield return(new WaitForSeconds(5));

        manager.AdvanceLevel();
    }