7
« on: February 08, 2011, 11:00:23 pm »
First, I suggest the Bullet being a different class. And that the Player/Centipede/Bullet be derived from the same base class.
Then, I believe the easiest way in SFML to do bounding box detection is just to use Rect.Intersects(Rect). Then you can always get the Rect from one of your player classes like this:
FloatRect Entity::myRect() {
return sprite.GetPosition().x,
sprite.GetPosition().y,
sprite.GetSize().x,
sprite.GetSize().y,
}
bool Entity::Collides(Entity *e) {
return myRect().Intersects(e->myRect());
}
If the Bullet, Centipede, and Player are all derived from Entity, it becomes really easy to track this. And you can always the hitboxes smaller/larger that way by just overloading the myRect() function.
For "deleting" the Centipede, I would flag it as dead, or set an "alive" bool to false. Then something in my Game class (or whoever has the instance) would come collect and destroy it. This way if you want to have multiple centipedes later on, you can just iterate through them in that function and see if any of them are dead yet.
void Game::Collision() {
if (centipede) {
if (p1->Collides(centipede))
centipede->Kill(); // Kill() would just set alive to false.
}
}
void Game::Recycle() {
if (centipede) {
if (!centipede->Alive()) { // Alive() would just be a getter of the alive status.
delete centipede;
centipede=NULL;
}
}
}
If you wanted something to happen first while the Centipede is dying, you could store a separate bool for alive and dead possibly. And then when alive is false and dead is false, do an animation, or fade it out, etc... until a certain point. Then when it reaches that certain point, set dead to true. Then the game can check if that dead bool is true, and come to delete it.
Then, you just stop calling the Centipede's functions when the centipede pointer is NULL, to prevent any dereferencing errors. For example:
if (centipede) centipede->Draw();
As the previous poster said, it's a pretty broad question, and there's several ways to go about it. If I had a little more information, I could probably help you out more.