Skip to content

Commit 6362287

Browse files
committed
Fixed save bug
1 parent 6215e8a commit 6362287

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

src/com/droidquest/DQ.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ public void setRadioSelected(boolean selected) {
281281
public void actionPerformed(ActionEvent e) {
282282
if (e.getActionCommand().equals("Save Level")) {
283283
FileDialog fd = new FileDialog(this, "Save Level", FileDialog.SAVE);
284-
fd.setDirectory(System.getProperty("user.home") + "/.DroidQuest/" + "ROlevels");
284+
fd.setDirectory(System.getProperty("user.home") + "/.DroidQuest/" + "Saves");
285285
fd.show();
286286
System.out.println("Dialog returned with "
287-
+ fd.getDirectory()
288-
+ fd.getFile());
287+
+ fd.getDirectory()
288+
+ fd.getFile());
289289
if (fd.getFile() != null) {
290290
myRoom.SaveLevel(fd.getDirectory() + fd.getFile());
291291
}

src/com/droidquest/RoomDisplay.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
public class RoomDisplay extends JPanel {
2323
public final DQ dq;
24-
Level level;
24+
public Level level;
2525
public Timer timer;
2626
private int timerspeed = 128;
2727
public boolean useSounds = true;
@@ -36,6 +36,8 @@ public boolean isFocusable() {
3636
return (true);
3737
}
3838

39+
public RoomDisplay rd;
40+
3941
public RoomDisplay(final DQ dq) {
4042
this.dq = dq;
4143
setSize(new Dimension(560, 384));
@@ -131,7 +133,7 @@ public void actionPerformed(ActionEvent e) {
131133
}
132134
catch (FileNotFoundException ie) {
133135
// filename does not exist
134-
RoomDisplay rd = level.roomdisplay;
136+
rd = level.roomdisplay;
135137
String classname = "com.droidquest.levels." + filename.substring(0, filename.length() - 4);
136138
Constructor constructor = null;
137139
try {
@@ -443,16 +445,45 @@ void SaveLevel() {
443445

444446
public void SaveLevel(String filename) {
445447
System.out.println("Saving level " + filename);
448+
String[] filenames = filename.split("/");
449+
if (filenames.length > 1) {
450+
filename = filenames[filenames.length - 1];
451+
}
446452
try {
447-
FileOutputStream out = new FileOutputStream(System.getProperty("user.home") + "/.DroidQuest/" + "ROlevels/" + filename);
453+
FileOutputStream out = new FileOutputStream(System.getProperty("user.home") + "/.DroidQuest/Saves/" + filename);
448454
ObjectOutputStream s = new ObjectOutputStream(out);
449455
level.writeObject(s);
450456
s.flush();
451457
s.close();
452458
out.close();
453459
}
454-
catch (FileNotFoundException e) {
455-
System.out.println("File Not Found");
460+
catch (FileNotFoundException e) {
461+
System.out.println("File Not Found" + filename);
462+
return;
463+
}
464+
catch (IOException e) {
465+
System.out.println("IO Exception");
466+
System.out.println(e.getMessage());
467+
}
468+
}
469+
470+
public void SaveLevelAuto(String filename) {
471+
System.out.println("Saving level " + filename);
472+
String[] filenames = filename.split("/");
473+
if (filenames.length > 1) {
474+
filename = filenames[filenames.length - 1];
475+
}
476+
try {
477+
File file = new File(System.getProperty("user.home") + "/.DroidQuest/" + filename);
478+
if (!file.exists()) {
479+
file.createNewFile();
480+
}
481+
FileOutputStream out = new FileOutputStream(file);
482+
ObjectOutputStream s = new ObjectOutputStream(out);
483+
level.writeObject(s);
484+
s.flush();
485+
s.close();
486+
out.close();
456487
}
457488
catch (IOException e) {
458489
System.out.println("IO Exception");
@@ -468,16 +499,23 @@ void LoadLevel(String filename) {
468499
Room.level = level;
469500
Material.level = level;
470501

502+
String[] split = filename.split("/");
503+
if (split.length == 1) {
504+
filename = System.getProperty("user.home") + "/.DroidQuest/Saves/" + filename;
505+
}
506+
507+
System.out.println("Loading level " + filename);
508+
471509
// Add flags for loading Object inventories or running Init()
472510
try {
473-
FileInputStream in = new FileInputStream(System.getProperty("user.home") + "/.DroidQuest/" + "ROlevels/" + filename);
511+
FileInputStream in = new FileInputStream(filename);
474512
ObjectInputStream s = new ObjectInputStream(in);
475513
level.readObject(s);
476514
s.close();
477515
in.close();
478516
}
479517
catch (FileNotFoundException e) {
480-
System.out.println("File Not Found");
518+
System.out.println("File Not Found" + filename);
481519
return;
482520
}
483521
catch (IOException e) {

src/com/droidquest/levels/MainMenu.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ public MainMenu(RoomDisplay rd) {
267267
player = gameCursor;
268268
currentViewer = player;
269269

270-
File f = new File(System.getProperty("user.home") + "/.DroidQuest/ROlevels/");
271-
if (!f.exists()) {
272-
f.mkdirs();
270+
File file = new File(System.getProperty("user.home") + "/.DroidQuest/Saves/");
271+
if (!file.exists()) {
272+
file.mkdirs();
273273
}
274-
String[] files = f.list();
274+
String[] files = file.list();
275275
int pageIndex = 5;
276276
for (int a = 0; a < files.length; a++) {
277277
if (a > 4 && a % 5 == 0) {
@@ -301,7 +301,7 @@ public MainMenu(RoomDisplay rd) {
301301
LinkRoomsUpDown(pageIndex, newPageIndex);
302302
pageIndex = newPageIndex;
303303
}
304-
materials.addElement(new Portal(files[a], false, false));
304+
materials.addElement(new Portal(System.getProperty("user.home") + "/.DroidQuest/Saves/" + files[a], false, false));
305305
int matIndex = materials.size() - 1;
306306
int y = 1 + (a % 5) * 2;
307307
Room room = rooms.elementAt(pageIndex);

0 commit comments

Comments
 (0)