jeudi 27 février 2020

I want to randomize the succession of enemies spawned from the number of different enemies that will spawn in a wave. (TD GAME), How do I do this?

    private void DetermineEnemySelection()
    {
        int _totalCount = 0;
        for(int i = 0; i < enemyCounts.Length; i++)
        {
            _totalCount += enemyCounts[i];
        }

        Stack<int> rn = new Stack<int>();

        for(int i = 0; i < _totalCount; i++)
        {
            int _sel = Random.Range(0, 5);
            if(enemyCounts[_sel] == 0)
            {

            }
        }
    }

Context: enemyCounts[] contains the number of enemies per type (enemyCounts[0] is the number of enemies of type 1 that will spawn in a wave)

_totalCount is the total number of enemies that will spawn in the wave, including all enemy types. My Idea was to have a stack of numbers that will be the enemy type id. The Numbers go from 0 to 6 and will be pushed randomly. If the enemyCounts[_sel] is empty, I want to create a new random number and if possible exclude that random number from the range (so if enemyCounts[3] is empty, maybe generate the numbers 0 1 2 4 5)

I can't find a solution. I already had one that involved a while loop but this crashed the game after a while for some reason reaching 2701470 attempts to generate the next enemy which results in a crash. I kinda want a stack that will in the end have the length of _totalCount (so you dont get duplicate enemies) and looks kinda like this {0,1,2,0,4,1,0,3,2,0,3,5,1,5,3,3,2,2,0} etc. meaning that this will be the succession of enemies.

What is the best way to approach this, without any endless loops?




Aucun commentaire:

Enregistrer un commentaire