I have a base class named CollidableObject and a couple of inherited classes named Player, Enemy, InertObject, etc.
I am trying to find an easy way to iterate through all the instances of them, so I initially created a list of the type CollidableObject and put all the instances of the inherited classes in there.
The thing is that because of the nature of polymorphism, when I do the following
foreach (CollidableObject CollidableObject in collidableObjects)
{
if (CollidableObject is Player)
{
CollidableObject.Draw(spriteBatch, testPlayerTexture);
}
// Then the same prodedure for each type of CollidableObject.
// Might change to switch or something.
}
The draw method that it calls is the generic Draw method from the CollidbaleObject base, not the overridden/new one from the Player/Enemy/inertObject class.
How do I get around this. Is there a way to iterate through a collection of objects from the same tree but maintaining their inherited type?