Make serialization to file efficient
parent
6a2d2361c7
commit
02ef7f3292
|
@ -7,7 +7,10 @@ import java.io.*;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
public class BufferedImageWrapper implements Serializable {
|
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) {
|
public BufferedImageWrapper(int width, int height, int imageType) {
|
||||||
image = new BufferedImage(width, height, imageType);
|
image = new BufferedImage(width, height, imageType);
|
||||||
}
|
}
|
||||||
|
@ -24,15 +27,44 @@ public class BufferedImageWrapper implements Serializable {
|
||||||
this.image = image;
|
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() {}
|
public BufferedImageWrapper() {}
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private void writeObject(ObjectOutputStream out) throws IOException {
|
private void writeObject(ObjectOutputStream out) throws IOException {
|
||||||
ImageIO.write(image, "png", out); // png is lossless
|
out.writeObject(flipImage);
|
||||||
|
if (imageString != null) {
|
||||||
|
out.writeObject(imageString);
|
||||||
|
} else {
|
||||||
|
ImageIO.write(image, "png", out); // png is lossless
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||||
image = ImageIO.read(in);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,14 +58,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
public Camera camera;
|
public Camera camera;
|
||||||
|
|
||||||
// image imports begin here
|
// image imports begin here
|
||||||
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(getImage("img/backgrounds/pointyMountains.png"));
|
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(("img/backgrounds/pointyMountains.png"));
|
||||||
public BufferedImageWrapper box = new BufferedImageWrapper(getImage("img/tiles/boxes/box.png"));
|
public BufferedImageWrapper box = new BufferedImageWrapper(("img/tiles/boxes/box.png"));
|
||||||
public BufferedImageWrapper boxCoin = new BufferedImageWrapper(getImage("img/tiles/boxes/boxCoin.png"));
|
public BufferedImageWrapper boxCoin = new BufferedImageWrapper(("img/tiles/boxes/boxCoin.png"));
|
||||||
public BufferedImageWrapper cloud1 = new BufferedImageWrapper(getImage("img/backgrounds/cloud1.png"));
|
public BufferedImageWrapper cloud1 = new BufferedImageWrapper(("img/backgrounds/cloud1.png"));
|
||||||
public BufferedImageWrapper cloud2 = new BufferedImageWrapper(getImage("img/backgrounds/cloud2.png"));
|
public BufferedImageWrapper cloud2 = new BufferedImageWrapper(("img/backgrounds/cloud2.png"));
|
||||||
public BufferedImageWrapper cloud3 = new BufferedImageWrapper(getImage("img/backgrounds/cloud3.png"));
|
public BufferedImageWrapper cloud3 = new BufferedImageWrapper(("img/backgrounds/cloud3.png"));
|
||||||
public BufferedImageWrapper bomb;
|
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 {
|
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
|
||||||
|
@ -80,24 +80,24 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
try {
|
try {
|
||||||
// load player sprites from disk here
|
// load player sprites from disk here
|
||||||
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')));
|
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][0][i] = new BufferedImageWrapper(sprite);
|
||||||
playerSpriteArray[1][1][i] = new BufferedImageWrapper(sprite);
|
playerSpriteArray[1][1][i] = new BufferedImageWrapper(sprite);
|
||||||
playerSpriteArray[0][0][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
|
playerSpriteArray[0][0][i] = new BufferedImageWrapper(sprite, true);
|
||||||
playerSpriteArray[0][1][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
|
playerSpriteArray[0][1][i] = new BufferedImageWrapper(sprite, true);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 9; i++) {
|
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
|
// 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
|
||||||
BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk1.png")),
|
BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(("img/enemy/slime/slimeWalk1.png")),
|
||||||
new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk2.png")),
|
new BufferedImageWrapper(("img/enemy/slime/slimeWalk2.png")),
|
||||||
new BufferedImageWrapper(getImage("img/enemy/slime/slimeDead.png"))};
|
new BufferedImageWrapper(("img/enemy/slime/slimeDead.png"))};
|
||||||
BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk1.png"))),
|
BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper((("img/enemy/slime/slimeWalk1.png")), true),
|
||||||
new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk2.png"))),
|
new BufferedImageWrapper(("img/enemy/slime/slimeWalk2.png"), true),
|
||||||
new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeDead.png")))};
|
new BufferedImageWrapper(("img/enemy/slime/slimeDead.png"), true)};
|
||||||
// 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;
|
||||||
|
@ -105,7 +105,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
slimeSpriteArray[1][1] = flippedTemporarySlimeArray;
|
slimeSpriteArray[1][1] = flippedTemporarySlimeArray;
|
||||||
|
|
||||||
// load bomb sprites
|
// load bomb sprites
|
||||||
bomb = new BufferedImageWrapper(getImage("img/misc/bomb.png"));
|
bomb = new BufferedImageWrapper(("img/misc/bomb.png"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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
|
// increment sprite image to be used and keeps it below 12
|
||||||
playerFrame = (playerFrame + 1) % 11;
|
playerFrame = (playerFrame + 1) % 11;
|
||||||
playerFrameCounter -= 5;
|
playerFrameCounter -= 5;
|
||||||
// // if the player has moved enough to justify a frame change, a new save will also be made
|
// if the player has moved enough to justify a frame change, a new save will also be made
|
||||||
// try {
|
try {
|
||||||
// FileManager.writeObjectToFile("local/game_state", new ArrayList()); // this is placeholder, replace with this
|
FileManager.writeObjectToFile("local/game_state", this); // this is placeholder, replace with this
|
||||||
// } catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// e.printStackTrace();
|
e.printStackTrace();
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repaint();
|
repaint();
|
||||||
|
|
|
@ -103,6 +103,6 @@ public class MapReader implements Serializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void newTile(String filePath) throws IOException, SpriteException {
|
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))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 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 {
|
public MenuPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
|
||||||
this.gameFrame = gameFrame;
|
this.gameFrame = gameFrame;
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class Particle extends GenericSprite implements Serializable {
|
||||||
super(x,y,length, length);
|
super(x,y,length, length);
|
||||||
this.xVelocity = xVelocity;
|
this.xVelocity = xVelocity;
|
||||||
this.yVelocity = yVelocity;
|
this.yVelocity = yVelocity;
|
||||||
sprite = new BufferedImageWrapper(GamePanel.getImage(filePath));
|
sprite = new BufferedImageWrapper((filePath));
|
||||||
}
|
}
|
||||||
public void move(){
|
public void move(){
|
||||||
x+=xVelocity;
|
x+=xVelocity;
|
||||||
|
|
|
@ -279,7 +279,7 @@ public class Player extends GenericSprite {
|
||||||
}
|
}
|
||||||
} else if((GameFrame.game.map[x][y] == null||GameFrame.game.map[x][y].replaceAble)&&canPlaceSteel){
|
} else if((GameFrame.game.map[x][y] == null||GameFrame.game.map[x][y].replaceAble)&&canPlaceSteel){
|
||||||
Tile temp = GameFrame.game.map[x][y];
|
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].movable = true;
|
||||||
GameFrame.game.map[x][y].previousBlock = temp;
|
GameFrame.game.map[x][y].previousBlock = temp;
|
||||||
holdingSteel = false;
|
holdingSteel = false;
|
||||||
|
|
Loading…
Reference in New Issue