Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/GamePanel.java
master
bob 2022-06-08 14:35:13 -04:00
commit ac873cf221
5 changed files with 101 additions and 22 deletions

View File

@ -35,9 +35,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
// 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 enemyFrameCounter = 0; public int enemyFrameCounter = 0;
public boolean isPaused;
public PauseMenu pauseMenu;
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
public static BufferedImage[] explosionArray = new BufferedImage[9];
//public static ArrayList<Tile>map = new ArrayList<Tile>(); //public static ArrayList<Tile>map = new ArrayList<Tile>();
@ -61,6 +64,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
this.gameFrame = gameFrame; this.gameFrame = gameFrame;
camera = new Camera(0); camera = new Camera(0);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
pauseMenu = new PauseMenu(GAME_HEIGHT/2, 100, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 60), "Paused");
try { try {
// load player sprites from disk here // load player sprites from disk here
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
@ -71,6 +75,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
playerSpriteArray[0][0][i] = flipImageHorizontally(sprite); playerSpriteArray[0][0][i] = flipImageHorizontally(sprite);
playerSpriteArray[0][1][i] = flipImageHorizontally(sprite); playerSpriteArray[0][1][i] = flipImageHorizontally(sprite);
} }
for (int i = 0; i < 9; i++) {
explosionArray[i] = getImage("img/misc/bomb/sonicExplosion0" + i + ".png");
}
// load slime sprites from disk here // load slime sprites from disk here
// these variables were not defined above because they are temporary variables // these variables were not defined above because they are temporary variables
BufferedImage[] temporarySlimeArray = {getImage("img/enemy/slime/slimeWalk1.png"), BufferedImage[] temporarySlimeArray = {getImage("img/enemy/slime/slimeWalk1.png"),
@ -128,6 +135,13 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//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
public void draw(Graphics g, int playerFrame, int enemyFrame){ public void draw(Graphics g, int playerFrame, int enemyFrame){
background.draw(g); background.draw(g);
if (isPaused) {
// set player frame to 0 to prevent frame from changing when player moves
playerFrame = 0;
Graphics2D g2d = (Graphics2D)(g);
// remove extraneous details from game when paused
g2d.setPaint(Color.white);
}
//Don't want to draw off screen items //Don't want to draw off screen items
int xMin = Math.max(0,((GamePanel.camera.x+GAME_WIDTH)/Tile.length)-(GAME_WIDTH/(2*Tile.length))-5); int xMin = Math.max(0,((GamePanel.camera.x+GAME_WIDTH)/Tile.length)-(GAME_WIDTH/(2*Tile.length))-5);
int xMax = Math.min(map.length, 7+xMin + GAME_WIDTH/Tile.length); int xMax = Math.min(map.length, 7+xMin + GAME_WIDTH/Tile.length);
@ -154,6 +168,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
} }
g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100); g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100);
g.drawString(b.x+" "+((b.x+GamePanel.GAME_WIDTH)/Tile.length-4),100,200); g.drawString(b.x+" "+((b.x+GamePanel.GAME_WIDTH)/Tile.length-4),100,200);
if (isPaused) {
g.setColor(new Color(255, 255, 255, 100));
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
pauseMenu.draw(g, Color.white, Color.black);
}
} }
//call the move methods in other classes to update positions //call the move methods in other classes to update positions
@ -212,7 +232,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){ public void run(){
b = new StickyBomb(600, 100, 20, 1,-5, bomb); b = new StickyBomb(600, 100, 20, 1,-5, bomb, explosionArray);
LevelManager.setLevel(1); LevelManager.setLevel(1);
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen. //the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
@ -229,23 +249,21 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//only move objects around and update screen if enough time has passed //only move objects around and update screen if enough time has passed
if(delta >= 1){ if(delta >= 1){
if (!isPaused) {
// only perform game functions if game is not paused
try { try {
move(); move();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
checkCollision(); checkCollision();
try {
updateParticle();
} catch (IOException e) {
throw new RuntimeException(e);
}
repaint();
if (playerFrameCounter > 5) { if (playerFrameCounter > 5) {
// increment sprite image to be used and keeps it below 12 // increment sprite image to be used and keeps it below 12
playerFrame = (playerFrame + 1) % 11; playerFrame = (playerFrame + 1) % 11;
playerFrameCounter -= 5; playerFrameCounter -= 5;
} }
}
repaint();
delta--; delta--;
} }
} }
@ -259,8 +277,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//if a key is pressed, we'll send it over to the Player class for processing //if a key is pressed, we'll send it over to the Player class for processing
public void keyPressed(KeyEvent e){ public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
isPaused = !isPaused;
} else {
player.keyPressed(e); player.keyPressed(e);
} }
}
//if a key is released, we'll send it over to the Player class for processing //if a key is released, we'll send it over to the Player class for processing
public void keyReleased(KeyEvent e){ public void keyReleased(KeyEvent e){

View File

@ -66,7 +66,15 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
//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(new MouseAdapter() { addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
//player.mousePressed(e); if (hoverCheck(e)) {
keyPressed(new KeyEvent(new Component() {
}, 0, 0, 0, KeyEvent.VK_ENTER));
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
hoverCheck(e);
} }
}); });
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
@ -125,6 +133,16 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
} }
} }
public boolean hoverCheck(MouseEvent e) {
for (TextBox t: textBoxArray) {
if (t.isHover(e.getX(), e.getY())) {
currentBox = textBoxArray.indexOf(t);
return true;
}
}
return false;
}
//if a key is pressed, we'll send it over to the Player class for processing //if a key is pressed, we'll send it over to the Player class for processing
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.getKeyCode() == KeyEvent.VK_ENTER) {

21
src/PauseMenu.java Normal file
View File

@ -0,0 +1,21 @@
import java.awt.*;
public class PauseMenu extends TextBox {
public PauseMenu(int y, int textYOffset, int xWidth, int yHeight, int totalWidth, Font font, String text) {
super(y, xWidth, yHeight, totalWidth, font, text, null);
this.y -= textYOffset;
}
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));
g.drawRect(newX, newY, xWidth, yHeight);
g.setColor(backgroundColor);
g.fillRect(newX, newY, xWidth, yHeight);
}
g.setColor(textColor);
drawCenteredString(g, y, newX, xWidth, text);
}
}

View File

@ -9,18 +9,22 @@ public class StickyBomb extends GenericSprite{
public int yVelocity; public int yVelocity;
public boolean isMove; public boolean isMove;
public int realX; public int realX;
BufferedImage sprite; public BufferedImage sprite;
public BufferedImage[] explosionSpriteArray;
public int fuse; public int fuse;
public int explosionPixel = 0;
public int explosionCounter = 0;
public boolean alive; public boolean alive;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite){ public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){
super(x,y,length,length); super(x,y,length,length);
this.length = length; this.length = length;
this.xVelocity = xVelocity; this.xVelocity = xVelocity;
this.yVelocity = yVelocity; this.yVelocity = yVelocity;
this.sprite = sprite; this.sprite = sprite;
this.explosionSpriteArray = explosionSpriteArray;
fuse = GlobalState.second*5; fuse = GlobalState.second*5;
isMove = true; isMove = true;
alive = true; alive = true;
@ -99,12 +103,21 @@ public class StickyBomb extends GenericSprite{
public void mousePressed(MouseEvent e){ public void mousePressed(MouseEvent e){
int xx = e.getX(); int xx = e.getX();
int yy = e.getY(); int yy = e.getY();
GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35, (xx-GamePanel.player.x)/20, (yy-GamePanel.player.y)/10, sprite); GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35,
(xx-GamePanel.player.x)/20, (yy-GamePanel.player.y)/10, sprite, explosionSpriteArray);
} }
public void draw(Graphics g){ public void draw(Graphics g){
if (explosionCounter >= 2) {
explosionPixel += 1;
explosionCounter -= 2;
}
if(alive) { if(alive) {
g.drawImage(sprite, x - GamePanel.camera.x, y, length, length, null); g.drawImage(sprite, x - GamePanel.camera.x, y, length, length, null);
} else if (explosionPixel < explosionSpriteArray.length - 1) {
g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel,
y-10*explosionPixel, length+10*explosionPixel, length+10*explosionPixel, null);
explosionCounter += 1;
} }
} }
} }

View File

@ -53,4 +53,9 @@ public class TextBox extends Rectangle {
g.setFont(font); g.setFont(font);
drawCenteredTextBox(g, text, backgroundColor, textColor); drawCenteredTextBox(g, text, backgroundColor, textColor);
} }
@Override
public boolean equals(Object o) {
return this == o;
}
} }