mardi 5 septembre 2017

TowerDefense game, randomising enemyspawns using vector

I am creating a tower defense game, and im randomizing the enemies using the code below.

The code pushes back what enemies that are eligible to spawn into the vector depending on what game level the user is on. Later it randomizes an integer depending on the size of the vector, in that way I can safely randomize between all enemies. Finally it returns the enemy type that is randomized.

The issue at hand however, is that the only enemy that spawns is the angry farmer. When the randomized integer equals 0(angry farmer), then the vector contains options for different enemies to spawn. However, when the integer equals something different, the vector contains multiply angry farmers instead of the actual enemy that shouldve spawned. See image for better understanding.

See the second last section from bottom, instead of the vector containing farmer and squire[0, 1], it contains only farmers[0, 0] In this case, the enemy spawned shouldve been a squire, but the vector only contains options for farmers. ]1

I have no idea how this should or can be fixed, Ive got no idea what the issue can be, the code doesnt seem to be wrong to me. The code is below, any help at all is very appreciated, cheers fellas!

enum ENEMY_TYPE {
    ET_ANGRYFARMER = 0,
    ET_SQUIRE = 1,
    ET_KNIGHT = 2,
    ET_KNIGHTONHORSE = 3,
    ET_WIZARD = 4,
    ET_HEALER = 5
};
ENEMY_TYPE CGame::decideWhatEnemyToSpawn() {
        std::vector<ENEMY_TYPE> availableEnemies;
        availableEnemies.push_back(ET_ANGRYFARMER);

        //Adds enemies to vector depending on game level
        if (m_gameLevel >= 3) 
            availableEnemies.push_back(ET_SQUIRE);
        if (m_gameLevel >= 5) 
            availableEnemies.push_back(ET_KNIGHT);
        if (m_gameLevel >= 8) 
            availableEnemies.push_back(ET_KNIGHTONHORSE);
        if (m_gameLevel >= 11)
            availableEnemies.push_back(ET_WIZARD);
        if (m_gameLevel >= 15)
            availableEnemies.push_back(ET_HEALER);

        //randoms which enemy to spawn
        int maxVal = availableEnemies.size(),
            randInt = (m_gameLevel >= 3) ? rand() % maxVal : 0;

        /// DEBUG ///
        std::cout << "-----------------------------------------------" << std::endl;
        std::cout << "gameLevel: " + std::to_string(m_gameLevel) +  "\nrandInt: " + std::to_string(randInt) << "\nMaxVal: " + std::to_string(maxVal) + "\nenemy: " + std::to_string(availableEnemies[randInt]) + "\n\n";
        for (int i = 0; i < availableEnemies.size(); i++) {
            std::cout << availableEnemies[i] << std::endl;
        }
        ////////////
        return availableEnemies[randInt];
    }




Aucun commentaire:

Enregistrer un commentaire