So I’m making a 2D sprite based isometric game in java without any libraries. It go well so far but I have encountered a problem with my rendering. My levels consist of a 3D array of a abstract class called Tile, but now I want to be able to render entities on the map too. So what would be a good way of doing that? Since the Entity class is in a ArrayList i can’t simply loop through it. I have been thinking of having an ArrayList that consists of WeakReferences to a class called LevelComponent that only contains x, y, z, and sprite. While the update method loops through the Tile, and Entity class respectively
Here is some made up code on how I thinking of doing it.
public class Map{
List<WeakReference<Entity>> render = new ArrayList<>();
public void update(float alpha){
for(int i=0; i<entities.size(); i++){
Entity e = entities.get(i);
if(e.isDead()){
entities.remove(i); i--;
continue;
}
e.update(alpha);
}
for(int x=0; x<SIZE_WIDTH; x++)
for(int y=0; y<SIZE_HEIGHT; y++)
for(int z=0; z<SIZE_TOP; z++)
if(t != null)
t.update(alpha);
}
public void render(Graphics2D g2, int offetX, int offsetY){
for(int i=0; i<levelComp.size(); i++){
LevelComponent l = levelComp.get(i).get();
if(l == null){
levelComp.remove(i); i--;
continue;
}
l.render(g2, offetX, offetY);
}
}
}
Is there a better way I don’t see?