I'm trying to implement a sensible management system for all the objects in a basic platform game. There are various types of objects - projectiles, the player, enemies, powerups, moving platforms, that type of thing.
Initially I thought for the sake of simplicity I'd have all objects derive from a base class Entity, and then stick them all in a vector<Entity*>. Then when it's time for them to act or to be drawn to the screen, I can just make a single pass over the vector and call it->update() or it->display(). This saves a bunch of code duplication from having them all in separate vectors for each type and having to iterate over each vector separately to call display functions and so on.
Difficulties arise with this method when I need to do something like collision-test all Projectiles against all Enemies - I have to iterate over the vector, testing if each element is a Projectile, and if it is, iterate over the vector again testing to see if each element is an Enemy, and then testing for a collision if so. I have to do this a bunch of times for, say, enemies vs projectiles, player vs projectiles, player vs powerups, enemies vs player, and it turns into a huge tangled mess of code and nested iterations over the vector, and the vast majority of element comparisons don't need to be made as the entities are of the wrong type.
I'm looking for an elegant way to simplify this, so that Type A vs Type B checks are as simple as possible, and also so that mass updating and displaying of objects is equally simple.
Is there an accepted best practice for this kind of thing? Thanks in advance for any replies.