I am currently coding a simple Monogame Space Shooter.
I have almost finished my enemy shooting behaviour, but I spent a long time on fixing it and got to the point where I wanted somebody to help me out.
Okay so basiclly I have my 9 enemies (instances of Enemy class) in my Level class, and in level update I do stuff like checking if enemy has been hit by player, if they are Dead so I can remove them from the list etc.
I made a simple Timer class that has only 1 function, uses
var timer += GameTime.ElpasedGameTime.TotalMiliseconds
and simply checks if timer is bigger or equal than function input parameter(or argument?), and func is a bool so it then returns T or F.
Now inside my Enemy.cs claas I have this function:
public bool IsShooting()
{
if (enemyTimer.Wait(randomFloat))
{
shot = true;
return true;
}
else
{
shot = false;
return false;
}
}
enemyTimer = new Timer() & randomFloat = field(variable at the top of the class) in my Enemy class.
And now last but not least(actually the problem here) is this, so now in my Level class I got this little update function:
public void Update(Weapon weapon)
{
if (enemies.Count == 0)
isFinished = true;
if(!isFinished)
{
for (int i = 0; i < enemies.Count; ++i)
{
if (enemies[i].shot)
{
seconds = rand.Next(1, 10);
randMiliseconds = 1000f * seconds;
}
enemies[i].randomFloat = randMiliseconds;
enemies[i].Update();
if (enemies[i].isDead == true) // If enemy is dead, remove it from level
{
enemies.RemoveAt(i);
--i;
}
foreach (Bullet bullet in weapon.bullets)
{
if ( !(i < 0) && weapon.ShotEnemy(bullet, enemies[i])) // If not enemies count == 0 and if
{ // enemy has been shot decrease its health
enemies[i].Health -= weapon.Damage; // and set bullet visibility to false;
bullet.isVisible = false;
}
}
}
}
}
This for loop goes through all the enemies, first if block is used for checking that if the enemy has shot the bullet, then set random value again to something different, if shot has not yet been fired then use the old value and wether it has been changed or not it sets randomFloat field in Enemy class to that value.
Now this is how it works: First all of 9 enemies shoot 1 bullet at the same time (Just about when I open the window, cause I putted randomFloat = 1000f; in my Enemy CTOR), and then randomFloat value is set in Level class to something different, I have message output and FOR EVERY DAMN ENEMY randomFloat is the same.... now that is a problem, I want the rand value to be different for all of them, and it should, right? Becouse those 9 enemies are all different instances of that class and if in each for(;;) loop cycle in my Level.Update() if it gets positive value that enemies[index] has fired a shot it should change it to some number that is very unlikly to be the same for all 9 enemies (seconds = rand.Next(1,20); randomFloat = 1000 * seconds; ). So basiclly randomFloat get random, but it is the same every time for all enemies, and they all shot and will shot at the same random Time
Expected result: Every enemy shooting at different random time