sorted entity rendering

This commit is contained in:
2025-11-28 23:22:10 +01:00
parent b2b65dd290
commit 6a6bfd971f
3 changed files with 25 additions and 6 deletions

View File

@@ -4,11 +4,13 @@ import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.*;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.Player;
import de.miaurizius.jgame2d.entity.objects.SuperObject;
import de.miaurizius.jgame2d.tile.TileManager;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -47,6 +49,7 @@ public class GamePanel extends JPanel implements Runnable {
public Player player = new Player(this, keyH);
public Entity[] obj = new Entity[10];
public Entity[] npc = new Entity[10];
ArrayList<Entity> entityList = new ArrayList<>();
// GAME STATE
public GameState gameState;
@@ -127,9 +130,15 @@ public class GamePanel extends JPanel implements Runnable {
// GAME
tileM.draw(graphics2d);
for (Entity superObject : obj) if (superObject != null) superObject.draw(graphics2d);
for(Entity npc : npc) if(npc != null) npc.draw(graphics2d);
player.draw(graphics2d);
// ENTITY RENDER SYSTEM
entityList.add(player);
for(Entity entity : npc) if(entity != null) entityList.add(entity);
for(Entity entity : obj) if(entity != null) entityList.add(entity);
entityList.sort(Comparator.comparingInt(o -> o.worldY));
for(Entity entity : entityList) entity.draw(graphics2d);
entityList.clear();
ui.draw(graphics2d);
long drawEnd = System.nanoTime();