I am working on creating a relatively simple tower defense type game as a project to really learn some game development and some c++ stuff i haven't ever really used to accomplish anything other than like an example or tutorial. So far I have gotten the map to load from a file and the enemies to follow the path set by the map. However, I can't figure out how to delete the enemies when they get to the end. Currently, I have them just now displaying anymore once they reach the end. However, once I add the towers and they are attacked I think it will be more important for enemy to actually be removed from the enemy array. I am not sure how I should go about doing this though. Below is my main game loop. Any suggestions or suggested reading would be greatly appreciated. I could really find anything applicable on Google.
#include "SDL.h"
#include "Timer.h"
#include "globals.h"
#include "MapSquare.h"
#include "Map.h"
#include "Enemy.h"
#include <string>
#include <vector>
#include "GameObject.h"
int main(int argc, char* args[])
{
init();
bool quit = false;
Timer fps;
//load level
Map level("map1.dat"); //load the level
std::vector<Enemy> enemies; //hold all the enemies on the map
Timer enemySpawn(1500); //new enemy spawns every 1.5 seconds
int numEnemies= 0;
//While the user hasn't quit
while( quit == false )
{
fps.start();
//------------------------------------------------INPUT------------------------------------------------------------------------------------------------------
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//------------------------------------------------LOGIC-------------------------------------------------------------------------------------------------------
for(unsigned int i = 0; i < enemies.size();i++)
{
enemies.update(&level); //update the state of all the enemies currently on the map
}
//check if time for new enemy
enemySpawn.update(); //update the spawn counter
if(enemySpawn.check() && numEnemies < level.getNumEnemies()) //if time for a new enemy and less then max
{
enemies.push_back(Enemy(level.getStart().getX(),level.getStart().getY(),2.5,1000,Enemy::Green)); //add enemy to the map
numEnemies++;
}
//------------------------------------------------DISPLAY------------------------------------------------------------------------------------------------------
level.show(); //show the map
for(unsigned int i = 0; i < enemies.size();i++)
{
enemies.show(); //show each enemy
}
SDL_Flip(screen);
//Cap Framerate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
return 0;
};