samedi 26 décembre 2015

rand% spawning all my entities in predictable pattern

I have been trying to create a "1D shooter game" with prints in c++. My problem is I am trying to spawn enemies from a list at random moments and I also want to make it random where the enemy spawns (left or right of the screen).

This does not seem to fully work, as I seem to get a stream of contiguous enemies from one side, and only after all those enemies coming from the left side are killed, enemies from the right might appear.

I never seem to get a couple coming from the left and a couple coming from the right at similar times. Its always either one or the other. In addition to this, once an enemy is spawned, the rest seem to spawn almost all at once, and suddenly there is no spawning whatsoever. The pattern seems predictable and wrong.

Here is my method that is called every time in the game loop (the game loop takes care of calling srand(time(NULL)) on every iteration, and after that calls this function.

void Enemy::spawnEnemy()
{
    if (enemList.size() < 5)
    {
        if (!(rand() % 3))
        {
            if (!(rand() % 2))
            {
                Enemy * newEnemy = new Enemy(worldWidth, 'l');
                enemList.push_front(newEnemy);
            }
            else
            {
                Enemy * newEnemy = new Enemy(0, 'r');
                enemList.push_front(newEnemy);
            }
        }
    }
}

Thank you very much in advance. I really hope someone can solve this problem, because I have been struggling to find an answer for it!




Aucun commentaire:

Enregistrer un commentaire