Day 15. Balanced spawning. Back to wave system task gave me unbalanced debugging times.
I created a function inside the SpawnManager script that lets the programmer pass the percentage of enemies against power-ups. 1 to 100. Calculation happens every new wave.
//100 = _percentEnemies + percentPowerups
//100 = 70 + 30
//? = 3 + ?
// 70 / 3 = 23.3 — percent of one enemy
// 100/23.3 = 4.29 == 100%
// 4.29–3 = 1.29 Round up to 2
float oneEnemyPercent = _percentEnemies / enemiesSpawned;
_currentWavePowerupToSpawn = Mathf.CeilToInt(Mathf.Ceil(100f / oneEnemyPercent) — enemiesSpawned);
I used function Mathf.Ceil that round float number up. 10.01 will be 11.
During a wave, power-ups will be limited by the function above.
Limit Health and increase Ammo power-up task I granted to The Great Random to decide if health should be spawned. In case if we already randomized Health to be spawned, another Random function randomize between 0 and 1 gave less chance for health to be spawned and instead it spawn ammo.
if (randomPowerup == 4 && Random.Range(0, 2) == 0) randomPowerup = 3; //ammo frequent. health rarely
So glad I finished this one.
Today I also stuck with the debugging wave system. The problem was at the enemy counter. Sometimes killed enemies was more then spawned. It leads to the situation when enemies stop spawning in the middle of the game. It made me puzzled. I did not find the root. Sometimes when an enemy object going to be destroyed, I use a variable to decrease current active enemies before that method. currentEnemyCount going below zero. To fix this bug I counted Enemies with the function FindGameObjectsWithTag.
int _enemyAlive = GameObject.FindGameObjectsWithTag(“Enemy”).Length;