From 8b2ee741073685bf739813a0467cff38ebf3931f Mon Sep 17 00:00:00 2001 From: Rishav <140265067+IamRishavDas@users.noreply.github.com> Date: Sat, 13 Apr 2024 16:49:23 +0530 Subject: [PATCH] added the puase menu (mouse events has some problems ) --- GameStates/Playing.java | 15 +++++---- UI/PauseButton.java | 66 ++++++++++++++++++++++++++++++++++++ UI/PauseOverlay.java | 49 +++++++++++++++++++++++---- UI/SoundButton.java | 74 +++++++++++++++++++++++++++++++++++++++++ Utils/Constants.java | 8 +++-- Utils/LoadSave.java | 1 + 6 files changed, 198 insertions(+), 15 deletions(-) create mode 100644 UI/PauseButton.java create mode 100644 UI/SoundButton.java diff --git a/GameStates/Playing.java b/GameStates/Playing.java index 0dd4101..a92a0bd 100644 --- a/GameStates/Playing.java +++ b/GameStates/Playing.java @@ -14,7 +14,7 @@ public class Playing extends State implements StateMethods{ private Player player; private LevelManager levelManager; - private boolean paused; + private boolean paused = true; private PauseOverlay pauseOverlay; public Playing(Game game) { @@ -43,6 +43,7 @@ public void windowFocousLost() { public void update() { levelManager.update(); player.update(); + pauseOverlay.update(); } @@ -79,24 +80,24 @@ public void keyReleased(KeyEvent e) { @Override public void mouseMoved(MouseEvent e){ - + if(paused) pauseOverlay.mouseMoved(e); } @Override public void mousePressed(MouseEvent e) { - + if(paused) pauseOverlay.mousePressed(e); } - - + + @Override public void mouseClicked(MouseEvent e) { - + if(paused) pauseOverlay.mouseClicked(e); } @Override public void mouseReleased(MouseEvent e) { - + if(paused) pauseOverlay.mouseReleased(e); } } diff --git a/UI/PauseButton.java b/UI/PauseButton.java new file mode 100644 index 0000000..19b56cc --- /dev/null +++ b/UI/PauseButton.java @@ -0,0 +1,66 @@ +package UI; + +import java.awt.Rectangle; + +public class PauseButton { + protected int x; + protected int y; + protected int width; + protected int height; + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Rectangle getBounds() { + return bounds; + } + + public void setBounds(Rectangle bounds) { + this.bounds = bounds; + } + + protected Rectangle bounds; + + public PauseButton(int x, int y, int width, int height){ + this.x = x; + this.y = y; + this.width = width; + this.height = height; + createBounds(); + } + + private void createBounds() { + bounds = new Rectangle(x, y, width, height); + } + + +} diff --git a/UI/PauseOverlay.java b/UI/PauseOverlay.java index 4241b9f..0ab14d9 100644 --- a/UI/PauseOverlay.java +++ b/UI/PauseOverlay.java @@ -5,6 +5,7 @@ import java.awt.image.BufferedImage; import NewGame.Game; +import Utils.Constants; import Utils.LoadSave; @@ -16,8 +17,21 @@ public class PauseOverlay{ private int bgH; private int bgW; + private SoundButton musicButton; + private SoundButton sfxButton; + public PauseOverlay(){ loadBackground(); + createSoundButton(); + } + + private void createSoundButton() { + int soundX = (int) (450 * Game.SCALE); + int musicY = (int) (140 * Game.SCALE); + int sfxY = (int) (186 * Game.SCALE); + musicButton = new SoundButton(soundX, musicY, Constants.Ui.PauseButtons.SOUND_SIZE, Constants.Ui.PauseButtons.SOUND_SIZE); + sfxButton = new SoundButton(soundX, sfxY, Constants.Ui.PauseButtons.SOUND_SIZE, Constants.Ui.PauseButtons.SOUND_SIZE); + } private void loadBackground() { @@ -25,20 +39,24 @@ private void loadBackground() { bgW = (int) (backgroundImage.getWidth() * Game.SCALE); bgH = (int) (backgroundImage.getHeight() * Game.SCALE); bgX = Game.GAME_WIDTH / 2 - bgW / 2; - bgY = 100; + bgY = (int) (25 * Game.SCALE); } public void update(){ - + musicButton.update(); + sfxButton.update(); } public void render(Graphics g){ - g.drawImage(backgroundImage, bgX, bgY, bgW, bgH, null); + g.drawImage(backgroundImage, bgX, bgY, bgW, bgH, null); // background + musicButton.render(g); + sfxButton.render(g); } public void mousePressed(MouseEvent e) { - + if(isIn(e, musicButton)) musicButton.setMousePressed(true); + else if(isIn(e, sfxButton)) sfxButton.setMousePressed(true); } public void mouseClicked(MouseEvent e) { @@ -46,10 +64,29 @@ public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { - + if(isIn(e, musicButton)){ + if(musicButton.isMousePressed()){ + musicButton.setMuted(!musicButton.isMuted()); + } + } + else if(isIn(e, sfxButton)){ + if(sfxButton.isMousePressed()){ + sfxButton.setMuted(!sfxButton.isMuted()); + } + } + musicButton.resetBools(); + sfxButton.resetBools(); } public void mouseMoved(MouseEvent e) { - + musicButton.setMouseOver(false); + sfxButton.setMouseOver(false); + + if(isIn(e, musicButton)) musicButton.setMouseOver(true); + else if(isIn(e, sfxButton)) sfxButton.setMouseOver(true); + } + + private boolean isIn(MouseEvent e, PauseButton b){ + return b.getBounds().contains(e.getX(), e.getY()); } } diff --git a/UI/SoundButton.java b/UI/SoundButton.java new file mode 100644 index 0000000..ff77b25 --- /dev/null +++ b/UI/SoundButton.java @@ -0,0 +1,74 @@ +package UI; + +import java.awt.Graphics; +import java.awt.image.BufferedImage; + +import Utils.Constants; +import Utils.LoadSave; + +public class SoundButton extends PauseButton{ + + private BufferedImage[][] soundImages; + private int rowIndex, colIndex; + private boolean mouseOver; + private boolean mousePressed; + private boolean muted; + + public SoundButton(int x, int y, int width, int height) { + super(x, y, width, height); + loadSoundImages(); + } + + private void loadSoundImages() { + BufferedImage temp = LoadSave.getImage(LoadSave.SOUND_BUTTONS); + soundImages = new BufferedImage[2][3]; + + for(int i=0; i