Added double parallax, dialogue menu

master
John 2022-06-15 11:24:45 -04:00
parent 4aa0bd2022
commit 4c02b86b55
6 changed files with 78 additions and 4 deletions

BIN
img/backgrounds/cloud1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
img/backgrounds/cloud2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
img/backgrounds/cloud3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -23,8 +23,8 @@ public class BackgroundImage implements Serializable {
this.camera = camera; this.camera = camera;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % width, y, width, height, null); g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % GamePanel.GAME_WIDTH, y, width, height, null);
// added to prevent the background image from disappearing // added to prevent the background image from disappearing
g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % width + width - 1, y, width, height, null); g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % GamePanel.GAME_WIDTH + GamePanel.GAME_WIDTH - 1, y, width, height, null);
} }
} }

59
src/DialogueMenu.java Normal file
View File

@ -0,0 +1,59 @@
import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class DialogueMenu extends TextBox implements Serializable {
public static final int PORTRAIT_WIDTH = 200;
public static final int PADDING = 20;
public DialogueMenu(int y, int yHeight, Font font) {
super(y, GamePanel.GAME_WIDTH, yHeight, 0, font, null, null);
newX = PORTRAIT_WIDTH + PADDING;
}
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);
}
public static void drawCenteredString(Graphics g, int y, int x, String text) {
// split text by spaces
String[] newText = text.split(" ");
ArrayList<String> lines = new ArrayList<String>();
lines.add("");
// get font size
FontMetrics metrics = g.getFontMetrics();
int currentLineWidth = 0, lastLineIndex = 0;
for (String s: newText) {
currentLineWidth += metrics.stringWidth(s);
if (currentLineWidth < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*2)) {
lines.set(lastLineIndex, lines.get(lastLineIndex) + s);
} else {
currentLineWidth = metrics.stringWidth(s);
lines.add(s);
}
}
y -= PADDING;
// center y (half is above y value, half is below y value)
for (String s: lines) {
y -= (metrics.getAscent() - metrics.getDescent()) / 2;
// draw string
g.drawString(text, x, y);
}
}
public void draw(Graphics g, String text, Color backgroundColor, Color textColor) {
g.setFont(font);
drawCenteredTextBox(g, text, backgroundColor, textColor);
}
}

View File

@ -31,13 +31,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public transient Image image; public transient Image image;
public transient Graphics graphics; public transient Graphics graphics;
public Player player; public Player player;
public BackgroundImage background; public BackgroundImage background, cloudOneBackground, cloudTwoBackground, cloudThreeBackground;
public int playerFrame, enemyFrame; public int playerFrame, enemyFrame;
// 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 boolean isPaused, isDialogue;
public PauseMenu pauseMenu; public PauseMenu pauseMenu;
public DialogueMenu dialogueMenu;
public BufferedImageWrapper[][][] playerSpriteArray = new BufferedImageWrapper[2][2][11]; public BufferedImageWrapper[][][] playerSpriteArray = new BufferedImageWrapper[2][2][11];
@ -60,6 +61,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(getImage("img/backgrounds/pointyMountains.png")); public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(getImage("img/backgrounds/pointyMountains.png"));
public BufferedImageWrapper box = new BufferedImageWrapper(getImage("img/tiles/boxes/box.png")); public BufferedImageWrapper box = new BufferedImageWrapper(getImage("img/tiles/boxes/box.png"));
public BufferedImageWrapper boxCoin = new BufferedImageWrapper(getImage("img/tiles/boxes/boxCoin.png")); public BufferedImageWrapper boxCoin = new BufferedImageWrapper(getImage("img/tiles/boxes/boxCoin.png"));
public BufferedImageWrapper cloud1 = new BufferedImageWrapper(getImage("img/backgrounds/cloud1.png"));
public BufferedImageWrapper cloud2 = new BufferedImageWrapper(getImage("img/backgrounds/cloud2.png"));
public BufferedImageWrapper cloud3 = new BufferedImageWrapper(getImage("img/backgrounds/cloud3.png"));
public BufferedImageWrapper bomb; public BufferedImageWrapper bomb;
@ -67,7 +71,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
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);
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");
dialogueMenu = new DialogueMenu(0, 200, new Font(Font.MONOSPACED, Font.BOLD, 60));
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++) {
@ -170,6 +178,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
//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) throws IOException { public void draw(Graphics g, int playerFrame, int enemyFrame) throws IOException {
background.draw(g); background.draw(g);
cloudOneBackground.draw(g);
cloudTwoBackground.draw(g);
cloudThreeBackground.draw(g);
if (isPaused) { if (isPaused) {
// set player frame to 0 to prevent frame from changing when player moves // set player frame to 0 to prevent frame from changing when player moves
playerFrame = 0; playerFrame = 0;
@ -237,6 +248,10 @@ 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);
} else if (isDialogue) {
g.setColor(new Color(255, 255, 255, 100));
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
dialogueMenu.draw(g, "Lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum", Color.white, Color.black);
} }
} }