Fixed save bug

This commit is contained in:
Chris Cromer 2016-02-27 16:59:49 -03:00
parent 6215e8a7ab
commit 6362287240
3 changed files with 53 additions and 15 deletions

View File

@ -281,11 +281,11 @@ public class DQ extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Save Level")) {
FileDialog fd = new FileDialog(this, "Save Level", FileDialog.SAVE);
fd.setDirectory(System.getProperty("user.home") + "/.DroidQuest/" + "ROlevels");
fd.setDirectory(System.getProperty("user.home") + "/.DroidQuest/" + "Saves");
fd.show();
System.out.println("Dialog returned with "
+ fd.getDirectory()
+ fd.getFile());
+ fd.getDirectory()
+ fd.getFile());
if (fd.getFile() != null) {
myRoom.SaveLevel(fd.getDirectory() + fd.getFile());
}

View File

@ -21,7 +21,7 @@ import java.lang.reflect.InvocationTargetException;
public class RoomDisplay extends JPanel {
public final DQ dq;
Level level;
public Level level;
public Timer timer;
private int timerspeed = 128;
public boolean useSounds = true;
@ -36,6 +36,8 @@ public class RoomDisplay extends JPanel {
return (true);
}
public RoomDisplay rd;
public RoomDisplay(final DQ dq) {
this.dq = dq;
setSize(new Dimension(560, 384));
@ -131,7 +133,7 @@ public class RoomDisplay extends JPanel {
}
catch (FileNotFoundException ie) {
// filename does not exist
RoomDisplay rd = level.roomdisplay;
rd = level.roomdisplay;
String classname = "com.droidquest.levels." + filename.substring(0, filename.length() - 4);
Constructor constructor = null;
try {
@ -443,16 +445,45 @@ public class RoomDisplay extends JPanel {
public void SaveLevel(String filename) {
System.out.println("Saving level " + filename);
String[] filenames = filename.split("/");
if (filenames.length > 1) {
filename = filenames[filenames.length - 1];
}
try {
FileOutputStream out = new FileOutputStream(System.getProperty("user.home") + "/.DroidQuest/" + "ROlevels/" + filename);
FileOutputStream out = new FileOutputStream(System.getProperty("user.home") + "/.DroidQuest/Saves/" + filename);
ObjectOutputStream s = new ObjectOutputStream(out);
level.writeObject(s);
s.flush();
s.close();
out.close();
}
catch (FileNotFoundException e) {
System.out.println("File Not Found");
catch (FileNotFoundException e) {
System.out.println("File Not Found" + filename);
return;
}
catch (IOException e) {
System.out.println("IO Exception");
System.out.println(e.getMessage());
}
}
public void SaveLevelAuto(String filename) {
System.out.println("Saving level " + filename);
String[] filenames = filename.split("/");
if (filenames.length > 1) {
filename = filenames[filenames.length - 1];
}
try {
File file = new File(System.getProperty("user.home") + "/.DroidQuest/" + filename);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(out);
level.writeObject(s);
s.flush();
s.close();
out.close();
}
catch (IOException e) {
System.out.println("IO Exception");
@ -468,16 +499,23 @@ public class RoomDisplay extends JPanel {
Room.level = level;
Material.level = level;
String[] split = filename.split("/");
if (split.length == 1) {
filename = System.getProperty("user.home") + "/.DroidQuest/Saves/" + filename;
}
System.out.println("Loading level " + filename);
// Add flags for loading Object inventories or running Init()
try {
FileInputStream in = new FileInputStream(System.getProperty("user.home") + "/.DroidQuest/" + "ROlevels/" + filename);
FileInputStream in = new FileInputStream(filename);
ObjectInputStream s = new ObjectInputStream(in);
level.readObject(s);
s.close();
in.close();
}
catch (FileNotFoundException e) {
System.out.println("File Not Found");
System.out.println("File Not Found" + filename);
return;
}
catch (IOException e) {

View File

@ -267,11 +267,11 @@ public class MainMenu extends Level {
player = gameCursor;
currentViewer = player;
File f = new File(System.getProperty("user.home") + "/.DroidQuest/ROlevels/");
if (!f.exists()) {
f.mkdirs();
File file = new File(System.getProperty("user.home") + "/.DroidQuest/Saves/");
if (!file.exists()) {
file.mkdirs();
}
String[] files = f.list();
String[] files = file.list();
int pageIndex = 5;
for (int a = 0; a < files.length; a++) {
if (a > 4 && a % 5 == 0) {
@ -301,7 +301,7 @@ public class MainMenu extends Level {
LinkRoomsUpDown(pageIndex, newPageIndex);
pageIndex = newPageIndex;
}
materials.addElement(new Portal(files[a], false, false));
materials.addElement(new Portal(System.getProperty("user.home") + "/.DroidQuest/Saves/" + files[a], false, false));
int matIndex = materials.size() - 1;
int y = 1 + (a % 5) * 2;
Room room = rooms.elementAt(pageIndex);