Long story short, I’m making a game. I have a Level class that contains entities called Interactables that implement the game logic. Problem is, I’ve made many extentions to the Interactable class. Moveable, Controllable, Collider, Collidable, etc. When I add an interactable to the game, I write something like…
public void addInteractable(Interactable inter)
{
interactables.add(inter);
if(inter instanceof Moveable)
moveables.add((Moveable)inter);
if(inter instanceof Controllable)
controllables.add((Controllable)inter);
if(inter instanceof Collider)
colliders.add((Collider)inter);
if(inter instanceof Collidable)
collidables.add((Collidable)inter);
}
The reason I split the Interactable into multiple lists is to reduce the number of comparisons between Interactables. For instance, Colliders only need to be concerned with Collidables, so when applying collisions, I iterate through colliders and collidables enforcing collisions. The split is also necessary because certain routines belonging to certain Interactables need to be called in a particular order. Collidables need to move before Colliders enforce collisions.
Of course you could imagine that removing an Interactable from the level would be a similar process as to the one above, so I’m not going to write that out.
My real question is, is this a bad practice? Would someone else consider a different approach as to adding and removing entities from a level? I know instanceof checks are a sign of bad design, but I felt that they had their place in this one area of my game.