I am coding a small game and want to polish my c++ and therefore i use every fancy design pattern i can find ;)
So now I assume I use the wrong pattern because I have heavy performance issues,
I use the prototype pattern for my enemy class which clones then different enemies, which must be drawn and updated....so...i want to append each alpha some minors dependent on their distance to them.... so all are enemies and I thought I search for each minor the nearest alpha enemy by for looping my vector of enemies.. .first for an minor and if i have a minor I am looking for the nearest alpha by looping again through the for loop to check for alpha types
class Enemy
{
public:
virtual ~Enemy() {};
virtual Enemy* clone() = 0;
...
virtual void draw();
...
void update(std::vector<Enemy*>& enemies);
and ...
void Enemy::update(std::vector<Enemy*>& enemies) {
Enemy* closestAlpha = nullptr;
for (int i = 0; i < enemies.size(); i++) {
if (enemies[i]->getType() == eType::MINOR) {
//Find the closest alpha
closestAlpha = getNearestAlpha(enemies);
// If we found a alpha, move towards him and join him
if (closestAlpha != nullptr) {
// Get the direction vector twoards the alpha
}
}
}
}
and ...
Enemy* Enemy::getNearestAlpha(std::vector<Enemy*>& enemies) {
Enemy* closestAlpha = nullptr;
float smallestDistance = 9999999.0f;
for (int i = 0; i < enemies.size(); i++) {
}
}
So if I use this update function even I do nothing in the for loop, my fps drops to 5 fps and without this update function I got my desired 60 .
What I am doing wrong??
Aucun commentaire:
Enregistrer un commentaire