I have a manager object that keeps a pointer to every Sprite, and a pointer to the Sprite that has the focus (the player). It checks in a loop if the player collides to any other sprite, and if it does the I would like to collide them.
The player, and the other objects inherit from class Sprite. The problem is that I have both polymorphism and function overloading (maybe I shouldn't?), but I can't make it work.
I tried to make Collision a friend function, make it a virtual member function, but no matter what every time when Player collides let's say a GoodObject, Sprite::Collision(Sprite&) is called and I'd like Player::Collision(GoodObject&) to be called.
I understand that the problem is that polymorphism and function overloading in the child class don't fit together, because in the derived class the Collide function(s) don't have the same footprint as in the base Sprite class.
What techniques could I use to make the collisions? Should I use dynamic_cast? Is there nicer way to do it?
The code is something like this:
class Sprite // basic sprite
{
...
public:
bool IsCollision(const Sprite &other) const;
virtual Collide(Sprite &other); // for the polymorhism
};
class GoodObject : public Sprite
{ // colliding this would give the player extra points
...
};
class BadObject : public Sprite
{ // colliding this would kill the player
...
};
class Player : public Sprite
{
public:
virtual Collide(GoodObject &other); // function overloading
virtual Collide(BadObject &other);
};
class Manager
{
public:
void Loop(void);
private:
std::list<Sprite*> sprites;
Sprite *focus;
};
void Manager::Loop(void)
{
while(loop)
{
for (std::list<Sprite*>::iterator i = sprites.begin(); i != sprites.end(); ++i)
{
Sprite &sprite = **i;
sprite.Step(); // move the sprite, change animation frames
if (&sprite != focus && focus->IsCollision(sprite))
focus->Collide(sprite); // this is the PROBLEM
}
}
}

New Topic/Question
Reply




MultiQuote






|