Files
JGame2D/src/de/miaurizius/jgame2d/core/GamePanel.java
2025-11-28 23:22:10 +01:00

175 lines
4.8 KiB
Java

package de.miaurizius.jgame2d.core;
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.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;
public class GamePanel extends JPanel implements Runnable {
// SCREEN SETTINGS
final int originalTileSize = 16; //16x16 tile
final int scale = 3;
public final int tileSize = originalTileSize * scale; //48x48 tile
// 4:3 ratio
public final int maxScreenCol = 16;
public final int maxScreenRow = 12;
public final int screenWidth = tileSize * maxScreenCol; // 768 pixels
public final int screenHeight = tileSize * maxScreenRow; // 576 pixels
// WORLD SETTINGS
public final int maxWorldCol = 50;
public final int maxWorldRow = 50;
//FPS
int FPS = 60;
// SYSTEM
public TileManager tileM = new TileManager(this);
public KeyHandler keyH = new KeyHandler(this);
Sound se = new Sound();
Sound music = new Sound();
public CollisionHandler collisionH = new CollisionHandler(this);
public AssetSetter assetSetter = new AssetSetter(this);
public UI ui = new UI(this);
public EventHandler eventH = new EventHandler(this);
Thread gameThread;
// ENTITY AND OBJECT
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;
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.black);
this.setDoubleBuffered(true);
this.addKeyListener(keyH);
this.setFocusable(true);
}
public void setupGame() {
assetSetter.setObject();
assetSetter.setNPC();
//playMusic(0); //Play main theme
gameState = GameState.TITLE;
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
double drawInterval = (double) 1000000000 / FPS;
double delta = 0;
double lastTime = System.nanoTime();
long currentTime;
long timer = 0;
int drawCount = 0;
while(gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
timer += (long) (currentTime - lastTime);
lastTime = currentTime;
if(delta >= 1) {
update();
repaint();
delta--;
drawCount++;
}
if(timer >= 1000000000) {
System.out.println("FPS: " + drawCount);
drawCount = 0;
timer = 0;
}
}
}
public void update() {
switch(gameState) {
case PLAY:
player.update();
for(Entity entity : npc) if(entity != null) entity.update();
break;
case PAUSE:
break;
}
}
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
// DEBUG
long drawStart;
drawStart = System.nanoTime();
// TITLE SCREEN
if(gameState == GameState.TITLE) {
ui.draw(graphics2d);
return;
}
// GAME
tileM.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();
long passed = drawEnd - drawStart;
// DEBUG
if(keyH.checkDrawTime) {
graphics2d.setColor(Color.white);
graphics2d.drawString("Draw Time: " + passed, 10, 400);
System.out.println("Draw Time: " + passed);
Logger.getLogger("DEBUG").log(Level.FINE, "Draw Time: " + passed);
}
// END
graphics.dispose();
}
public void playMusic(int i) {
music.setFile(i);
music.play();
music.loop();
}
public void stopMusic() {
music.stop();
}
public void playSE(int i) {
se.setFile(i);
se.play();
}
}