diff --git a/src/BackgroundImage.java b/src/BackgroundImage.java index 96e852c..aecc20c 100644 --- a/src/BackgroundImage.java +++ b/src/BackgroundImage.java @@ -1,7 +1,7 @@ -//The purpose of this class is to create a background which acts as a parallax -import javax.swing.*; +// Eric Li, Charlie Zhao, ICS4U, Finished 2022-06-15 +// 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.image.BufferedImage; import java.io.Serializable; public class BackgroundImage implements Serializable { @@ -23,8 +23,9 @@ public class BackgroundImage implements Serializable { this.camera = camera; } 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); - // 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); } } diff --git a/src/BombDirectionShow.java b/src/BombDirectionShow.java index e99856a..fe3ba00 100644 --- a/src/BombDirectionShow.java +++ b/src/BombDirectionShow.java @@ -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.UnsupportedAudioFileException; import java.awt.*; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.Serializable; @@ -13,11 +15,14 @@ public class BombDirectionShow extends StickyBomb implements Serializable { } public void draw(Graphics g) throws UnsupportedAudioFileException, LineUnavailableException, IOException { + // updates realX (affected by camera.x) update(); isMove = true; + // renders a maximum of 10 rectangles int loopCounter = 0; while(isMove&&loopCounter<10) { super.move(); + // draws rectangles if they don't intersect a tile if(isMove&&canUpdate(0,0)) { g.drawRect(x - GameFrame.game.camera.x, y, 2, 2); } diff --git a/src/BufferedImageWrapper.java b/src/BufferedImageWrapper.java index 9bac3f1..dfc96aa 100644 --- a/src/BufferedImageWrapper.java +++ b/src/BufferedImageWrapper.java @@ -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 java.awt.image.BufferedImage; import java.awt.image.ColorModel; @@ -11,28 +14,34 @@ public class BufferedImageWrapper implements Serializable { public String imageString; public Boolean flipImage = false; + // same constructor as BufferedImage public BufferedImageWrapper(int width, int height, int imageType) { image = new BufferedImage(width, height, imageType); } + // same constructor as BufferedImage public BufferedImageWrapper(int width, int height, int imageType, IndexColorModel cm) { image = new BufferedImage(width, height, imageType, cm); } + // same constructor as BufferedImage public BufferedImageWrapper(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable properties) { image = new BufferedImage(cm, raster, isRasterPremultiplied, properties); } + // directly load an image into the object public BufferedImageWrapper(BufferedImage image) { this.image = image; } + // save a file location to load the image from public BufferedImageWrapper(String imageString) throws IOException { - image = GamePanel.getImage(imageString); - this.imageString = imageString; + this(imageString, false); } + // save a file location to load the image from, and flip the image if necessary 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); if (flip) { image = GamePanel.flipImageHorizontally(temporaryImage); @@ -43,33 +52,41 @@ public class BufferedImageWrapper implements Serializable { this.imageString = imageString; } + // empty constructor to allow using the custom serializing method readObject public BufferedImageWrapper() {} + // custom writeObject method that allows writing an otherwise unserializable method to disk @Serial private void writeObject(ObjectOutputStream out) throws IOException { + // save whether the image is flipped out.writeObject(flipImage); + // if the imageString is present, write that to disk to prevent excessive disk writes if (imageString != null) { out.writeObject(imageString); - } else { + } else { // otherwise, write the image to disk ImageIO.write(image, "png", out); // png is lossless } } + // custom readObject method that allows reading an otherwise unserializable method from disk @Serial private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { Object o; + // read whether the image is flipped or not flipImage = (Boolean)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) { BufferedImage temporaryImage; this.imageString = (String)o; temporaryImage = GamePanel.getImage(imageString); + // then flip the image if the flipImage boolean is true if (flipImage) { image = GamePanel.flipImageHorizontally(temporaryImage); } else { image = temporaryImage; } - } else { + } else { // otherwise, load the image directly from the save file image = ImageIO.read(in); } } diff --git a/src/Camera.java b/src/Camera.java index ad5cd5d..1c4b15b 100644 --- a/src/Camera.java +++ b/src/Camera.java @@ -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 //If you look at the players absolute position(Relative to the screen), the players y position changes, but the x position //never actually changes. + +import java.io.Serializable; + public class Camera implements Serializable { public int x; public Camera(int x){ diff --git a/src/CameraPanel.java b/src/CameraPanel.java index 4fad7ad..db118df 100644 --- a/src/CameraPanel.java +++ b/src/CameraPanel.java @@ -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.*; public class CameraPanel extends JPanel { + // creates new camera public Camera camera = new Camera(0); public CameraPanel() {} diff --git a/src/FileManager.java b/src/FileManager.java index 9d1d0c0..aa4a567 100644 --- a/src/FileManager.java +++ b/src/FileManager.java @@ -2,11 +2,9 @@ //This class allows us to input and output flies useful for // inputting levels and making save data. import java.io.*; -import java.util.ArrayList; import java.util.List; import java.util.Scanner; -// TODO: close scanner etc after use public final class FileManager { // will create file if it doesn't exist diff --git a/src/GameFrame.java b/src/GameFrame.java index 923e860..f421a33 100644 --- a/src/GameFrame.java +++ b/src/GameFrame.java @@ -28,7 +28,6 @@ public class GameFrame extends JFrame{ main = new CameraPanel(); main.setLayout(new CardLayout()); try { - System.out.println("done2"); game = (GamePanel)FileManager.readObjectFromFile("local/game_state.dat", Arrays.asList("Any")); game.gameFrame = main; game.isContinue = true; diff --git a/src/GamePanel.java b/src/GamePanel.java index e9bbdde..a90d9a9 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -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 Color tutorialColor = Color.darkGray; public static Color loreColor = Color.lightGray; + public int level = 1; public PauseMenu loadingMenu; public int bombCount; diff --git a/src/LevelManager.java b/src/LevelManager.java index 7beb5a1..a653f57 100644 --- a/src/LevelManager.java +++ b/src/LevelManager.java @@ -1,14 +1,11 @@ -import javax.imageio.ImageIO; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; -import java.awt.*; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; public class LevelManager implements Serializable { - public static int level = 1; public static int xSpawn = -400; public static int ySpawn = 450; @@ -20,7 +17,7 @@ public class LevelManager implements Serializable { GameFrame.game.fireballs.clear(); GameFrame.game.player.yVelocity = 0; GameFrame.game.player.xVelocity = 0; - LevelManager.level = level; + GameFrame.game.level = level; if(level == 1){ //-400/450 xSpawn = -400; @@ -88,7 +85,6 @@ public class LevelManager implements Serializable { setLevel(level, false); } public static void nextLevel(){ - setLevel(level+1); - // System.out.println("done222"); + setLevel(GameFrame.game.level+1); } } diff --git a/src/Player.java b/src/Player.java index 2b04203..24194a6 100644 --- a/src/Player.java +++ b/src/Player.java @@ -292,13 +292,13 @@ public class Player extends GenericSprite { public void reset() throws UnsupportedAudioFileException, LineUnavailableException, IOException { SoundWrapper.playSound("sound/OOF.wav"); - LevelManager.setLevel(LevelManager.level, true); + LevelManager.setLevel(GameFrame.game.level, true); GameFrame.game.camera.x = LevelManager.xSpawn; y = LevelManager.ySpawn; holdingSteel = false; } public void resetNoSound() throws IOException { - LevelManager.setLevel(LevelManager.level, true); + LevelManager.setLevel(GameFrame.game.level, true); GameFrame.game.camera.x = LevelManager.xSpawn; y = LevelManager.ySpawn; holdingSteel = false;