Add documentation and fix continue bug

master
John 2022-06-20 13:57:51 -04:00
parent b82cfd79d5
commit 595a70249f
10 changed files with 45 additions and 22 deletions

View File

@ -1,7 +1,7 @@
//The purpose of this class is to create a background which acts as a parallax // Eric Li, Charlie Zhao, ICS4U, Finished 2022-06-15
import javax.swing.*; // the purpose of this class is to create a background
// this background can also be parallax and move as the player moves
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.Serializable; import java.io.Serializable;
public class BackgroundImage implements Serializable { public class BackgroundImage implements Serializable {
@ -23,8 +23,9 @@ public class BackgroundImage implements Serializable {
this.camera = camera; this.camera = camera;
} }
public void draw(Graphics g){ public void draw(Graphics g){
// draw image, image moves one pixel every parallaxRatio pixels the camera moves
g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % GamePanel.GAME_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 // draws second image so that the background is always present
g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % GamePanel.GAME_WIDTH + GamePanel.GAME_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);
} }
} }

View File

@ -1,7 +1,9 @@
// Eric Li, Charlie Zhao, ICS4U, Finished 2022-06-17
// this class shows the projected trajectory of a bomb based on the current mouse position
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -13,11 +15,14 @@ public class BombDirectionShow extends StickyBomb implements Serializable {
} }
public void draw(Graphics g) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public void draw(Graphics g) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
// updates realX (affected by camera.x)
update(); update();
isMove = true; isMove = true;
// renders a maximum of 10 rectangles
int loopCounter = 0; int loopCounter = 0;
while(isMove&&loopCounter<10) { while(isMove&&loopCounter<10) {
super.move(); super.move();
// draws rectangles if they don't intersect a tile
if(isMove&&canUpdate(0,0)) { if(isMove&&canUpdate(0,0)) {
g.drawRect(x - GameFrame.game.camera.x, y, 2, 2); g.drawRect(x - GameFrame.game.camera.x, y, 2, 2);
} }

View File

@ -1,3 +1,6 @@
// Eric Li, Charlie Zhao, ICS4U, Finished 2022-06-19
// Enables serialization of the otherwise unserializable BufferedImage object
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.ColorModel; import java.awt.image.ColorModel;
@ -11,28 +14,34 @@ public class BufferedImageWrapper implements Serializable {
public String imageString; public String imageString;
public Boolean flipImage = false; public Boolean flipImage = false;
// same constructor as BufferedImage
public BufferedImageWrapper(int width, int height, int imageType) { public BufferedImageWrapper(int width, int height, int imageType) {
image = new BufferedImage(width, height, imageType); image = new BufferedImage(width, height, imageType);
} }
// same constructor as BufferedImage
public BufferedImageWrapper(int width, int height, int imageType, IndexColorModel cm) { public BufferedImageWrapper(int width, int height, int imageType, IndexColorModel cm) {
image = new BufferedImage(width, height, imageType, cm); image = new BufferedImage(width, height, imageType, cm);
} }
// same constructor as BufferedImage
public BufferedImageWrapper(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties) { public BufferedImageWrapper(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties) {
image = new BufferedImage(cm, raster, isRasterPremultiplied, properties); image = new BufferedImage(cm, raster, isRasterPremultiplied, properties);
} }
// directly load an image into the object
public BufferedImageWrapper(BufferedImage image) { public BufferedImageWrapper(BufferedImage image) {
this.image = image; this.image = image;
} }
// save a file location to load the image from
public BufferedImageWrapper(String imageString) throws IOException { public BufferedImageWrapper(String imageString) throws IOException {
image = GamePanel.getImage(imageString); this(imageString, false);
this.imageString = imageString;
} }
// save a file location to load the image from, and flip the image if necessary
public BufferedImageWrapper(String imageString, boolean flip) throws IOException { public BufferedImageWrapper(String imageString, boolean flip) throws IOException {
// flip the image by calling the GamePanel static method if the flip boolean is passed as true
BufferedImage temporaryImage = GamePanel.getImage(imageString); BufferedImage temporaryImage = GamePanel.getImage(imageString);
if (flip) { if (flip) {
image = GamePanel.flipImageHorizontally(temporaryImage); image = GamePanel.flipImageHorizontally(temporaryImage);
@ -43,33 +52,41 @@ public class BufferedImageWrapper implements Serializable {
this.imageString = imageString; this.imageString = imageString;
} }
// empty constructor to allow using the custom serializing method readObject
public BufferedImageWrapper() {} public BufferedImageWrapper() {}
// custom writeObject method that allows writing an otherwise unserializable method to disk
@Serial @Serial
private void writeObject(ObjectOutputStream out) throws IOException { private void writeObject(ObjectOutputStream out) throws IOException {
// save whether the image is flipped
out.writeObject(flipImage); out.writeObject(flipImage);
// if the imageString is present, write that to disk to prevent excessive disk writes
if (imageString != null) { if (imageString != null) {
out.writeObject(imageString); out.writeObject(imageString);
} else { } else { // otherwise, write the image to disk
ImageIO.write(image, "png", out); // png is lossless ImageIO.write(image, "png", out); // png is lossless
} }
} }
// custom readObject method that allows reading an otherwise unserializable method from disk
@Serial @Serial
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
Object o; Object o;
// read whether the image is flipped or not
flipImage = (Boolean)in.readObject(); flipImage = (Boolean)in.readObject();
o = in.readObject(); o = in.readObject();
// if the loaded object is a string, load the image residing at the path received from Path.of(string)
if (o instanceof String) { if (o instanceof String) {
BufferedImage temporaryImage; BufferedImage temporaryImage;
this.imageString = (String)o; this.imageString = (String)o;
temporaryImage = GamePanel.getImage(imageString); temporaryImage = GamePanel.getImage(imageString);
// then flip the image if the flipImage boolean is true
if (flipImage) { if (flipImage) {
image = GamePanel.flipImageHorizontally(temporaryImage); image = GamePanel.flipImageHorizontally(temporaryImage);
} else { } else {
image = temporaryImage; image = temporaryImage;
} }
} else { } else { // otherwise, load the image directly from the save file
image = ImageIO.read(in); image = ImageIO.read(in);
} }
} }

View File

@ -1,8 +1,10 @@
import java.io.Serializable; // Eric Li, Charlie Zhao, ICS4U, Finished 2022-06-12
//This class controls the x-scrolling aspect for the player //This class controls the x-scrolling aspect for the player
//If you look at the players absolute position(Relative to the screen), the players y position changes, but the x position //If you look at the players absolute position(Relative to the screen), the players y position changes, but the x position
//never actually changes. //never actually changes.
import java.io.Serializable;
public class Camera implements Serializable { public class Camera implements Serializable {
public int x; public int x;
public Camera(int x){ public Camera(int x){

View File

@ -1,6 +1,10 @@
// Eric Li, Charlie Zhao, ICS4U, Finished 2022-06-19
// This class extends JPanel and adds a Camera to it, allowing it to keep the same camera object
import javax.swing.*; import javax.swing.*;
public class CameraPanel extends JPanel { public class CameraPanel extends JPanel {
// creates new camera
public Camera camera = new Camera(0); public Camera camera = new Camera(0);
public CameraPanel() {} public CameraPanel() {}

View File

@ -2,11 +2,9 @@
//This class allows us to input and output flies useful for //This class allows us to input and output flies useful for
// inputting levels and making save data. // inputting levels and making save data.
import java.io.*; import java.io.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
// TODO: close scanner etc after use
public final class FileManager { public final class FileManager {
// will create file if it doesn't exist // will create file if it doesn't exist

View File

@ -28,7 +28,6 @@ public class GameFrame extends JFrame{
main = new CameraPanel(); main = new CameraPanel();
main.setLayout(new CardLayout()); main.setLayout(new CardLayout());
try { try {
System.out.println("done2");
game = (GamePanel)FileManager.readObjectFromFile("local/game_state.dat", Arrays.asList("Any")); game = (GamePanel)FileManager.readObjectFromFile("local/game_state.dat", Arrays.asList("Any"));
game.gameFrame = main; game.gameFrame = main;
game.isContinue = true; game.isContinue = true;

View File

@ -39,6 +39,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public static Font loreFont = new Font(Font.MONOSPACED, Font.ITALIC + Font.BOLD, 36); public static Font loreFont = new Font(Font.MONOSPACED, Font.ITALIC + Font.BOLD, 36);
public static Color tutorialColor = Color.darkGray; public static Color tutorialColor = Color.darkGray;
public static Color loreColor = Color.lightGray; public static Color loreColor = Color.lightGray;
public int level = 1;
public PauseMenu loadingMenu; public PauseMenu loadingMenu;
public int bombCount; public int bombCount;

View File

@ -1,14 +1,11 @@
import javax.imageio.ImageIO;
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
public class LevelManager implements Serializable { public class LevelManager implements Serializable {
public static int level = 1;
public static int xSpawn = -400; public static int xSpawn = -400;
public static int ySpawn = 450; public static int ySpawn = 450;
@ -20,7 +17,7 @@ public class LevelManager implements Serializable {
GameFrame.game.fireballs.clear(); GameFrame.game.fireballs.clear();
GameFrame.game.player.yVelocity = 0; GameFrame.game.player.yVelocity = 0;
GameFrame.game.player.xVelocity = 0; GameFrame.game.player.xVelocity = 0;
LevelManager.level = level; GameFrame.game.level = level;
if(level == 1){ if(level == 1){
//-400/450 //-400/450
xSpawn = -400; xSpawn = -400;
@ -88,7 +85,6 @@ public class LevelManager implements Serializable {
setLevel(level, false); setLevel(level, false);
} }
public static void nextLevel(){ public static void nextLevel(){
setLevel(level+1); setLevel(GameFrame.game.level+1);
System.out.println("done222");
} }
} }

View File

@ -292,13 +292,13 @@ public class Player extends GenericSprite {
public void reset() throws UnsupportedAudioFileException, LineUnavailableException, IOException { public void reset() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
SoundWrapper.playSound("sound/OOF.wav"); SoundWrapper.playSound("sound/OOF.wav");
LevelManager.setLevel(LevelManager.level, true); LevelManager.setLevel(GameFrame.game.level, true);
GameFrame.game.camera.x = LevelManager.xSpawn; GameFrame.game.camera.x = LevelManager.xSpawn;
y = LevelManager.ySpawn; y = LevelManager.ySpawn;
holdingSteel = false; holdingSteel = false;
} }
public void resetNoSound() throws IOException { public void resetNoSound() throws IOException {
LevelManager.setLevel(LevelManager.level, true); LevelManager.setLevel(GameFrame.game.level, true);
GameFrame.game.camera.x = LevelManager.xSpawn; GameFrame.game.camera.x = LevelManager.xSpawn;
y = LevelManager.ySpawn; y = LevelManager.ySpawn;
holdingSteel = false; holdingSteel = false;