diff --git a/img/backgrounds/cloud1.png b/img/backgrounds/cloud1.png new file mode 100644 index 0000000..2fe57a5 Binary files /dev/null and b/img/backgrounds/cloud1.png differ diff --git a/img/backgrounds/cloud2.png b/img/backgrounds/cloud2.png new file mode 100644 index 0000000..13e29a8 Binary files /dev/null and b/img/backgrounds/cloud2.png differ diff --git a/img/backgrounds/cloud3.png b/img/backgrounds/cloud3.png new file mode 100644 index 0000000..2c5df41 Binary files /dev/null and b/img/backgrounds/cloud3.png differ diff --git a/src/BackgroundImage.java b/src/BackgroundImage.java index 9e64047..96e852c 100644 --- a/src/BackgroundImage.java +++ b/src/BackgroundImage.java @@ -23,8 +23,8 @@ public class BackgroundImage implements Serializable { this.camera = camera; } 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 - 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); } } diff --git a/src/DialogueMenu.java b/src/DialogueMenu.java new file mode 100644 index 0000000..257269f --- /dev/null +++ b/src/DialogueMenu.java @@ -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 lines = new ArrayList(); + 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); + } +} diff --git a/src/GamePanel.java b/src/GamePanel.java index 3d12d33..cb6eb55 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -31,13 +31,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ public transient Image image; public transient Graphics graphics; public Player player; - public BackgroundImage background; + public BackgroundImage background, cloudOneBackground, cloudTwoBackground, cloudThreeBackground; public int playerFrame, enemyFrame; // keeps track of how many ticks has elapsed since last frame change public int playerFrameCounter = 0; public int enemyFrameCounter = 0; - public boolean isPaused; + public boolean isPaused, isDialogue; public PauseMenu pauseMenu; + public DialogueMenu dialogueMenu; 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 box = new BufferedImageWrapper(getImage("img/tiles/boxes/box.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; @@ -67,7 +71,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ this.gameFrame = gameFrame; camera = new Camera(0); 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"); + dialogueMenu = new DialogueMenu(0, 200, new Font(Font.MONOSPACED, Font.BOLD, 60)); try { // load player sprites from disk here 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 public void draw(Graphics g, int playerFrame, int enemyFrame) throws IOException { background.draw(g); + cloudOneBackground.draw(g); + cloudTwoBackground.draw(g); + cloudThreeBackground.draw(g); if (isPaused) { // set player frame to 0 to prevent frame from changing when player moves playerFrame = 0; @@ -237,6 +248,10 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ g.setColor(new Color(255, 255, 255, 100)); g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); 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); } } @@ -334,11 +349,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ playerFrame = (playerFrame + 1) % 11; playerFrameCounter -= 5; // if the player has moved enough to justify a frame change, a new save will also be made -// try { -// FileManager.writeObjectToFile("local/game_state", this); -// } catch (IOException e) { -// e.printStackTrace(); -// } + try { + FileManager.writeObjectToFile("local/game_state", new ArrayList()); // this is placeholder, replace with this + } catch (IOException e) { + e.printStackTrace(); + } } } repaint();