Hello All!
As part of my top-down shooter that I'm making I am using a vector to hold data on projectiles. Currently, when I run the game on my netbook in release mode or on my pc in debug mode my game begins to lag a lot when adding projectiles to the vector (aka shooting) and when checking for collisions between projectiles and game entities.
Below is the declaration of the vector in the entity.h:
std::vector<Projectile> projectiles;
Below I have the code for firing the projectiles inside a function within entity.cpp:
reloadTimer.restart();
projectiles.push_back(projectile);
// put a projectile in the back of the vector
projectiles[projectiles.size() - 1].resetProjectile(getXPosition(), getYPosition(), xtar, ytar);
// reset the projectiles position and target
And below is the code for the collision between player projectiles and enemy entities:
for(int i = 0; i < EnemyContainer.size(); i ++){
for(int k = 0; k < player.projectiles.size(); k ++){
if(player.projectiles[k].BBCollision(EnemyContainer[i].getSprite())){
// if the player bullet collides with an enemy
EnemyContainer[i].loseHealth(player.getProjDamage()); // make the enemy lose health
player.projectiles.erase(player.projectiles.begin() + k); // "kill" the projectile
}
}
}
I was wondering if any of you wonderful people could help me once again and give me an idea in how to reduce lag when doing those two things? I have already used vector::reserve() in the constructor of the entity class, so I'm not sure what's the problem. Thank you very much in advance!