Skip to content

Commit

Permalink
added the puase menu (mouse events has some problems
Browse files Browse the repository at this point in the history
)
  • Loading branch information
IamRishavDas committed Apr 13, 2024
1 parent c3dc771 commit 8b2ee74
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 15 deletions.
15 changes: 8 additions & 7 deletions GameStates/Playing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -43,6 +43,7 @@ public void windowFocousLost() {
public void update() {
levelManager.update();
player.update();
pauseOverlay.update();
}


Expand Down Expand Up @@ -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);
}
}
66 changes: 66 additions & 0 deletions UI/PauseButton.java
Original file line number Diff line number Diff line change
@@ -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);
}


}
49 changes: 43 additions & 6 deletions UI/PauseOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.image.BufferedImage;

import NewGame.Game;
import Utils.Constants;
import Utils.LoadSave;


Expand All @@ -16,40 +17,76 @@ 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() {
backgroundImage = LoadSave.getImage(LoadSave.PAUSE_BACKGROUND);
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) {

}

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());
}
}
74 changes: 74 additions & 0 deletions UI/SoundButton.java
Original file line number Diff line number Diff line change
@@ -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<soundImages.length; i++){
for(int j=0; j<soundImages[i].length; j++){
soundImages[i][j] = temp.getSubimage(j * Constants.Ui.PauseButtons.SOUND_SIZE_DEFAULT, i * Constants.Ui.PauseButtons.SOUND_SIZE_DEFAULT, Constants.Ui.PauseButtons.SOUND_SIZE_DEFAULT, Constants.Ui.PauseButtons.SOUND_SIZE_DEFAULT);
}
}
}

public boolean isMouseOver() {
return mouseOver;
}

public void setMouseOver(boolean mouseOver) {
this.mouseOver = mouseOver;
}

public boolean isMousePressed() {
return mousePressed;
}

public void setMousePressed(boolean mousePressed) {
this.mousePressed = mousePressed;
}

public boolean isMuted() {
return muted;
}

public void setMuted(boolean muted) {
this.muted = muted;
}

public void update(){
if(muted) rowIndex = 1;
else rowIndex = 0;

colIndex = 0;
if(mouseOver) colIndex = 1;
if(mousePressed) colIndex = 2;
}

public void resetBools(){
mouseOver = false;
mousePressed = false;
}

public void render(Graphics g){
g.drawImage(soundImages[rowIndex][colIndex], x, y, width, height, null);
}
}
8 changes: 6 additions & 2 deletions Utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public static class Ui{
public static class Buttons{
public static final int B_WIDTH_DEFAULT = 140;
public static final int B_HEIGHT_DEFAULT = 56;
public static final int B_WIDTH = (int)(B_WIDTH_DEFAULT * Game.SCALE);
public static final int B_HEIGHT = (int)(B_HEIGHT_DEFAULT * Game.SCALE);
public static final int B_WIDTH = (int)(B_WIDTH_DEFAULT * Game.SCALE);
public static final int B_HEIGHT = (int)(B_HEIGHT_DEFAULT * Game.SCALE);
}
public static class PauseButtons{
public static final int SOUND_SIZE_DEFAULT = 42;
public static final int SOUND_SIZE = (int) (SOUND_SIZE_DEFAULT * Game.SCALE);
}
}
}
1 change: 1 addition & 0 deletions Utils/LoadSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class LoadSave {
public static final String MENU_BUTTONS = "menu_buttons.png";
public static final String MENU_BACKGROUND = "menu_background.png";
public static final String PAUSE_BACKGROUND = "pause_menu.png";
public static final String SOUND_BUTTONS = "sound_button.png";

public static BufferedImage getImage(String filename) {
BufferedImage image = null;
Expand Down

0 comments on commit 8b2ee74

Please sign in to comment.