I am developing a simple 2D game in Java and I'm stuck.
All my game objects(Enemy
, Player
, Item
, Wall
, ...) are extending Entity
.
Entity
is an abstract class containing some abstract methods like update()
.
I have made an interface called Drawable
which contains a draw()
method.
Some game objects like Item
should be an Entity
but also be Drawable
while others, like Wall, should just be an Entity
(not connected to a tile, just x and y coordinates)
It all looks something like this:
List<Entity> entities;
In constructor i do this:
entities = tileMapReader.getEntities();
My question is: How should i draw my Drawable
s?
I want to be able to do something like this:
for (Entity entity : entities) {
entity.draw(g);
}
But since all Entities don't have the draw()
method I can't do that. And i don't think if (entity instanceof Drawable)
is such a good idea.