From faca2712203e3a5129c8dd9055e95e3384e512a6 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 19 Jun 2022 15:09:53 -0700 Subject: [PATCH] Fix pause menu --- src/GameFrame.java | 3 ++- src/GamePanel.java | 44 ++++++++++++++++++++++++++++++------------- src/PauseMenu.java | 8 ++++++-- src/SettingPanel.java | 2 +- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/GameFrame.java b/src/GameFrame.java index d191b70..4d16a54 100644 --- a/src/GameFrame.java +++ b/src/GameFrame.java @@ -32,7 +32,8 @@ public class GameFrame extends JFrame{ game = (GamePanel)FileManager.readObjectFromFile("local/game_state.dat", Arrays.asList("Any")); game.gameFrame = main; game.isContinue = true; - game.addUserInterface(); + game.requestFocusable(); + game.addMouseListener(); } catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) { System.out.println(e); game = new GamePanel(main); //run GamePanel constructor diff --git a/src/GamePanel.java b/src/GamePanel.java index 226221a..aed3301 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -45,8 +45,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ // keeps track of how many ticks has elapsed since last frame change public int playerFrameCounter = 0; public int timeSinceLastSave = 0; - public boolean isPaused, isDialogue, waitForDialogue, mouseAlreadyTranslated; - public PauseMenu pauseMenu; + public transient boolean isPaused; + public boolean isDialogue, waitForDialogue, mouseAlreadyTranslated; + public PauseMenu pauseMenu, pauseMenuExitOne, pauseMenuExitTwo, pauseMenuResume; public DialogueMenu dialogueMenu; public ArrayList dialogueArray = new ArrayList(); @@ -93,7 +94,10 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ cloudOneBackground = new BackgroundImage(200, 200, cloud1, cloud1.image.getWidth(), cloud1.image.getHeight(), 5, camera); cloudTwoBackground = new BackgroundImage(600, 250, cloud2, cloud2.image.getWidth(), cloud3.image.getHeight(), 5, camera); cloudThreeBackground = new BackgroundImage(1000, 200, cloud3, cloud2.image.getWidth(), cloud3.image.getHeight(), 5, camera); - pauseMenu = new PauseMenu(GAME_HEIGHT/2, 100, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 60), "Paused"); + pauseMenu = new PauseMenu(GAME_HEIGHT/2, 100, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 60), "Paused", true); + pauseMenuExitOne = new PauseMenu(GAME_HEIGHT/2, 0, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 24), "Press ENTER to return", true); + pauseMenuExitTwo = new PauseMenu(GAME_HEIGHT/2, -20, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 24), "to the main menu", true); + pauseMenuResume = new PauseMenu(GAME_HEIGHT/2, -50, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 18), "(or press ESC to resume)", true); dialogueMenu = new DialogueMenu(GAME_HEIGHT-100, 200, new Font(Font.MONOSPACED, Font.BOLD, 20), narratorPortrait, true); dialogueArray.add("Did you ever hear the tragedy of Darth Plagueis The Wise?"); dialogueArray.add("I thought not."); @@ -153,16 +157,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ this.setFocusable(true); //make everything in this class appear on the screen this.addKeyListener(this); //start listening for keyboard input // request focus when the CardLayout selects this game - this.addComponentListener(new ComponentAdapter() { - - @Override - public void componentShown(ComponentEvent cEvt) { - Component src = (Component) cEvt.getSource(); - src.requestFocusInWindow(); - } - - }); + requestFocusable(); //add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods + addMouseListener(); + } + + public void addMouseListener() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { try { @@ -209,6 +209,18 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ }); } + public void requestFocusable() { + this.addComponentListener(new ComponentAdapter() { + + @Override + public void componentShown(ComponentEvent cEvt) { + Component src = (Component) cEvt.getSource(); + src.requestFocusInWindow(); + } + + }); + } + // startThread is to be called after the game has started to avoid any issues TODO: better explanation public void startThread() { //make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time! @@ -315,6 +327,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ g.setColor(new Color(255, 255, 255, 100)); g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); pauseMenu.draw(g, Color.white, Color.black); + pauseMenuExitOne.draw(g, new Color(0,0, 0, 0), Color.gray); + pauseMenuExitTwo.draw(g, new Color(0,0, 0, 0), Color.gray); + pauseMenuResume.draw(g, new Color(0,0, 0, 0), Color.gray); } else if (isDialogue) { g.setColor(new Color(255, 255, 255, 100)); g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); @@ -505,8 +520,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ e = UtilityFunction.intercept(e, middlewareArray); if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { isPaused = !isPaused; + System.out.println(isPaused); } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { - if (!waitForDialogue) { + if (isPaused && (e.getSource() == null)) { + ((CardLayout)gameFrame.getLayout()).show(gameFrame, "menu"); + } else if (!waitForDialogue) { waitForDialogue = true; } else { dialogueMenu.currentFrame = 0; diff --git a/src/PauseMenu.java b/src/PauseMenu.java index 9f289cd..d428090 100644 --- a/src/PauseMenu.java +++ b/src/PauseMenu.java @@ -2,16 +2,20 @@ import java.awt.*; import java.io.Serializable; public class PauseMenu extends TextBox implements Serializable { - public PauseMenu(int y, int textYOffset, int xWidth, int yHeight, int totalWidth, Font font, String text) { + public boolean hasBorder; + public PauseMenu(int y, int textYOffset, int xWidth, int yHeight, int totalWidth, Font font, String text, boolean hasBorder) { super(y, xWidth, yHeight, totalWidth, font, text, null); this.y -= textYOffset; + this.hasBorder = hasBorder; } public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) { if (backgroundColor != null) { g.setColor(textColor); // TODO: make drawn line widths consistent - ((Graphics2D)g).setStroke(new BasicStroke(4f)); + if (hasBorder) { + ((Graphics2D) g).setStroke(new BasicStroke(4f)); + } g.drawRect(newX, newY, xWidth, yHeight); g.setColor(backgroundColor); g.fillRect(newX, newY, xWidth, yHeight); diff --git a/src/SettingPanel.java b/src/SettingPanel.java index 5a61087..ccab594 100644 --- a/src/SettingPanel.java +++ b/src/SettingPanel.java @@ -49,7 +49,7 @@ public class SettingPanel extends MenuPanel { textBoxArray.add(left); textBoxArray.add(right); - pauseMenu = new PauseMenu(GAME_HEIGHT/2, 0, 400, 400, GAME_WIDTH, smallFont, "Enter your key"); + pauseMenu = new PauseMenu(GAME_HEIGHT/2, 0, 400, 400, GAME_WIDTH, smallFont, "Enter your key", true); } //call the draw methods in each class to update positions as things move