Make serialization to file efficient

master
John 2022-06-15 22:08:35 -04:00
parent 6a2d2361c7
commit 02ef7f3292
6 changed files with 63 additions and 31 deletions

View File

@ -7,7 +7,10 @@ import java.io.*;
import java.util.Hashtable;
public class BufferedImageWrapper implements Serializable {
public BufferedImage image;
transient public BufferedImage image;
public String imageString;
public Boolean flipImage = false;
public BufferedImageWrapper(int width, int height, int imageType) {
image = new BufferedImage(width, height, imageType);
}
@ -24,15 +27,44 @@ public class BufferedImageWrapper implements Serializable {
this.image = image;
}
public BufferedImageWrapper(String imageString) throws IOException {
image = GamePanel.getImage(imageString);
this.imageString = imageString;
}
public BufferedImageWrapper(String imageString, boolean flip) throws IOException {
BufferedImage temporaryImage = GamePanel.getImage(imageString);
if (flip) {
image = GamePanel.flipImageHorizontally(temporaryImage);
} else {
image = temporaryImage;
}
flipImage = flip;
this.imageString = imageString;
}
public BufferedImageWrapper() {}
@Serial
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(flipImage);
if (imageString != null) {
out.writeObject(imageString);
} else {
ImageIO.write(image, "png", out); // png is lossless
}
}
@Serial
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
Object o;
flipImage = (Boolean)in.readObject();
o = in.readObject();
if (o instanceof String) {
image = GamePanel.getImage((String)o);
this.imageString = (String)o;
} else {
image = ImageIO.read(in);
}
}
}

View File

@ -58,14 +58,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
public Camera camera;
// image imports begin here
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 backgroundImage = new BufferedImageWrapper(("img/backgrounds/pointyMountains.png"));
public BufferedImageWrapper box = new BufferedImageWrapper(("img/tiles/boxes/box.png"));
public BufferedImageWrapper boxCoin = new BufferedImageWrapper(("img/tiles/boxes/boxCoin.png"));
public BufferedImageWrapper cloud1 = new BufferedImageWrapper(("img/backgrounds/cloud1.png"));
public BufferedImageWrapper cloud2 = new BufferedImageWrapper(("img/backgrounds/cloud2.png"));
public BufferedImageWrapper cloud3 = new BufferedImageWrapper(("img/backgrounds/cloud3.png"));
public BufferedImageWrapper bomb;
public BufferedImageWrapper onePortrait = new BufferedImageWrapper(getImage("img/dialogue/Gunther.png"));
public BufferedImageWrapper onePortrait = new BufferedImageWrapper(("img/dialogue/Gunther.png"));
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
@ -80,24 +80,24 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
try {
// load player sprites from disk here
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')));
String sprite = (String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));
playerSpriteArray[1][0][i] = new BufferedImageWrapper(sprite);
playerSpriteArray[1][1][i] = new BufferedImageWrapper(sprite);
playerSpriteArray[0][0][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
playerSpriteArray[0][1][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
playerSpriteArray[0][0][i] = new BufferedImageWrapper(sprite, true);
playerSpriteArray[0][1][i] = new BufferedImageWrapper(sprite, true);
}
for (int i = 0; i < 9; i++) {
explosionArray[i] = new BufferedImageWrapper(getImage("img/misc/bomb/sonicExplosion0" + i + ".png"));
explosionArray[i] = new BufferedImageWrapper(("img/misc/bomb/sonicExplosion0" + i + ".png"));
}
// load slime sprites from disk here
// these variables were not defined above because they are temporary variables
BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk1.png")),
new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk2.png")),
new BufferedImageWrapper(getImage("img/enemy/slime/slimeDead.png"))};
BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk1.png"))),
new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk2.png"))),
new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeDead.png")))};
BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(("img/enemy/slime/slimeWalk1.png")),
new BufferedImageWrapper(("img/enemy/slime/slimeWalk2.png")),
new BufferedImageWrapper(("img/enemy/slime/slimeDead.png"))};
BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper((("img/enemy/slime/slimeWalk1.png")), true),
new BufferedImageWrapper(("img/enemy/slime/slimeWalk2.png"), true),
new BufferedImageWrapper(("img/enemy/slime/slimeDead.png"), true)};
// please note that these sprites are reversed compared to the player sprites
slimeSpriteArray[0][0] = temporarySlimeArray;
slimeSpriteArray[0][1] = temporarySlimeArray;
@ -105,7 +105,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
slimeSpriteArray[1][1] = flippedTemporarySlimeArray;
// load bomb sprites
bomb = new BufferedImageWrapper(getImage("img/misc/bomb.png"));
bomb = new BufferedImageWrapper(("img/misc/bomb.png"));
} catch (IOException e) {
e.printStackTrace();
}
@ -350,12 +350,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
// increment sprite image to be used and keeps it below 12
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", new ArrayList()); // this is placeholder, replace with this
// } catch (IOException e) {
// e.printStackTrace();
// }
// 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); // this is placeholder, replace with this
} catch (IOException e) {
e.printStackTrace();
}
}
}
repaint();

View File

@ -103,6 +103,6 @@ public class MapReader implements Serializable {
}
}
public static void newTile(String filePath) throws IOException, SpriteException {
GameFrame.game.map[x][y]=(new SingleTile(TileX,TileY, new BufferedImageWrapper(GamePanel.getImage(filePath))));
GameFrame.game.map[x][y]=(new SingleTile(TileX,TileY, new BufferedImageWrapper((filePath))));
}
}

View File

@ -39,7 +39,7 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
public static boolean gameStart = false;
// image imports begin here
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(GamePanel.getImage("img/backgrounds/pointyMountains.png"));
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(("img/backgrounds/pointyMountains.png"));
public MenuPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
this.gameFrame = gameFrame;

View File

@ -19,7 +19,7 @@ public class Particle extends GenericSprite implements Serializable {
super(x,y,length, length);
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
sprite = new BufferedImageWrapper(GamePanel.getImage(filePath));
sprite = new BufferedImageWrapper((filePath));
}
public void move(){
x+=xVelocity;

View File

@ -279,7 +279,7 @@ public class Player extends GenericSprite {
}
} else if((GameFrame.game.map[x][y] == null||GameFrame.game.map[x][y].replaceAble)&&canPlaceSteel){
Tile temp = GameFrame.game.map[x][y];
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")));
GameFrame.game.map[x][y] = new SingleTile(x*Tile.length - (GamePanel.GAME_WIDTH /2), y*Tile.length, new BufferedImageWrapper(("img/tiles/boxes/steel.png")));
GameFrame.game.map[x][y].movable = true;
GameFrame.game.map[x][y].previousBlock = temp;
holdingSteel = false;