Made changes to allow for serialization, removed unnecessary statics from GameFrame and moved them to GamePanel (GamePanel.game.{var} instead of GameFrame.{var})

master
John 2022-06-14 21:34:35 -04:00
parent 3caa0b8c37
commit 406dcfb9df
22 changed files with 252 additions and 241 deletions

View File

@ -2,15 +2,16 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.Serializable;
public class BackgroundImage { public class BackgroundImage implements Serializable {
public int x, y; public int x, y;
public int width, height; public int width, height;
public int parallaxRatio; public int parallaxRatio;
public BufferedImage backgroundImage; public BufferedImageWrapper backgroundImage;
public Camera camera; public Camera camera;
public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio, Camera camera) { public BackgroundImage(int x, int y, BufferedImageWrapper backgroundImage, int width, int height, int parallaxRatio, Camera camera) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
@ -22,8 +23,8 @@ public class BackgroundImage {
this.camera = camera; this.camera = camera;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawImage(backgroundImage, x-camera.x/parallaxRatio % width, y, width, height, null); g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % width, y, width, height, null);
// added to prevent the background image from disappearing // added to prevent the background image from disappearing
g.drawImage(backgroundImage, x-camera.x/parallaxRatio % width + width - 1, y, width, height, null); g.drawImage(backgroundImage.image, x-camera.x/parallaxRatio % width + width - 1, y, width, height, null);
} }
} }

View File

@ -1,7 +1,8 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.Serializable;
public class BombDirectionShow extends StickyBomb{ public class BombDirectionShow extends StickyBomb implements Serializable {
public BombDirectionShow(int x, int y, int xVelocity, int yVelocity) { public BombDirectionShow(int x, int y, int xVelocity, int yVelocity) {
super(x, y, xVelocity, yVelocity, null, null); super(x, y, xVelocity, yVelocity, null, null);
WIDTH = 25; WIDTH = 25;
@ -15,7 +16,7 @@ public class BombDirectionShow extends StickyBomb{
while(isMove&&loopCounter<10) { while(isMove&&loopCounter<10) {
super.move(); super.move();
if(isMove&&canUpdate(0,0)) { if(isMove&&canUpdate(0,0)) {
g.drawRect(x - GamePanel.camera.x + Player.PLAYER_WIDTH / 2, y + Player.PLAYER_HEIGHT / 2, 2, 2); g.drawRect(x - GameFrame.game.camera.x + Player.PLAYER_WIDTH / 2, y + Player.PLAYER_HEIGHT / 2, 2, 2);
} }
loopCounter++; loopCounter++;
} }

View File

@ -1,40 +0,0 @@
import javax.imageio.ImageIO;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.*;
import java.util.Hashtable;
public class BufferedImage extends java.awt.image.BufferedImage implements Serializable {
public BufferedImage(int width, int height, int imageType) {
super(width, height, imageType);
}
public BufferedImage(int width, int height, int imageType, IndexColorModel cm) {
super(width, height, imageType, cm);
}
public BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties) {
super(cm, raster, isRasterPremultiplied, properties);
}
@Serial
private void writeObject(ObjectOutputStream out) throws IOException {
ImageIO.write(this, "png", out); // png is lossless
}
@Serial
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
java.awt.image.BufferedImage temporaryImage = ImageIO.read(in);
ColorModel cm = temporaryImage.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = temporaryImage.copyData(null);
// TODO: make work
setState(this, new BufferedImage(cm, raster, isAlphaPremultiplied, null));
}
private void setState(BufferedImage currentImage, BufferedImage newImage) {
currentImage = newImage;
}
}

View File

@ -0,0 +1,38 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.*;
import java.util.Hashtable;
public class BufferedImageWrapper implements Serializable {
public BufferedImage image;
public BufferedImageWrapper(int width, int height, int imageType) {
image = new BufferedImage(width, height, imageType);
}
public BufferedImageWrapper(int width, int height, int imageType, IndexColorModel cm) {
image = new BufferedImage(width, height, imageType, cm);
}
public BufferedImageWrapper(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties) {
image = new BufferedImage(cm, raster, isRasterPremultiplied, properties);
}
public BufferedImageWrapper(BufferedImage image) {
this.image = image;
}
public BufferedImageWrapper() {}
@Serial
private void writeObject(ObjectOutputStream out) throws IOException {
ImageIO.write(image, "png", out); // png is lossless
}
@Serial
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
image = ImageIO.read(in);
}
}

View File

@ -27,8 +27,13 @@ public class FileManager {
} }
public static Object readObjectFromFile(String fileLocation, List<String> allowedObject) throws IOException, ClassNotFoundException { public static Object readObjectFromFile(String fileLocation, List<String> allowedObject) throws IOException, ClassNotFoundException {
ObjectInputStream objectStream;
FileInputStream fileStream = new FileInputStream(fileLocation); FileInputStream fileStream = new FileInputStream(fileLocation);
SafeObjectInputStream objectStream = new SafeObjectInputStream(fileStream, allowedObject); if (!allowedObject.contains("Any")) {
objectStream = new SafeObjectInputStream(fileStream, allowedObject);
} else {
objectStream = new ObjectInputStream(fileStream);
}
return objectStream.readObject(); return objectStream.readObject();
} }

View File

@ -16,28 +16,34 @@ import javax.swing.*;
public class GameFrame extends JFrame{ public class GameFrame extends JFrame{
MenuPanel menu; MenuPanel menu;
GamePanel game; public static GamePanel game;
SettingPanel settings; SettingPanel settings;
CameraPanel main; CameraPanel main;
public GameFrame(){ public GameFrame(){
try {
// read previously saved controls
// SafeObjectInputStream was implemented to prevent arbitrary code execution from occurring if the save files were modified
GamePanel.middlewareArray = (ArrayList<Middleware>)FileManager.
readObjectFromFile("local/controls", Arrays.asList("java.util.ArrayList", "Middleware"));
} catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) {
GamePanel.middlewareArray = new ArrayList<Middleware>();
}
try { try {
main = new CameraPanel(); main = new CameraPanel();
main.setLayout(new CardLayout()); main.setLayout(new CardLayout());
menu = new MenuPanel(main); menu = new MenuPanel(main);
try { try {
game = (GamePanel)FileManager.readObjectFromFile("local/game_state", Collections.singletonList("GamePanel")); game = (GamePanel)FileManager.readObjectFromFile("local/game_state", Arrays.asList("Any"));
game.gameFrame = main;
game.startThread();
} catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) { } catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) {
System.out.println(e);
game = new GamePanel(main); //run GamePanel constructor game = new GamePanel(main); //run GamePanel constructor
game.startThread();
} }
/*
try {
// read previously saved controls
// SafeObjectInputStream was implemented to prevent arbitrary code execution from occurring if the save files were modified
game.middlewareArray = (ArrayList<Middleware>)FileManager.
readObjectFromFile("local/controls", Arrays.asList("java.util.ArrayList", "Middleware"));
} catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) {
game.middlewareArray = new ArrayList<Middleware>();
}
*/
settings = new SettingPanel(main); settings = new SettingPanel(main);
main.add(menu, "menu"); main.add(menu, "menu");
main.add(settings, "settings"); main.add(settings, "settings");
@ -45,6 +51,7 @@ public class GameFrame extends JFrame{
} catch (IOException | SpriteException | UnsupportedAudioFileException | LineUnavailableException e) { } catch (IOException | SpriteException | UnsupportedAudioFileException | LineUnavailableException e) {
// TODO: handle IO errors gracefully // TODO: handle IO errors gracefully
// exceptions are raised when tiles are not found or are of incorrect dimensions // exceptions are raised when tiles are not found or are of incorrect dimensions
throw new RuntimeException(e);
} }
this.add(main); this.add(main);
this.setTitle("GUI is cool!"); //set title for frame this.setTitle("GUI is cool!"); //set title for frame

View File

@ -25,12 +25,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public static final int GAME_WIDTH = 1225; public static final int GAME_WIDTH = 1225;
public static final int GAME_HEIGHT = 630; public static final int GAME_HEIGHT = 630;
public JPanel gameFrame; public transient JPanel gameFrame;
public Thread gameThread; public transient Thread gameThread;
public Image image; public transient Image image;
public Graphics graphics; public transient Graphics graphics;
public static Player player; public Player player;
public BackgroundImage background; public BackgroundImage background;
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
@ -40,35 +40,27 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public PauseMenu pauseMenu; public PauseMenu pauseMenu;
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; public BufferedImageWrapper[][][] playerSpriteArray = new BufferedImageWrapper[2][2][11];
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; public BufferedImageWrapper[][][] slimeSpriteArray = new BufferedImageWrapper[2][2][3];
public static BufferedImage[] explosionArray = new BufferedImage[9]; public BufferedImageWrapper[] explosionArray = new BufferedImageWrapper[9];
//public static ArrayList<Tile>map = new ArrayList<Tile>(); //public static ArrayList<Tile>map = new ArrayList<Tile>();
public static Tile[][]map = new Tile[300][18]; public Tile[][]map = new Tile[300][18];
public static ArrayList<Middleware> middlewareArray = new ArrayList<Middleware>(); public ArrayList<Middleware> middlewareArray = new ArrayList<Middleware>();
public static ArrayList<Tile>particleTiles = new ArrayList<Tile>(); public ArrayList<Tile>particleTiles = new ArrayList<Tile>();
public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>(); public ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public static ArrayList<StickyBomb>bombs = new ArrayList<>(); public ArrayList<StickyBomb>bombs = new ArrayList<>();
public BombDirectionShow bombDir = null; public BombDirectionShow bombDir = null;
public static ArrayList<Particle>particles = new ArrayList<Particle>(); public ArrayList<Particle>particles = new ArrayList<Particle>();
public static Camera camera; public Camera camera;
// image imports begin here // image imports begin here
public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png"); public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(getImage("img/backgrounds/pointyMountains.png"));
public BufferedImage box = getImage("img/tiles/boxes/box.png"); public BufferedImageWrapper box = new BufferedImageWrapper(getImage("img/tiles/boxes/box.png"));
public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png"); public BufferedImageWrapper boxCoin = new BufferedImageWrapper(getImage("img/tiles/boxes/boxCoin.png"));
public static BufferedImage bomb; public BufferedImageWrapper bomb;
static {
try {
bomb = getImage("img/misc/bomb.png");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
@ -81,31 +73,34 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0'))); BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));
playerSpriteArray[1][0][i] = sprite; playerSpriteArray[1][0][i] = new BufferedImageWrapper(sprite);
playerSpriteArray[1][1][i] = sprite; playerSpriteArray[1][1][i] = new BufferedImageWrapper(sprite);
playerSpriteArray[0][0][i] = flipImageHorizontally(sprite); playerSpriteArray[0][0][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
playerSpriteArray[0][1][i] = flipImageHorizontally(sprite); playerSpriteArray[0][1][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
} }
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
explosionArray[i] = getImage("img/misc/bomb/sonicExplosion0" + i + ".png"); explosionArray[i] = new BufferedImageWrapper(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"), BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk1.png")),
getImage("img/enemy/slime/slimeWalk2.png"), new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk2.png")),
getImage("img/enemy/slime/slimeDead.png")}; new BufferedImageWrapper(getImage("img/enemy/slime/slimeDead.png"))};
BufferedImage[] flippedTemporarySlimeArray = {flipImageHorizontally(getImage("img/enemy/slime/slimeWalk1.png")), BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk1.png"))),
flipImageHorizontally(getImage("img/enemy/slime/slimeWalk2.png")), new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk2.png"))),
flipImageHorizontally(getImage("img/enemy/slime/slimeDead.png"))}; new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeDead.png")))};
// please note that these sprites are reversed compared to the player sprites // please note that these sprites are reversed compared to the player sprites
slimeSpriteArray[0][0] = temporarySlimeArray; slimeSpriteArray[0][0] = temporarySlimeArray;
slimeSpriteArray[0][1] = temporarySlimeArray; slimeSpriteArray[0][1] = temporarySlimeArray;
slimeSpriteArray[1][0] = flippedTemporarySlimeArray; slimeSpriteArray[1][0] = flippedTemporarySlimeArray;
slimeSpriteArray[1][1] = flippedTemporarySlimeArray; slimeSpriteArray[1][1] = flippedTemporarySlimeArray;
// load bomb sprites
bomb = new BufferedImageWrapper(getImage("img/misc/bomb.png"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', playerSpriteArray); //create a player controlled player, set start location to middle of screenk player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, playerSpriteArray); //create a player controlled player, set start location to middle of screenk
// the height of 35 is set because it is half of the original tile height (i.e., 70px) // the height of 35 is set because it is half of the original tile height (i.e., 70px)
this.setFocusable(true); //make everything in this class appear on the screen this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input this.addKeyListener(this); //start listening for keyboard input
@ -124,9 +119,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
try { try {
player.mousePressed(e); player.mousePressed(e);
} catch (SpriteException ex) { } catch (SpriteException | IOException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
@ -146,12 +139,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
} }
}); });
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
}
// startThread is to be called after the game has started to avoid any issues TODO: better explanation
public void startThread() {
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time! //make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread = new Thread(this); gameThread = new Thread(this);
gameThread.start(); gameThread.start();
System.out.println(gameFrame);
} }
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself //paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
@ -179,7 +174,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
g2d.setPaint(Color.white); 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,((this.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);
for(int i=xMin; i<xMax; i++){ for(int i=xMin; i<xMax; i++){
for(int j=0; j<map[0].length; j++){ for(int j=0; j<map[0].length; j++){
@ -212,8 +207,8 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
} }
if(player.leftMouseDown){ if(player.leftMouseDown){
bombDir = new BombDirectionShow(GamePanel.player.x + GamePanel.camera.x + WIDTH/2, GamePanel.player.y+HEIGHT/2, bombDir = new BombDirectionShow(this.player.x + this.camera.x + WIDTH/2, this.player.y+HEIGHT/2,
(player.mouseX - GamePanel.player.x) / 20, (player.mouseY - GamePanel.player.y) / 10); (player.mouseX - this.player.x) / 20, (player.mouseY - this.player.y) / 10);
bombDir.draw(g); bombDir.draw(g);
} }
@ -231,7 +226,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
// g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length)+" "+player.leftMouseDown,100,100); // g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length)+" "+player.leftMouseDown,100,100);
// g.drawString(camera.x+" "+((player.mouseX+camera.x+GAME_WIDTH/2)/Tile.length)+" "+player.leftMouseDown,100,200); // g.drawString(camera.x+" "+((player.mouseX+camera.x+GAME_WIDTH/2)/Tile.length)+" "+player.leftMouseDown,100,200);
g.drawImage(bomb,20,20,35,35,null); g.drawImage(bomb.image,20,20,35,35,null);
g.drawString("X"+LevelManager.bombs,60,40); g.drawString("X"+LevelManager.bombs,60,40);
if (isPaused) { if (isPaused) {
g.setColor(new Color(255, 255, 255, 100)); g.setColor(new Color(255, 255, 255, 100));

View File

@ -7,8 +7,9 @@ In 2D GUI, basically everything is a rectangle even if it doesn't look like it!
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
public class GenericSprite extends Rectangle{ public class GenericSprite extends Rectangle implements Serializable {
public double yVelocity; public double yVelocity;
public double xVelocity; public double xVelocity;
@ -87,14 +88,14 @@ public class GenericSprite extends Rectangle{
public boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
int lowX = Math.max(0, (this.x+GamePanel.GAME_WIDTH/2)/Tile.length-4); int lowX = Math.max(0, (this.x+GamePanel.GAME_WIDTH/2)/Tile.length-4);
int highX = Math.min(lowX + 8, GamePanel.map.length); int highX = Math.min(lowX + 8, GameFrame.game.map.length);
int lowY = Math.max(0,(this.y/Tile.length)-6); int lowY = Math.max(0,(this.y/Tile.length)-6);
int highY = Math.min(lowY + 12, GamePanel.map[0].length); int highY = Math.min(lowY + 12, GameFrame.game.map[0].length);
for(int i=lowX; i<highX; i++) { for(int i=lowX; i<highX; i++) {
for (int j = lowY; j < highY; j++) { for (int j = lowY; j < highY; j++) {
if (GamePanel.map[i][j] != null) { if (GameFrame.game.map[i][j] != null) {
if (collide(GamePanel.map[i][j], this.x + x, this.y + y)) { if (collide(GameFrame.game.map[i][j], this.x + x, this.y + y)) {
if (GamePanel.map[i][j].isFinish&&isPlayer) { if (GameFrame.game.map[i][j].isFinish&&isPlayer) {
LevelManager.nextLevel(); LevelManager.nextLevel();
return true; return true;
} }

View File

@ -1,4 +1,6 @@
public class GlobalState { import java.io.Serializable;
public class GlobalState implements Serializable {
public static final int second = 10; public static final int second = 10;
public static int randInt(int low, int high){ public static int randInt(int low, int high){
return (int)(Math.random()*(high-low+1))+low; return (int)(Math.random()*(high-low+1))+low;

View File

@ -1,8 +1,9 @@
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
public class LevelManager { public class LevelManager implements Serializable {
public static int level = 1; public static int level = 1;
public static int xSpawn = 0; public static int xSpawn = 0;
public static int ySpawn = 600; public static int ySpawn = 600;
@ -11,8 +12,8 @@ public class LevelManager {
public static int bombs; public static int bombs;
public static void setLevel(int level){ public static void setLevel(int level){
GamePanel.player.yVelocity = 0; GameFrame.game.player.yVelocity = 0;
GamePanel.player.xVelocity = 0; GameFrame.game.player.xVelocity = 0;
LevelManager.level = level; LevelManager.level = level;
if(level == 1){ if(level == 1){
xSpawn = 0; xSpawn = 0;

View File

@ -3,7 +3,7 @@ import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
public class MapReader { public class MapReader implements Serializable {
public static int x = 0; public static int x = 0;
public static int y = 0; public static int y = 0;
public static int TileX = 0; public static int TileX = 0;
@ -26,10 +26,10 @@ public class MapReader {
public static void inputMap(String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public static void inputMap(String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
x = 0; x = 0;
y = 0; y = 0;
GamePanel.enemy.clear(); GameFrame.game.enemy.clear();
GamePanel.particleTiles.clear(); GameFrame.game.particleTiles.clear();
for(int i=0; i<GamePanel.map.length; i++){ for(int i=0; i<GameFrame.game.map.length; i++){
Arrays.fill(GamePanel.map[i], null); Arrays.fill(GameFrame.game.map[i], null);
} }
String file = FileManager.readFile(filePath); String file = FileManager.readFile(filePath);
for(int i=0; i<file.length(); i++){ for(int i=0; i<file.length(); i++){
@ -76,33 +76,33 @@ public class MapReader {
newTile("img/tiles/terrain/cornerBottomRight.png"); newTile("img/tiles/terrain/cornerBottomRight.png");
} else if(file.charAt(i)=='b'){ } else if(file.charAt(i)=='b'){
newTile("img/tiles/boxes/box.png"); newTile("img/tiles/boxes/box.png");
GamePanel.map[x][y].breakable = true; GameFrame.game.map[x][y].breakable = true;
} else if(file.charAt(i)=='!'){ } else if(file.charAt(i)=='!'){
GamePanel.enemy.add(new NonPlayer(TileX, TileY, GamePanel.slimeSpriteArray, 50, 28, 100)); GameFrame.game.enemy.add(new NonPlayer(TileX, TileY, GameFrame.game.slimeSpriteArray, 50, 28, 100));
} else if(file.charAt(i)=='+') { } else if(file.charAt(i)=='+') {
newTile("img/tiles/boxes/finish.png"); newTile("img/tiles/boxes/finish.png");
GamePanel.map[x][y].isFinish = true; GameFrame.game.map[x][y].isFinish = true;
GamePanel.map[x][y].nonBombCollide = true; GameFrame.game.map[x][y].nonBombCollide = true;
} else if(file.charAt(i)=='v'){ } else if(file.charAt(i)=='v'){
newTile("img/tiles/background/wall.png"); newTile("img/tiles/background/wall.png");
GamePanel.map[x][y].collision = false; GameFrame.game.map[x][y].collision = false;
GamePanel.map[x][y].replaceAble = true; GameFrame.game.map[x][y].replaceAble = true;
} else if(file.charAt(i)=='l'){ } else if(file.charAt(i)=='l'){
newTile("img/tiles/terrain/lava.png"); newTile("img/tiles/terrain/lava.png");
GamePanel.map[x][y].nonBombCollide = true; GameFrame.game.map[x][y].nonBombCollide = true;
GamePanel.map[x][y].kills = true; GameFrame.game.map[x][y].kills = true;
if(y>0&&GamePanel.map[x][y-1]==null) { if(y>0&&GameFrame.game.map[x][y-1]==null) {
GamePanel.particleTiles.add(GamePanel.map[x][y]); GameFrame.game.particleTiles.add(GameFrame.game.map[x][y]);
} }
} else if(file.charAt(i)=='o'){ } else if(file.charAt(i)=='o'){
newTile("img/tiles/boxes/steel.png"); newTile("img/tiles/boxes/steel.png");
GamePanel.map[x][y].movable = true; GameFrame.game.map[x][y].movable = true;
} }
x+=1; x+=1;
} }
} }
public static void newTile(String filePath) throws IOException, SpriteException { public static void newTile(String filePath) throws IOException, SpriteException {
GamePanel.map[x][y]=(new SingleTile(TileX,TileY, GamePanel.getImage(filePath))); GameFrame.game.map[x][y]=(new SingleTile(TileX,TileY, new BufferedImageWrapper(GamePanel.getImage(filePath))));
} }
} }

View File

@ -39,7 +39,7 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
public static boolean gameStart = false; public static boolean gameStart = false;
// image imports begin here // image imports begin here
public BufferedImage backgroundImage = GamePanel.getImage("img/backgrounds/pointyMountains.png"); public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(GamePanel.getImage("img/backgrounds/pointyMountains.png"));
public MenuPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public MenuPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
this.gameFrame = gameFrame; this.gameFrame = gameFrame;
@ -148,7 +148,7 @@ public class MenuPanel 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) {
e = UtilityFunction.intercept(e, GamePanel.middlewareArray); e = UtilityFunction.intercept(e, GameFrame.game.middlewareArray);
if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if(textBoxArray.get(currentBox).id.equals("game")){ if(textBoxArray.get(currentBox).id.equals("game")){
gameStart = true; gameStart = true;

View File

@ -7,8 +7,9 @@ import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
public class NonPlayer extends GenericSprite { public class NonPlayer extends GenericSprite implements Serializable {
public final int SPEED = 3; public final int SPEED = 3;
// please note that these are not static, in contrast to the player class, as different enemies will have different heights // please note that these are not static, in contrast to the player class, as different enemies will have different heights
public int npcWidth; public int npcWidth;
@ -23,8 +24,8 @@ public class NonPlayer extends GenericSprite {
public double fadeCounter; public double fadeCounter;
// private final Sound bump; // private final Sound bump;
public BufferedImage[][][] spriteArray; public BufferedImageWrapper[][][] spriteArray;
public NonPlayer(int x, int y, BufferedImage[][][] sprites, int npcWidth, int npcHeight, int health) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public NonPlayer(int x, int y, BufferedImageWrapper[][][] sprites, int npcWidth, int npcHeight, int health) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
super(x, y, npcHeight, npcWidth); super(x, y, npcHeight, npcWidth);
// bump = new Sound("sound/bump.wav"); // bump = new Sound("sound/bump.wav");
this.health = health; this.health = health;
@ -49,7 +50,7 @@ public class NonPlayer extends GenericSprite {
public void update(){ public void update(){
realX = x-GamePanel.camera.x; realX = x-GameFrame.game.camera.x;
} }
public void move(){ public void move(){
if (isDead) { if (isDead) {
@ -88,12 +89,12 @@ public class NonPlayer extends GenericSprite {
frame %= spriteArray[0][0].length - 1; frame %= spriteArray[0][0].length - 1;
currentXDirection = (int)(Math.signum(xVelocity) + 1) / 2; currentXDirection = (int)(Math.signum(xVelocity) + 1) / 2;
currentYDirection = (int)(Math.signum(yVelocity) + 1) / 2; currentYDirection = (int)(Math.signum(yVelocity) + 1) / 2;
// x-GamePanel.camera.x is used as the camera doesn't follow NPCs // x-GameFrame.game.camera.x is used as the camera doesn't follow NPCs
g.drawImage(spriteArray[currentXDirection][currentYDirection][frame], x-GamePanel.camera.x, y, null); g.drawImage(spriteArray[currentXDirection][currentYDirection][frame].image, x-GameFrame.game.camera.x, y, null);
return 1; return 1;
} else { } else {
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)fadeCounter)); ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)fadeCounter));
g.drawImage(spriteArray[currentXDirection][currentYDirection][spriteArray[0][0].length-1], x-GamePanel.camera.x, (int)(y+HEIGHT/1.7), null); g.drawImage(spriteArray[currentXDirection][currentYDirection][spriteArray[0][0].length-1].image, x-GameFrame.game.camera.x, (int)(y+HEIGHT/1.7), null);
// reset composite to not affect other sprites being drawn // reset composite to not affect other sprites being drawn
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
fadeCounter -= 0.01; fadeCounter -= 0.01;

View File

@ -1,8 +1,9 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
public class Particle extends GenericSprite{ public class Particle extends GenericSprite implements Serializable {
public static final int small = 3; public static final int small = 3;
public static final int big = 10; public static final int big = 10;
@ -12,13 +13,13 @@ public class Particle extends GenericSprite{
public int lifeSpan = 10; public int lifeSpan = 10;
public BufferedImage sprite; public BufferedImageWrapper sprite;
public Particle(int x, int y, int xVelocity, int yVelocity, int length, String filePath) throws IOException { public Particle(int x, int y, int xVelocity, int yVelocity, int length, String filePath) throws IOException {
super(x,y,length, length); super(x,y,length, length);
this.xVelocity = xVelocity; this.xVelocity = xVelocity;
this.yVelocity = yVelocity; this.yVelocity = yVelocity;
sprite = GamePanel.getImage(filePath); sprite = new BufferedImageWrapper(GamePanel.getImage(filePath));
} }
public void move(){ public void move(){
x+=xVelocity; x+=xVelocity;
@ -26,7 +27,7 @@ public class Particle extends GenericSprite{
yVelocity+=0.5; yVelocity+=0.5;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawImage(sprite,x-GamePanel.camera.x,y,WIDTH,HEIGHT, null); g.drawImage(sprite.image,x-GameFrame.game.camera.x,y,WIDTH,HEIGHT, null);
} }
} }

View File

@ -1,6 +1,7 @@
import java.awt.*; import java.awt.*;
import java.io.Serializable;
public class PauseMenu extends TextBox { public class PauseMenu extends TextBox implements Serializable {
public PauseMenu(int y, int textYOffset, int xWidth, int yHeight, int totalWidth, Font font, String text) { public PauseMenu(int y, int textYOffset, int xWidth, int yHeight, int totalWidth, Font font, String text) {
super(y, xWidth, yHeight, totalWidth, font, text, null); super(y, xWidth, yHeight, totalWidth, font, text, null);
this.y -= textYOffset; this.y -= textYOffset;

View File

@ -18,11 +18,10 @@ public class Player extends GenericSprite {
public static final int steelReachRange = 3*Tile.length; public static final int steelReachRange = 3*Tile.length;
public int lastXDirection, lastYDirection, lastFrame; public int lastXDirection, lastYDirection, lastFrame;
public int upKey, downKey, rightKey, leftKey;
public static final int walkSpeedCap = 5; public static final int walkSpeedCap = 5;
public boolean alive; public boolean alive;
private final Sound jump; private transient final Sound jump = new Sound("sound/jump.wav");
public static int mouseX; public static int mouseX;
public static int mouseY; public static int mouseY;
@ -38,14 +37,10 @@ public class Player extends GenericSprite {
// sA[1] is x, -y // sA[1] is x, -y
// sA[2] is -x, y // sA[2] is -x, y
// sA[3] is x, y // sA[3] is x, y
public BufferedImage[][][] spriteArray; public BufferedImageWrapper[][][] spriteArray;
public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public Player(int x, int y, BufferedImageWrapper[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH); super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
jump = new Sound("sound/jump.wav"); // jump = new Sound("sound/jump.wav");
this.upKey = upKey;
this.downKey = downKey;
this.leftKey = leftKey;
this.rightKey = rightKey;
spriteArray = sprites; spriteArray = sprites;
alive = true; alive = true;
isPlayer = true; isPlayer = true;
@ -102,21 +97,21 @@ public class Player extends GenericSprite {
public boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
int lowX = Math.max(0, ((GamePanel.camera.x+GamePanel.GAME_WIDTH)/Tile.length)-4); int lowX = Math.max(0, ((GameFrame.game.camera.x+GamePanel.GAME_WIDTH)/Tile.length)-4);
int highX = Math.min(lowX + 8, GamePanel.map.length); int highX = Math.min(lowX + 8, GameFrame.game.map.length);
int lowY = Math.max(0,(this.y/Tile.length)-6); int lowY = Math.max(0,(this.y/Tile.length)-6);
int highY = Math.min(lowY + 12, GamePanel.map[0].length); int highY = Math.min(lowY + 12, GameFrame.game.map[0].length);
for(int i=lowX; i<highX; i++) { for(int i=lowX; i<highX; i++) {
for (int j = lowY; j < highY; j++) { for (int j = lowY; j < highY; j++) {
if (GamePanel.map != null) { if (GameFrame.game.map != null) {
if (collide(GamePanel.map[i][j], this.x + x, this.y + y)) { if (collide(GameFrame.game.map[i][j], this.x + x, this.y + y)) {
if (GamePanel.map[i][j].isFinish) { if (GameFrame.game.map[i][j].isFinish) {
LevelManager.nextLevel(); LevelManager.nextLevel();
GamePanel.player.reset(); GameFrame.game.player.reset();
return true; return true;
} }
if (GamePanel.map[i][j].kills) { if (GameFrame.game.map[i][j].kills) {
GamePanel.player.reset(); GameFrame.game.player.reset();
return true; return true;
} }
canUpdate = false; canUpdate = false;
@ -137,12 +132,12 @@ public class Player extends GenericSprite {
public void updatePlaceSteel(int x, int y){ public void updatePlaceSteel(int x, int y){
canPlaceSteel = true; canPlaceSteel = true;
boolean adjacent = false; boolean adjacent = false;
int realX = x*Tile.length-GamePanel.camera.x; int realX = x*Tile.length-GameFrame.game.camera.x;
System.out.println(realX); System.out.println(realX);
int[][]check = {{1,0},{0,1},{-1,0},{0,-1}}; int[][]check = {{1,0},{0,1},{-1,0},{0,-1}};
for(int[]a: check){ for(int[]a: check){
try{ try{
if(GamePanel.map[x+a[0]][y+a[1]]!=null&&!GamePanel.map[x+a[0]][y+a[1]].replaceAble){ if(GameFrame.game.map[x+a[0]][y+a[1]]!=null&&!GameFrame.game.map[x+a[0]][y+a[1]].replaceAble){
adjacent = true; adjacent = true;
break; break;
} }
@ -151,18 +146,18 @@ public class Player extends GenericSprite {
} }
} }
if(!adjacent){canPlaceSteel = false; return;} if(!adjacent){canPlaceSteel = false; return;}
if(GamePanel.map[x][y]!=null&&!GamePanel.map[x][y].replaceAble){canPlaceSteel = false; return;}; if(GameFrame.game.map[x][y]!=null&&!GameFrame.game.map[x][y].replaceAble){canPlaceSteel = false; return;};
} }
public void move() throws IOException { public void move() throws IOException {
// mouseX = MouseInfo.getPointerInfo().getLocation().x; // mouseX = MouseInfo.getPointerInfo().getLocation().x;
// mouseY = MouseInfo.getPointerInfo().getLocation().y; // mouseY = MouseInfo.getPointerInfo().getLocation().y;
int Tilex = (mouseX + GamePanel.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length; int Tilex = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length;
int Tiley = (mouseY / Tile.length); int Tiley = (mouseY / Tile.length);
if(holdingSteel){ if(holdingSteel){
updatePlaceSteel(Tilex,Tiley); updatePlaceSteel(Tilex,Tiley);
} }
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
GamePanel.camera.x += -Math.signum(xVelocity); GameFrame.game.camera.x += -Math.signum(xVelocity);
} }
if(!canUpdate(xVelocity, 0)){ if(!canUpdate(xVelocity, 0)){
int updateAmount = 0; int updateAmount = 0;
@ -170,12 +165,12 @@ public class Player extends GenericSprite {
while(canUpdate(updateAmount, 0)){ while(canUpdate(updateAmount, 0)){
updateAmount++; updateAmount++;
} }
GamePanel.camera.x+=updateAmount-1; GameFrame.game.camera.x+=updateAmount-1;
} else if(xVelocity<0){ } else if(xVelocity<0){
while(canUpdate(updateAmount, 0)){ while(canUpdate(updateAmount, 0)){
updateAmount--; updateAmount--;
} }
GamePanel.camera.x+=updateAmount+1; GameFrame.game.camera.x+=updateAmount+1;
} }
//xVelocity = 0; //xVelocity = 0;
} }
@ -194,12 +189,12 @@ public class Player extends GenericSprite {
} }
if(canUpdate(xVelocity, yVelocity)) { if(canUpdate(xVelocity, yVelocity)) {
y = y + (int) yVelocity; y = y + (int) yVelocity;
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; GameFrame.game.camera.x = GameFrame.game.camera.x + (int) xVelocity;
} else if(canUpdate(0,yVelocity)){ } else if(canUpdate(0,yVelocity)){
y = y + (int) yVelocity; y = y + (int) yVelocity;
xVelocity*=0.75; xVelocity*=0.75;
} else if(canUpdate(xVelocity,0)){ } else if(canUpdate(xVelocity,0)){
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; GameFrame.game.camera.x = GameFrame.game.camera.x + (int) xVelocity;
} }
@ -245,7 +240,7 @@ public class Player extends GenericSprite {
public void reset(){ public void reset(){
LevelManager.setLevel(LevelManager.level); LevelManager.setLevel(LevelManager.level);
GamePanel.camera.x = LevelManager.xSpawn; GameFrame.game.camera.x = LevelManager.xSpawn;
y = LevelManager.ySpawn; y = LevelManager.ySpawn;
holdingSteel = false; holdingSteel = false;
} }
@ -257,28 +252,28 @@ public class Player extends GenericSprite {
leftMouseDown = true; leftMouseDown = true;
} }
if(e.getButton()==MouseEvent.BUTTON3) { if(e.getButton()==MouseEvent.BUTTON3) {
int x = (mouseX + GamePanel.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length; int x = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length;
int y = (mouseY / Tile.length); int y = (mouseY / Tile.length);
rightMouseDown = true; rightMouseDown = true;
if (!holdingSteel) { if (!holdingSteel) {
if (GamePanel.map[x][y] != null&&GamePanel.map[x][y].movable) { if (GameFrame.game.map[x][y] != null&&GameFrame.game.map[x][y].movable) {
double xDis = (this.x+WIDTH/2) - (GamePanel.map[x][y].realX+Tile.length/2); double xDis = (this.x+WIDTH/2) - (GameFrame.game.map[x][y].realX+Tile.length/2);
double yDis = (this.y+HEIGHT/2) - (GamePanel.map[x][y].y+Tile.length/2); double yDis = (this.y+HEIGHT/2) - (GameFrame.game.map[x][y].y+Tile.length/2);
double hypo = Math.sqrt(xDis*xDis+yDis*yDis); double hypo = Math.sqrt(xDis*xDis+yDis*yDis);
if(hypo<steelReachRange) { if(hypo<steelReachRange) {
holdingSteel = true; holdingSteel = true;
if (GamePanel.map[x][y].previousBlock != null) { if (GameFrame.game.map[x][y].previousBlock != null) {
GamePanel.map[x][y] = GamePanel.map[x][y].previousBlock; GameFrame.game.map[x][y] = GameFrame.game.map[x][y].previousBlock;
} else { } else {
GamePanel.map[x][y] = null; GameFrame.game.map[x][y] = null;
} }
} }
} }
} else if((GamePanel.map[x][y] == null||GamePanel.map[x][y].replaceAble)&&canPlaceSteel){ } else if((GameFrame.game.map[x][y] == null||GameFrame.game.map[x][y].replaceAble)&&canPlaceSteel){
Tile temp = GamePanel.map[x][y]; Tile temp = GameFrame.game.map[x][y];
GamePanel.map[x][y] = new SingleTile(x*Tile.length - (GamePanel.GAME_WIDTH/2), y*Tile.length, GamePanel.getImage("img/tiles/boxes/steel.png")); GameFrame.game.map[x][y] = new SingleTile(x*Tile.length - (GamePanel.GAME_WIDTH /2), y*Tile.length, new BufferedImageWrapper(GamePanel.getImage("img/tiles/boxes/steel.png")));
GamePanel.map[x][y].movable = true; GameFrame.game.map[x][y].movable = true;
GamePanel.map[x][y].previousBlock = temp; GameFrame.game.map[x][y].previousBlock = temp;
holdingSteel = false; holdingSteel = false;
} }
} }
@ -301,10 +296,10 @@ public class Player extends GenericSprite {
mouseY = e.getY(); mouseY = e.getY();
if(e.getButton()==MouseEvent.BUTTON1) { if(e.getButton()==MouseEvent.BUTTON1) {
leftMouseDown = false; leftMouseDown = false;
if (GamePanel.bombs.size() < 3 && LevelManager.bombs>0) { if (GameFrame.game.bombs.size() < 3 && LevelManager.bombs>0) {
LevelManager.bombs--; LevelManager.bombs--;
GamePanel.bombs.add(new StickyBomb(GamePanel.player.x + GamePanel.camera.x + WIDTH/2, GamePanel.player.y+HEIGHT/2, GameFrame.game.bombs.add(new StickyBomb(GameFrame.game.player.x + GameFrame.game.camera.x + WIDTH/2, GameFrame.game.player.y+HEIGHT/2,
(mouseX - GamePanel.player.x) / 20, (mouseY - GamePanel.player.y) / 10, GamePanel.bomb, GamePanel.explosionArray)); (mouseX - GameFrame.game.player.x) / 20, (mouseY - GameFrame.game.player.y) / 10, GameFrame.game.bomb, GameFrame.game.explosionArray));
} }
} }
if(e.getButton()==MouseEvent.BUTTON3){ if(e.getButton()==MouseEvent.BUTTON3){
@ -313,7 +308,7 @@ public class Player extends GenericSprite {
} }
public void addParticle(int x) throws IOException { public void addParticle(int x) throws IOException {
if(GlobalState.randInt(1,3)==3) { if(GlobalState.randInt(1,3)==3) {
GamePanel.particles.add(new Particle(this.x + GamePanel.camera.x + WIDTH / 2 + GlobalState.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2) GameFrame.game.particles.add(new Particle(this.x + GameFrame.game.camera.x + WIDTH / 2 + GlobalState.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2)
, (int) (y + HEIGHT * 0.95), GlobalState.randInt(-2, 2) + x, GlobalState.randInt(-4, 1), GlobalState.randInt(1, 7), "img/particles/GrassParticle.png")); , (int) (y + HEIGHT * 0.95), GlobalState.randInt(-2, 2) + x, GlobalState.randInt(-4, 1), GlobalState.randInt(1, 7), "img/particles/GrassParticle.png"));
} }
} }
@ -323,13 +318,13 @@ public class Player extends GenericSprite {
g.drawOval(x+WIDTH/2-steelReachRange,y+HEIGHT/2-steelReachRange, steelReachRange*2,steelReachRange*2); g.drawOval(x+WIDTH/2-steelReachRange,y+HEIGHT/2-steelReachRange, steelReachRange*2,steelReachRange*2);
} }
if (!upPressed && !downPressed && !leftPressed && !rightPressed) { if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-10, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][0].image, x-10, y, null);
return 0; return 0;
} else { } else {
lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2; lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2;
lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2; lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2;
lastFrame = frame; lastFrame = frame;
g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-10, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][frame].image, x-10, y, null);
return 1; return 1;
} }

View File

@ -1,4 +1,4 @@
/* GamePanel class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called /* GameFrame.game.class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called
Child of JPanel because JPanel contains methods for drawing to the screen Child of JPanel because JPanel contains methods for drawing to the screen
@ -36,9 +36,6 @@ public class SettingPanel extends MenuPanel {
public int currentBox = 0; public int currentBox = 0;
public PauseMenu pauseMenu; public PauseMenu pauseMenu;
// image imports begin here
public BufferedImage backgroundImage = GamePanel.getImage("img/backgrounds/pointyMountains.png");
public SettingPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public SettingPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
super(gameFrame); super(gameFrame);
@ -63,19 +60,19 @@ public class SettingPanel extends MenuPanel {
title.draw(g,null, Color.black); title.draw(g,null, Color.black);
for (TextBox t: textBoxArray) { for (TextBox t: textBoxArray) {
oldText = t.text; oldText = t.text;
middlewareIndex = GamePanel.middlewareArray.indexOf(new Middleware(Integer.parseInt(t.id), -1)); middlewareIndex = GameFrame.game.middlewareArray.indexOf(new Middleware(Integer.parseInt(t.id), -1));
t.text += "(" + (middlewareIndex > -1 ? (char)GamePanel.middlewareArray.get(middlewareIndex).newCode: t.text += "(" + (middlewareIndex > -1 ? (char)GameFrame.game.middlewareArray.get(middlewareIndex).newCode:
GamePanel.middlewareArray.contains(new Middleware(-2, Integer.parseInt(t.id))) ? GameFrame.game.middlewareArray.contains(new Middleware(-2, Integer.parseInt(t.id))) ?
"None":(char)Integer.parseInt(t.id)) + ")"; "None":(char)Integer.parseInt(t.id)) + ")";
t.draw(g, null, Color.cyan); t.draw(g, null, Color.cyan);
t.text = oldText; t.text = oldText;
} }
oldText = textBoxArray.get(currentBox).text; oldText = textBoxArray.get(currentBox).text;
// TODO: clean up // TODO: clean up
middlewareIndex = GamePanel.middlewareArray.indexOf(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), -1)); middlewareIndex = GameFrame.game.middlewareArray.indexOf(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), -1));
// -2 was chosen as oldCode instead of -1 to prevent conflicts // -2 was chosen as oldCode instead of -1 to prevent conflicts
textBoxArray.get(currentBox).text += "(" + (middlewareIndex > -1 ? (char)GamePanel.middlewareArray.get(middlewareIndex).newCode: textBoxArray.get(currentBox).text += "(" + (middlewareIndex > -1 ? (char)GameFrame.game.middlewareArray.get(middlewareIndex).newCode:
GamePanel.middlewareArray.contains(new Middleware(-2, Integer.parseInt(textBoxArray.get(currentBox).id))) ? GameFrame.game.middlewareArray.contains(new Middleware(-2, Integer.parseInt(textBoxArray.get(currentBox).id))) ?
"None":(char)Integer.parseInt(textBoxArray.get(currentBox).id)) + ")"; "None":(char)Integer.parseInt(textBoxArray.get(currentBox).id)) + ")";
textBoxArray.get(currentBox).draw(g, Color.gray, Color.blue); textBoxArray.get(currentBox).draw(g, Color.gray, Color.blue);
textBoxArray.get(currentBox).text = oldText; textBoxArray.get(currentBox).text = oldText;
@ -108,18 +105,18 @@ public class SettingPanel extends MenuPanel {
boolean canRemove = true; boolean canRemove = true;
while (canRemove) { while (canRemove) {
// newCode is -1 as it does not matter // newCode is -1 as it does not matter
canRemove = GamePanel.middlewareArray.remove(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), lastKeyCode)); canRemove = GameFrame.game.middlewareArray.remove(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), lastKeyCode));
} }
// add actual middleware // add actual middleware
GamePanel.middlewareArray.add(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), lastKeyCode)); GameFrame.game.middlewareArray.add(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), lastKeyCode));
// add middleware to redirect default key // add middleware to redirect default key
GamePanel.middlewareArray.add(new Middleware(-1, Integer.parseInt(textBoxArray.get(currentBox).id))); GameFrame.game.middlewareArray.add(new Middleware(-1, Integer.parseInt(textBoxArray.get(currentBox).id)));
// lastKeyCode is set to -1 to prevent endless execution // lastKeyCode is set to -1 to prevent endless execution
lastKeyCode = -1; lastKeyCode = -1;
// save new key bind to file // save new key bind to file
try { try {
FileManager.writeObjectToFile("local/controls", GamePanel.middlewareArray); FileManager.writeObjectToFile("local/controls", GameFrame.game.middlewareArray);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -129,7 +126,7 @@ public class SettingPanel extends MenuPanel {
//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 (!waitForKey) { if (!waitForKey) {
e = UtilityFunction.intercept(e, GamePanel.middlewareArray); e = UtilityFunction.intercept(e, GameFrame.game.middlewareArray);
} }
if (waitForKey) { if (waitForKey) {
if (e.getKeyCode() != KeyEvent.VK_ENTER) { if (e.getKeyCode() != KeyEvent.VK_ENTER) {

View File

@ -1,18 +1,19 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.Serializable;
public class SingleTile extends Tile { public class SingleTile extends Tile implements Serializable {
public BufferedImage tileImage; public BufferedImageWrapper tileImage;
public SingleTile(int x, int y, BufferedImage tileImage) throws SpriteException { public SingleTile(int x, int y, BufferedImageWrapper tileImage) throws SpriteException {
super(x, y); super(x, y);
if (tileImage.getWidth() != tileImage.getHeight()) { if (tileImage.image.getWidth() != tileImage.image.getHeight()) {
throw new SpriteException(); throw new SpriteException();
} }
this.tileImage = tileImage; this.tileImage = tileImage;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawImage(tileImage, x-GamePanel.camera.x, y, length, length, null); g.drawImage(tileImage.image, x-GameFrame.game.camera.x, y, length, length, null);
} }
} }

View File

@ -1,8 +1,9 @@
import javax.sound.sampled.*; import javax.sound.sampled.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
public class Sound { public class Sound implements Serializable {
private AudioInputStream audioInputStream; private AudioInputStream audioInputStream;
private Clip clip; private Clip clip;
private File file; private File file;

View File

@ -2,8 +2,9 @@ import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.Serializable;
public class StickyBomb extends GenericSprite{ public class StickyBomb extends GenericSprite implements Serializable {
public static final int spriteLength = 35; public static final int spriteLength = 35;
@ -12,8 +13,8 @@ public class StickyBomb extends GenericSprite{
public int yVelocity; public int yVelocity;
public boolean isMove; public boolean isMove;
public int realX; public int realX;
public BufferedImage sprite ; public BufferedImageWrapper sprite ;
public BufferedImage[] explosionSpriteArray; public BufferedImageWrapper[] explosionSpriteArray;
public int fuse; public int fuse;
public int explosionPixel = 0; public int explosionPixel = 0;
@ -25,7 +26,7 @@ public class StickyBomb extends GenericSprite{
public StickyBomb(int x, int y, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){ public StickyBomb(int x, int y, int xVelocity, int yVelocity, BufferedImageWrapper sprite, BufferedImageWrapper[] explosionSpriteArray){
super(x,y,length,length); super(x,y,length,length);
this.xVelocity = xVelocity; this.xVelocity = xVelocity;
this.yVelocity = yVelocity; this.yVelocity = yVelocity;
@ -40,43 +41,43 @@ public class StickyBomb extends GenericSprite{
public void update(){ public void update(){
realX = x - GamePanel.camera.x; realX = x - GameFrame.game.camera.x;
} }
public void explode(){ public void explode(){
double yDis = GamePanel.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2); double yDis = GameFrame.game.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2);
double xDis = GamePanel.player.x+Player.PLAYER_WIDTH/2-(realX+(double)length/2); double xDis = GameFrame.game.player.x+Player.PLAYER_WIDTH/2-(realX+(double)length/2);
double hypo = Math.sqrt(yDis*yDis+xDis*xDis); double hypo = Math.sqrt(yDis*yDis+xDis*xDis);
if(hypo<300) { if(hypo<300) {
if (yDis != 0) { if (yDis != 0) {
GamePanel.player.yVelocity += 10 * (yDis) / (hypo); GameFrame.game.player.yVelocity += 10 * (yDis) / (hypo);
} }
if (xDis != 0) { if (xDis != 0) {
GamePanel.player.xVelocity += 10 * (xDis) / (hypo); GameFrame.game.player.xVelocity += 10 * (xDis) / (hypo);
} }
} }
GamePanel.player.capSpeed(); GameFrame.game.player.capSpeed();
alive = false; alive = false;
for(int i=0; i<GamePanel.enemy.size(); i++){ for(int i=0; i<GameFrame.game.enemy.size(); i++){
double disX = GamePanel.enemy.get(i).x+GamePanel.enemy.get(i).npcWidth/2 - (x+length/2); double disX = GameFrame.game.enemy.get(i).x+GameFrame.game.enemy.get(i).npcWidth/2 - (x+length/2);
double disY = GamePanel.enemy.get(i).y+GamePanel.enemy.get(i).npcHeight/2 - (y+length/2); double disY = GameFrame.game.enemy.get(i).y+GameFrame.game.enemy.get(i).npcHeight/2 - (y+length/2);
double eHypo = Math.sqrt(disX*disX+disY*disY); double eHypo = Math.sqrt(disX*disX+disY*disY);
if(eHypo<200){ if(eHypo<200){
GamePanel.enemy.get(i).isDead = true; GameFrame.game.enemy.get(i).isDead = true;
} }
} }
int lowX = Math.max(0, ((x+GamePanel.GAME_WIDTH)/Tile.length)-4); int lowX = Math.max(0, ((x+GamePanel.GAME_WIDTH)/Tile.length)-4);
int highX = Math.min(lowX + 8, GamePanel.map.length); int highX = Math.min(lowX + 8, GameFrame.game.map.length);
int lowY = Math.max(0,(this.y/Tile.length)-6); int lowY = Math.max(0,(this.y/Tile.length)-6);
int highY = Math.min(lowY + 12, GamePanel.map[0].length); int highY = Math.min(lowY + 12, GameFrame.game.map[0].length);
for(int i=0; i<100; i++) { for(int i=0; i<100; i++) {
for (int j = lowY; j < highY; j++) { for (int j = lowY; j < highY; j++) {
if (GamePanel.map[i][j] != null && GamePanel.map[i][j].breakable) { if (GameFrame.game.map[i][j] != null && GameFrame.game.map[i][j].breakable) {
double disX = (x + WIDTH / 2) - (GamePanel.map[i][j].x + Tile.length / 2); double disX = (x + WIDTH / 2) - (GameFrame.game.map[i][j].x + Tile.length / 2);
double disY = (y + HEIGHT / 2) - (GamePanel.map[i][j].y + Tile.length / 2); double disY = (y + HEIGHT / 2) - (GameFrame.game.map[i][j].y + Tile.length / 2);
if (Math.sqrt(disX * disX + disY * disY) < Tile.length * 5) { if (Math.sqrt(disX * disX + disY * disY) < Tile.length * 5) {
GamePanel.map[i][j] = null; GameFrame.game.map[i][j] = null;
} }
} }
} }
@ -197,10 +198,10 @@ public class StickyBomb extends GenericSprite{
explosionCounter -= 2; explosionCounter -= 2;
} }
if(alive) { if(alive) {
g.drawImage(sprite, x - GamePanel.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null); g.drawImage(sprite.image, x - GameFrame.game.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null);
//g.drawRect(x-GamePanel.camera.x,y,length,length); //g.drawRect(x-GameFrame.game.camera.x,y,length,length);
} else if (explosionPixel < explosionSpriteArray.length - 1) { } else if (explosionPixel < explosionSpriteArray.length - 1) {
g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel, g.drawImage(explosionSpriteArray[explosionPixel].image, x - GameFrame.game.camera.x - 10*explosionPixel,
y-10*explosionPixel, spriteLength+10*explosionPixel, spriteLength+10*explosionPixel, null); y-10*explosionPixel, spriteLength+10*explosionPixel, spriteLength+10*explosionPixel, null);
explosionCounter += 1; explosionCounter += 1;
} else { } else {

View File

@ -1,6 +1,7 @@
import java.awt.*; import java.awt.*;
import java.io.Serializable;
public class TextBox extends Rectangle { public class TextBox extends Rectangle implements Serializable {
int y; int y;
int xWidth, yHeight; int xWidth, yHeight;
int newX, newY; int newX, newY;

View File

@ -1,7 +1,8 @@
import java.awt.*; import java.awt.*;
import java.io.Serializable;
// all tiles must be squares // all tiles must be squares
public class Tile { public class Tile implements Serializable {
public int x; public int x;
public int y; public int y;
@ -34,11 +35,11 @@ public class Tile {
previousBlock = null; previousBlock = null;
} }
public void update(){ public void update(){
realX = x-GamePanel.camera.x; realX = x-GameFrame.game.camera.x;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.setColor(Color.black); g.setColor(Color.black);
g.fillRect(x-GamePanel.camera.x, y, length, length); g.fillRect(x-GameFrame.game.camera.x, y, length, length);
} }
} }