add MiniMap functionality and integrate map state handling

This commit is contained in:
2025-12-13 18:20:01 +01:00
parent 87aba81f32
commit 9263626ca2
8 changed files with 242 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ import de.miaurizius.jgame2d.core.handlers.*;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.Player;
import de.miaurizius.jgame2d.environment.EnvironmentManager;
import de.miaurizius.jgame2d.tile.MiniMap;
import de.miaurizius.jgame2d.tile.TileManager;
import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
@@ -15,7 +16,6 @@ import javax.sound.sampled.FloatControl;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
@@ -60,6 +60,7 @@ public class GamePanel extends JPanel implements Runnable {
public Config config = new Config(this);
public PathFinder pFinder = new PathFinder(this);
public EnvironmentManager eManager = new EnvironmentManager(this);
public MiniMap mapMan = new MiniMap(this);
Thread gameThread;
// ENTITY AND OBJECT
@@ -167,6 +168,11 @@ public class GamePanel extends JPanel implements Runnable {
return;
}
if(gameState == GameState.MAP) {
mapMan.drawFullMapScreen(fg2);
return;
}
// GAME
tileM.draw(fg2);
for(Entity entity : iTile[currentMap.getIndex()]) if(entity != null) entity.draw(fg2);
@@ -185,6 +191,9 @@ public class GamePanel extends JPanel implements Runnable {
// ENVIRONMENT
eManager.draw(fg2);
// MINI MAP
mapMan.drawMiniMap(fg2);
// UI
ui.draw(fg2);

View File

@@ -17,10 +17,10 @@ public class UI {
GamePanel panel;
Graphics2D graphics2d;
Font font;
BufferedImage heart_full, heart_half, heart_blank, coin;
ArrayList<String> messages = new ArrayList<>();
ArrayList<Integer> messageCounter = new ArrayList<>();
public Font font;
public String currentDialogue;
public Entity tradingNPC;
public int commandNum;

View File

@@ -14,5 +14,6 @@ public enum GameState {
TRANSITION,
TRADE,
SLEEP,
MAP,
}

View File

@@ -3,7 +3,7 @@ package de.miaurizius.jgame2d.core.enums;
public enum Map {
OVERWORLD("worldmap", 0),
HUT("hut", 1);
HUT("worldmap", 1);
private final String name;
private final int index;

View File

@@ -53,6 +53,9 @@ public class KeyHandler implements KeyListener {
case KeyEvent.VK_SPACE -> spacePressed = true;
//case KeyEvent.VK_F -> shotKeyPressed = true;
// MINI MAP TOGGLE
case KeyEvent.VK_X -> panel.mapMan.miniMapOn = !panel.mapMan.miniMapOn;
// DEBUG OPTIONS
case KeyEvent.VK_T -> debug = !debug;
case KeyEvent.VK_R -> {for(Map m : Map.values()) panel.tileM.loadMap(m);}
@@ -64,6 +67,7 @@ public class KeyHandler implements KeyListener {
panel.ui.commandNum = 0;
}
case KeyEvent.VK_C -> panel.gameState = GameState.CHARACTER;
case KeyEvent.VK_M -> panel.gameState = GameState.MAP;
}
}
private void handlePause(int code) {
@@ -182,6 +186,12 @@ public class KeyHandler implements KeyListener {
if(code == KeyEvent.VK_ESCAPE) panel.ui.tradeState = UI.TradeState.SELECT;
}
}
private void handleMap(int code) {
switch (code) {
// EXIT STATE
case KeyEvent.VK_M, KeyEvent.VK_ESCAPE -> panel.gameState = GameState.PLAY;
}
}
// UTILITY
private void playerInventory(int code) {
@@ -255,6 +265,7 @@ public class KeyHandler implements KeyListener {
case CHARACTER -> handleCharacter(code);
case GAMEOVER -> handleGameOver(code);
case TRADE -> handleTrade(code);
case MAP -> handleMap(code);
}
}
@Override

View File

@@ -0,0 +1,92 @@
package de.miaurizius.jgame2d.tile;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.Map;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class MiniMap extends TileManager {
GamePanel panel;
BufferedImage worldMap[];
public boolean miniMapOn;
public MiniMap(GamePanel panel) throws IOException {
super(panel);
this.panel = panel;
createWorldMap();
}
public void createWorldMap() {
worldMap = new BufferedImage[Map.values().length];
int worldMapWidth = panel.maxWorldCol * panel.tileSize;
int worldMapHeight = panel.maxWorldRow * panel.tileSize;
for (int i = 0; i < Map.values().length; i++) {
worldMap[i] = new BufferedImage(worldMapWidth, worldMapHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = worldMap[i].createGraphics();
int col = 0;
int row = 0;
while(col < panel.maxWorldCol && row < panel.maxWorldRow) {
int tileNum = mapTileNum[i][col][row];
int x = col * panel.tileSize;
int y = row * panel.tileSize;
g2.drawImage(panel.tileM.tile[tileNum].image, x, y, null);
col++;
if(col != panel.maxWorldCol) continue;
col = 0;
row++;
}
}
}
public void drawFullMapScreen(Graphics2D g2) {
// BACKGROUND
g2.setColor(Color.black);
g2.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
// DRAW MAP
int width = 500;
int height = 500;
int x = panel.screenWidth / 2 - width / 2;
int y = panel.screenHeight / 2 - height / 2;
g2.drawImage(worldMap[panel.currentMap.getIndex()], x, y, width, height, null);
// DRAW PLAYER
double scale = (double)(panel.tileSize * panel.maxWorldCol)/width;
int playerX = (int)(x + panel.player.worldX/scale);
int playerY = (int)(y + panel.player.worldY/scale);
int playerSize = (int)(panel.tileSize/scale);
g2.drawImage(panel.player.down1, playerX, playerY, playerSize, playerSize, null);
// HINT
g2.setFont(panel.ui.font.deriveFont(32F));
g2.setColor(Color.white);
g2.drawString("Press M to close", 750, 550);
}
public void drawMiniMap(Graphics2D g2) {
if(!miniMapOn) return;
// DRAW MAP
int width = 300;
int height = 300;
int x = panel.screenWidth - width - 50;
int y = 50;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
g2.drawImage(worldMap[panel.currentMap.getIndex()], x, y, width, height, null);
// DRAW PLAYER
double scale = (double)(panel.tileSize * panel.maxWorldCol)/width;
int playerX = (int)(x + panel.player.worldX/scale);
int playerY = (int)(y + panel.player.worldY/scale);
int playerSize = (int)(panel.tileSize/3);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
g2.drawImage(panel.player.down1, playerX-6, playerY-6, playerSize, playerSize, null);
}
}