open NPC dialogue if you collide with it

This commit is contained in:
2025-11-28 19:54:30 +01:00
parent 61a51f184c
commit dada9e4f2f
6 changed files with 85 additions and 16 deletions

View File

@@ -7,8 +7,9 @@ import java.awt.*;
public class UI {
GamePanel panel;
Graphics graphics2d;
Graphics2D graphics2d;
Font arial_40, arial_80B;
public String currentDialogue;
public UI(GamePanel panel) {
this.panel = panel;
@@ -16,7 +17,7 @@ public class UI {
arial_80B = new Font("Arial", Font.BOLD, 80);
}
public void draw(Graphics graphics2d) {
public void draw(Graphics2D graphics2d) {
this.graphics2d = graphics2d;
graphics2d.setFont(arial_40);
graphics2d.setColor(Color.white);
@@ -29,6 +30,9 @@ public class UI {
case GameState.PAUSE:
drawPauseScreen();
break;
case GameState.DIALOGUE:
drawDialogueScreen();
break;
}
}
@@ -39,6 +43,29 @@ public class UI {
graphics2d.drawString(text, getCenteredX(text), y);
}
public void drawDialogueScreen() {
// WINDOW
int x = panel.tileSize*2;
int y = panel.tileSize/2;
int width = panel.screenWidth - (panel.tileSize*4);
int height = panel.tileSize*4;
drawSubWindow(x, y, width, height);
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 23));
x += panel.tileSize;
y += panel.tileSize;
graphics2d.drawString(currentDialogue, x, y);
}
public void drawSubWindow(int x, int y, int width, int height) {
graphics2d.setColor(new Color(0,0,0,210));
graphics2d.fillRoundRect(x, y, width, height, 35, 35);
graphics2d.setColor(new Color(255,255,255));
graphics2d.setStroke(new BasicStroke(5));
graphics2d.drawRoundRect(x+5, y+5, width-10, height-10, 25, 25);
}
public int getCenteredX(String text) {
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
}

View File

@@ -3,6 +3,7 @@ package de.miaurizius.jgame2d.core.enums;
public enum GameState {
PLAY,
PAUSE;
PAUSE,
DIALOGUE,
}

View File

@@ -23,21 +23,39 @@ public class KeyHandler implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
// CONTROLS
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = true;
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = true;
switch(panel.gameState) {
case PLAY:
switch (code) {
// CONTROLS
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = true;
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = true;
// DEBUG OPTIONS
case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime;
// DEBUG OPTIONS
case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime;
// GAME STATES
case KeyEvent.VK_ESCAPE -> panel.gameState = (panel.gameState == GameState.PAUSE) ? GameState.PLAY : GameState.PAUSE;
// GAME STATES
case KeyEvent.VK_ESCAPE -> panel.gameState = GameState.PAUSE;
}
break;
case DIALOGUE:
switch (code) {
case KeyEvent.VK_SPACE -> panel.gameState = GameState.PLAY;
}
break;
}
}
/// TODO: setup keybinds that will always work (like pausing the game)
// public void alwaysOnKeys(int code) {
// // GAME STATES
// switch (code) {
// case KeyEvent.VK_ESCAPE -> panel.gameState = GameState.PAUSE;
// }
//
// }
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();