Hi there. I am currently coding a personal project for learning purposes. I have run into a few problems that I can only assume there is a better or more efficient way of doing so. So let me try to explain the code in question.
The project is a small shooter game. I have a class for enemies that has things like health position etc.. Lets say for this example I want to have 100 enemies so right now I am creating them like this:
1 2 3 4 5 6 7 8 9
//Declare the enemies
publicstaticint maxEnemies = 100;
Enemy[] enemies = new Enemy[maxEnemies];
for (int i = 0; i < maxEnemies; i++)
{
enemies[i] = new Enemy();
}
Whenever I need to update the enemy or check collision of the enemies i have to use a for loop and go through all the enemies. But what I would like is to have a way so that I dont have to use the for loop everytime I want to do something with these enemies. I would like to have a way so that i can add and delete them when i want. so that at one place there might be 20 but another place there might be 300. I have been looking into List but am not sure it would be much different that way. Is there someone who can maybe point me in the right direction?? It would be a huge help.
Regards,
Ed
Create a wave class, and have your waves of enemies contain an array/vector of pointers to enemies.
This may not be what you want I'm not sure, youd obviously still have to loop through every enemy in the wave when you updated each wave but you could update smaller groups of enemies instead of all of them every time.