From 4a9eefa416b3bdfd7d52b1b3eb6d75bdd6df7c27 Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 28 Nov 2025 20:05:19 +0100 Subject: [PATCH] finished dialogue system --- src/de/miaurizius/jgame2d/core/GamePanel.java | 2 +- src/de/miaurizius/jgame2d/core/UI.java | 8 ++++++-- .../jgame2d/core/handlers/KeyHandler.java | 3 ++- src/de/miaurizius/jgame2d/entity/Entity.java | 15 ++++++++++++++- .../miaurizius/jgame2d/entity/OldManNPC.java | 18 ++---------------- src/de/miaurizius/jgame2d/entity/Player.java | 7 +++++++ 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/de/miaurizius/jgame2d/core/GamePanel.java b/src/de/miaurizius/jgame2d/core/GamePanel.java index 5ace3df..6db5306 100644 --- a/src/de/miaurizius/jgame2d/core/GamePanel.java +++ b/src/de/miaurizius/jgame2d/core/GamePanel.java @@ -37,7 +37,7 @@ public class GamePanel extends JPanel implements Runnable { // SYSTEM public TileManager tileM = new TileManager(this); - KeyHandler keyH = new KeyHandler(this); + public KeyHandler keyH = new KeyHandler(this); Sound se = new Sound(); Sound music = new Sound(); public CollisionHandler collisionH = new CollisionHandler(this); diff --git a/src/de/miaurizius/jgame2d/core/UI.java b/src/de/miaurizius/jgame2d/core/UI.java index b6c83c5..e3ec07a 100644 --- a/src/de/miaurizius/jgame2d/core/UI.java +++ b/src/de/miaurizius/jgame2d/core/UI.java @@ -51,10 +51,14 @@ public class UI { int height = panel.tileSize*4; drawSubWindow(x, y, width, height); - graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 23)); + graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 28F)); x += panel.tileSize; y += panel.tileSize; - graphics2d.drawString(currentDialogue, x, y); + + for(String line : currentDialogue.split("\n")) { + graphics2d.drawString(line, x, y); + y += 40; + } } public void drawSubWindow(int x, int y, int width, int height) { diff --git a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java index 9e18a64..5205aa6 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java +++ b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java @@ -8,7 +8,7 @@ import java.awt.event.KeyListener; public class KeyHandler implements KeyListener { - public boolean upPressed, downPressed, leftPressed, rightPressed; + public boolean upPressed, downPressed, leftPressed, rightPressed, spacePressed; public GamePanel panel; // DEBUG public boolean checkDrawTime = false; @@ -31,6 +31,7 @@ public class KeyHandler implements KeyListener { 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; + case KeyEvent.VK_SPACE -> spacePressed = true; // DEBUG OPTIONS case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime; diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index 9bb536f..9c87eab 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -33,7 +33,20 @@ public class Entity { } public void setAction() {} - public void speak() {} + + public void speak() { + if(dialogue[dialogueIndex] == null) dialogueIndex = 0; + panel.ui.currentDialogue = dialogue[dialogueIndex]; + dialogueIndex++; + + switch(panel.player.direction) { + case UP -> direction = Direction.DOWN; + case DOWN -> direction = Direction.UP; + case LEFT -> direction = Direction.RIGHT; + case RIGHT -> direction = Direction.LEFT; + } + } + public void update() { setAction(); collisionOn = false; diff --git a/src/de/miaurizius/jgame2d/entity/OldManNPC.java b/src/de/miaurizius/jgame2d/entity/OldManNPC.java index 9abc516..db7d757 100644 --- a/src/de/miaurizius/jgame2d/entity/OldManNPC.java +++ b/src/de/miaurizius/jgame2d/entity/OldManNPC.java @@ -29,8 +29,8 @@ public class OldManNPC extends Entity { public void setDialogue() { dialogue[0] = "Hello, lad."; - dialogue[1] = "So you've come to this island to find the treasure?"; - dialogue[2] = "I used to be a great wizard but now... I'm a bit too old for taking an adventure"; + dialogue[1] = "So you've come to this island to \nfind the treasure?"; + dialogue[2] = "I used to be a great wizard but now... \nI'm a bit too old for taking an \nadventure"; dialogue[3] = "Well, good luck on you."; } @@ -45,18 +45,4 @@ public class OldManNPC extends Entity { if(i > 75) direction = Direction.RIGHT; actionLock = 0; } - - public void speak() { - if(dialogue[dialogueIndex] == null) dialogueIndex = 0; - panel.ui.currentDialogue = dialogue[dialogueIndex]; - dialogueIndex++; - - switch(panel.player.direction) { - case UP -> direction = Direction.DOWN; - case DOWN -> direction = Direction.UP; - case LEFT -> direction = Direction.RIGHT; - case RIGHT -> direction = Direction.LEFT; - } - } - } diff --git a/src/de/miaurizius/jgame2d/entity/Player.java b/src/de/miaurizius/jgame2d/entity/Player.java index 261720a..a6a7d06 100644 --- a/src/de/miaurizius/jgame2d/entity/Player.java +++ b/src/de/miaurizius/jgame2d/entity/Player.java @@ -95,12 +95,19 @@ public class Player extends Entity { public void interactNPC(int index) { if(index == 999) return; + //if(!panel.keyH.spacePressed) return; //Only uncomment if text should only appear if player hits space panel.gameState = GameState.DIALOGUE; panel.npc[index].speak(); + panel.keyH.spacePressed = false; } public void draw(Graphics2D graphics2d) { graphics2d.drawImage(parseSprite(), screenX, screenY, null); } + public void speak() { + //This method only exists for character specific things later... + super.speak(); + } + }