diff --git a/src/de/miaurizius/jgame2d/core/UI.java b/src/de/miaurizius/jgame2d/core/UI.java index 54391a9..b6c83c5 100644 --- a/src/de/miaurizius/jgame2d/core/UI.java +++ b/src/de/miaurizius/jgame2d/core/UI.java @@ -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; } diff --git a/src/de/miaurizius/jgame2d/core/enums/GameState.java b/src/de/miaurizius/jgame2d/core/enums/GameState.java index d335669..c19bedc 100644 --- a/src/de/miaurizius/jgame2d/core/enums/GameState.java +++ b/src/de/miaurizius/jgame2d/core/enums/GameState.java @@ -3,6 +3,7 @@ package de.miaurizius.jgame2d.core.enums; public enum GameState { PLAY, - PAUSE; + PAUSE, + DIALOGUE, } diff --git a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java index 917ccde..9e18a64 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java +++ b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java @@ -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(); diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index 5066f75..9bb536f 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -25,12 +25,15 @@ public class Entity { public int solidAreaDefaultX, solidAreaDefaultY; public boolean collisionOn = false; public int actionLock = 0; + String[] dialogue = new String[20]; + int dialogueIndex = 0; public Entity(GamePanel panel) { this.panel = panel; } public void setAction() {} + public void speak() {} 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 6e35430..9abc516 100644 --- a/src/de/miaurizius/jgame2d/entity/OldManNPC.java +++ b/src/de/miaurizius/jgame2d/entity/OldManNPC.java @@ -13,6 +13,7 @@ public class OldManNPC extends Entity { direction = Direction.DOWN; speed = 1; getImage(); + setDialogue(); } public void getImage() { @@ -26,6 +27,13 @@ public class OldManNPC extends Entity { right2 = initEntitySprites("npc/oldman_right_2"); } + 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[3] = "Well, good luck on you."; + } + public void setAction() { actionLock++; if(actionLock != 120) return; //lock action for x frames @@ -36,7 +44,19 @@ public class OldManNPC extends Entity { if(i > 50 && i <= 75) direction = Direction.LEFT; 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 1b8e0f2..261720a 100644 --- a/src/de/miaurizius/jgame2d/entity/Player.java +++ b/src/de/miaurizius/jgame2d/entity/Player.java @@ -2,10 +2,10 @@ package de.miaurizius.jgame2d.entity; import de.miaurizius.jgame2d.core.*; import de.miaurizius.jgame2d.core.enums.Direction; +import de.miaurizius.jgame2d.core.enums.GameState; import de.miaurizius.jgame2d.core.handlers.KeyHandler; import java.awt.*; -import java.awt.image.BufferedImage; public class Player extends Entity { @@ -91,12 +91,12 @@ public class Player extends Entity { public void pickObject(int index) { if(index == 999) return; - } public void interactNPC(int index) { if(index == 999) return; - System.out.println("npc collision detected"); + panel.gameState = GameState.DIALOGUE; + panel.npc[index].speak(); } public void draw(Graphics2D graphics2d) {