I have built a tile based level editor in java, It is just about completed, but as I was writing the method to save the level, that all of my tiles were image sprites and I dont know of a way to save them so that the actual game can read them without making each tile it's own image file which I would then need to import to read in the real game. (there are over 700 possible tiles, a level would probably use 100 of those);
My question to you all: what is the best way to save all these tiles? I am familiar with saving things to a text file but I am not able to save these images in the same way. Is there a way to save the level so that my text based information is saved in the same place as my tile sprites?
here is my tile class and what I have so far in terms of my save method
public class Tile {
int x, y, w, h, type;
BufferedImage layer1Image;
BufferedImage layer2Image;
BufferedImage layer3Image;
BufferedImage layer4Image;
boolean walkable;
public Tile(int x, int y, int w, int h) {
walkable = false;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
type = 0;
layer1Image = null;
layer2Image = null;
layer3Image = null;
layer4Image = null;
}
public Tile() {
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getW() {
return w;
}
public int getH() {
return h;
}
public void setX(int newX) {
x = newX;
}
public void setY(int newY) {
y = newY;
}
public void setW(int newW) {
w = newW;
}
public void setH(int newH) {
h = newH;
}
public boolean mouseOver(int mouseX, int mouseY) {
if (mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h) {
return true;
}
return false;
}
public int getType() {
return type;
}
public boolean getWalkable() {
return walkable;
}
public BufferedImage getLayer1Image() {
return layer1Image;
}
public BufferedImage getLayer2Image() {
return layer2Image;
}
public BufferedImage getLayer3Image() {
return layer3Image;
}
public BufferedImage getLayer4Image() {
return layer4Image;
}
public void clearImages() {
layer1Image = null;
layer2Image = null;
layer3Image = null;
layer4Image = null;
walkable = true;
}
public void setImage(BufferedImage image, int layer) {
switch (layer) {
case 0:
layer1Image = image;
break;
case 1:
layer2Image = image;
break;
case 2:
layer3Image = image;
case 3:
layer4Image = image;
}
}
public void display(Graphics g, int x, int y, int w, int h, int zoom) {
g.setColor(Color.BLACK);
g.drawImage(layer1Image, x, y, w, h, null);
g.drawImage(layer2Image, x, y, w, h, null);
g.drawImage(layer3Image, x, y, w, h, null);
g.drawImage(layer4Image, x, y, w, h, null);
g.drawRect(x, y, w, h);
}
public void getAttributes(Tile tile) {
layer1Image = tile.getLayer1Image();
layer2Image = tile.getLayer2Image();
layer3Image = tile.getLayer3Image();
layer4Image = tile.getLayer4Image();
x = tile.getX();
y = tile.getY();
w = tile.getW();
h = tile.getH();
walkable = tile.getWalkable();
type = tile.getType();
}
public void setWalkable(boolean b) {
walkable = b;
}
// Here is the save, its in a different class in case my formatting in the post is screwy
public void doSaveAsText() {
if (fileDialog == null) {
fileDialog = new JFileChooser();
}
File selectedFile;
if (editFile == null) {
selectedFile = new File("levelData.txt");
} else {
selectedFile = new File(editFile.getName(开发者_StackOverflow));
}
fileDialog.setSelectedFile(selectedFile);
fileDialog.setDialogTitle("Select File to be Saved");
int option = fileDialog.showSaveDialog(this);
if (option != JFileChooser.APPROVE_OPTION) {
return;
}
selectedFile = fileDialog.getSelectedFile();
if (selectedFile.exists()) {
int response = JOptionPane.showConfirmDialog(this, "Already exists, overwrite?", "Confirm Save", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION) {
return;
}
}
PrintWriter out;
try{
FileWriter stream = new FileWriter(selectedFile);
out = new PrintWriter(stream);
} catch (Exception e){
JOptionPane.showMessageDialog(this, "error");
return;
}
try{
out.println("LevelEditor v1.0");
for(int i = 0; i < mapPanel.tilesList.size(); i++){
Tile currentTile = mapPanel.tilesList.get(i);
out.println("startTile");
out.println("" + currentTile.getX());
out.println("" + currentTile.getY());
out.println("" + currentTile.getW());
out.println("" + currentTile.getH());
out.println("" + currentTile.getWalkable());
out.println("endTile");
}
out.close();
}catch(Exception e){
System.exit(0);
}
}
Any suggestions would be great
You could just save the whole higher in hierarchy Java objects to an output stream, and load them at a later stage.
See this example.
check out the serialization API, or just extract the bytes and make hexadecimal strings from them.
I'm not sure if you're more concerned about the fact that your tile images are separate from your text information, or the fact that each tile image will be in a separate file from the other tiles.
For the first problem, you may be able to store your textual date in image metadata (eg: a comment). Then your tiles would just be images files.
If you're more concerned about the fact that each tile image will be in a separate file from the other tiles then you could create a single image that consists of all of your tile images in a grid (or something similar). You then have a single image file that you can also edit in standard image editing software if you desire.
How about encapulating the BufferedImage
into a TileImage
class that holds a reference to a particular image, then you can output the image reference in your save file. That way you don't have to save the image data over and over again, and updated graphics will be available to saved games.
精彩评论