In my game, all game objects extend an Entity class. All enemy objects have their own class which extend Enemy. Enemy extends Entity.
In other words,
Entity -> Enemy -> SharkEnemy.
Now, I want to have a function to let me test for collision against a specific class.
That is, if for example I have a bullet class, I want it to only test for collision against entities that extend Enemy.
I’ve googled around and this is what I have:
public <T> Entity collide(Entity a, Class<T> desiredClass)
{
for (Entity b : entities)
if (b.getClass() == desiredClass && collide(a, b))
return b;
return null;
}
That is kind of what I want, but I need to know if it extends desiredClass, not if the class equals desiredClass.
Instead of checking for class equality, use
Class.isAssignableFrom()