/// <summary> /// [COROUTINE] called by effects to spawn enemies mid-wave /// </summary> /// <param name="wave">the wave to spawn</param> /// <param name="spawnLocation">where to spawn it</param> /// <param name="firstDestination">the first location for them to travel to before pathfinding. This should be the start or end of a path segment.</param> public IEnumerator spawnWaveAt(WaveData wave, Vector2 spawnLocation, Vector2 firstDestination) { //flag the wave as started wavesSpawning++; totalSpawnCount += wave.spawnCount; //init float timeBetweenSpawns = wave.time / wave.spawnCount; //delay between each spawn SpawnerScript spawner = ((GameObject)Instantiate(spawnerPrefab)).GetComponent <SpawnerScript>(); spawner.forceFirstPath(spawnLocation, firstDestination); //slight delay before spawning yield return(new WaitForSeconds(0.1f)); //spawn monsters. float timeToNextSpawn = 0; while ((wave.spawnCount - wave.spawnedThisWave) > 0) { yield return(null); //wait for the next frame timeToNextSpawn -= Time.deltaTime; //reduce time until next spawn by amount of time between frames wave.time -= Time.deltaTime; //update the wave data also so that the status text can update while (timeToNextSpawn < 0.0) //this is a loop in case multiple spawns happen in one frame { spawner.Spawn(-timeToNextSpawn, wave.enemyData); //spawn enemy. spawn timer provided so the enemy can place itself properly when framerate is low timeToNextSpawn += timeBetweenSpawns; //update spawn timer //update spawn counters wave.spawnedThisWave++; totalSpawnedThisWave++; //bail if we have finished spawning baddies if (wave.spawnedThisWave == wave.spawnCount) { break; } } } //wave is over wavesSpawning--; Destroy(spawner.gameObject); yield break; }