Im working on a game as a project for my game development course. It is using vectors to spawn enemies at the top of the screen and run down as well as creating "bullets" that are fired by both the player and the boss. After adding the boss and code for his bullets the previously working code is now throwing a vector subscript out of range error. This error is always thrown right after hitting an enemy and didn't happen before the boss was introduced.
bullets[i].shape.move(bullets[i].currVelocity);
//Bullet Removal
if (bullets[i].shape.getPosition().x < 0 || bullets[i].shape.getPosition().x > window.getSize().x
|| bullets[i].shape.getPosition().y < 0 || bullets[i].shape.getPosition().y > window.getSize().y) {
bullets.erase(bullets.begin() + i);
}
else {
//Collision Code
for (size_t k = 0; k < enemies.size(); k++) {
if (bullets[i].shape.getGlobalBounds().intersects(enemies[k].getGlobalBounds())) {
bullets.erase(bullets.begin() + i);
enemies.erase(enemies.begin() + k);
score = score + 1;
fiveScore = fiveScore + 1;
std::cout << "Score: " << score << std::endl;
hitSound.play();
if (fiveScore == 5) {
fiveScore = 0;
//scoreUpSound.play();
}
if (score >= 100 && bossSpawned == false) {
bossSpawned = true;
boss.setPosition(250.0f, 50.0f);
}
break;
}
}
if (bullets[i].shape.getGlobalBounds().intersects(boss.getGlobalBounds())) {
bullets.erase(bullets.begin() + i);
hitSound.play();
bossHP = bossHP - 1;
if (bossHP <= 0) {
bossSpawned = false;
boss.setPosition(3214.0f, 4322.0f);
text.setString("You have completed your quest." "\n" "The warlock is slain and the realm is" "\n" "safe, yet as life returns to the kingdom" "\n" "you are dissatisfied. Your brother and" "\n" "lover are both deceased and" "\n" "you feel only death will heal your pain.");
text.setPosition(25.0f, 150.0f);
window.clear();
window.draw(text);
window.display();
Sleep(9000);
return 0;
}
break;
}
}
}
This is the code for the player's bullet collisions.
b2.shape.setPosition(bossCenter);
b2.currVelocity = bossAimDirNorm * b2.maxSpeed;
bossbullets.push_back(BossBullet(b2));
}
for (size_t z = 0; z < bossbullets.size(); z++) {
bossbullets[z].shape.move(bossbullets[z].currVelocity);
//BossBullet Removal
if (bossbullets[z].shape.getPosition().x < 0 || bossbullets[z].shape.getPosition().x > window.getSize().x
|| bossbullets[z].shape.getPosition().y < 0 || bossbullets[z].shape.getPosition().y > window.getSize().y) {
bossbullets.erase(bossbullets.begin() + z);
}
else {
//Collision Code
if (bossbullets[z].shape.getGlobalBounds().intersects(player.getGlobalBounds())) {
bossbullets.erase(bossbullets.begin() + z);
playerDead = true;
text.setString("You have failed your quest.");
text.setPosition(150.0f, 150.0f);
window.clear();
window.draw(text);
window.display();
Sleep(5000);
return 0;
}
}
}
This is the code for the boss' bullet collisions. Any help or even ideas of what could be wrong would be helpful. Reverting to before I added the boss is the only solution I've found for now.