Fix pause menu

master
John 2022-06-19 15:09:53 -07:00
parent 45e3911815
commit faca271220
4 changed files with 40 additions and 17 deletions

View File

@ -32,7 +32,8 @@ public class GameFrame extends JFrame{
game = (GamePanel)FileManager.readObjectFromFile("local/game_state.dat", Arrays.asList("Any")); game = (GamePanel)FileManager.readObjectFromFile("local/game_state.dat", Arrays.asList("Any"));
game.gameFrame = main; game.gameFrame = main;
game.isContinue = true; game.isContinue = true;
game.addUserInterface(); game.requestFocusable();
game.addMouseListener();
} catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) { } catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) {
System.out.println(e); System.out.println(e);
game = new GamePanel(main); //run GamePanel constructor game = new GamePanel(main); //run GamePanel constructor

View File

@ -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 // keeps track of how many ticks has elapsed since last frame change
public int playerFrameCounter = 0; public int playerFrameCounter = 0;
public int timeSinceLastSave = 0; public int timeSinceLastSave = 0;
public boolean isPaused, isDialogue, waitForDialogue, mouseAlreadyTranslated; public transient boolean isPaused;
public PauseMenu pauseMenu; public boolean isDialogue, waitForDialogue, mouseAlreadyTranslated;
public PauseMenu pauseMenu, pauseMenuExitOne, pauseMenuExitTwo, pauseMenuResume;
public DialogueMenu dialogueMenu; public DialogueMenu dialogueMenu;
public ArrayList<String> dialogueArray = new ArrayList<String>(); public ArrayList<String> dialogueArray = new ArrayList<String>();
@ -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); 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); 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); 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); 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("Did you ever hear the tragedy of Darth Plagueis The Wise?");
dialogueArray.add("I thought not."); 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.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input this.addKeyListener(this); //start listening for keyboard input
// request focus when the CardLayout selects this game // request focus when the CardLayout selects this game
this.addComponentListener(new ComponentAdapter() { requestFocusable();
@Override
public void componentShown(ComponentEvent cEvt) {
Component src = (Component) cEvt.getSource();
src.requestFocusInWindow();
}
});
//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 //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() { addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
try { 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 // startThread is to be called after the game has started to avoid any issues TODO: better explanation
public void startThread() { 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! //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.setColor(new Color(255, 255, 255, 100));
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
pauseMenu.draw(g, Color.white, Color.black); 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) { } else if (isDialogue) {
g.setColor(new Color(255, 255, 255, 100)); g.setColor(new Color(255, 255, 255, 100));
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); 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); e = UtilityFunction.intercept(e, middlewareArray);
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
isPaused = !isPaused; isPaused = !isPaused;
System.out.println(isPaused);
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) { } 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; waitForDialogue = true;
} else { } else {
dialogueMenu.currentFrame = 0; dialogueMenu.currentFrame = 0;

View File

@ -2,16 +2,20 @@ import java.awt.*;
import java.io.Serializable; import java.io.Serializable;
public class PauseMenu extends TextBox implements 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); super(y, xWidth, yHeight, totalWidth, font, text, null);
this.y -= textYOffset; this.y -= textYOffset;
this.hasBorder = hasBorder;
} }
public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) { public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) {
if (backgroundColor != null) { if (backgroundColor != null) {
g.setColor(textColor); g.setColor(textColor);
// TODO: make drawn line widths consistent // 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.drawRect(newX, newY, xWidth, yHeight);
g.setColor(backgroundColor); g.setColor(backgroundColor);
g.fillRect(newX, newY, xWidth, yHeight); g.fillRect(newX, newY, xWidth, yHeight);

View File

@ -49,7 +49,7 @@ public class SettingPanel extends MenuPanel {
textBoxArray.add(left); textBoxArray.add(left);
textBoxArray.add(right); 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 //call the draw methods in each class to update positions as things move