aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-05-24 21:31:11 +0200
committerPacien TRAN-GIRARD2014-05-24 21:31:11 +0200
commitd06c60a6ccc4d97df62b19e91f18d4aa292d9c57 (patch)
treed4a8407181216c2add2bd52cf3e48f93fd5108f5 /src
parentd2f160a26bc9899c2646857ae321453346603391 (diff)
downloadesieequest-d06c60a6ccc4d97df62b19e91f18d4aa292d9c57.tar.gz
Better load/save
Diffstat (limited to 'src')
-rw-r--r--src/esieequest/controller/commands/Command.java1
-rw-r--r--src/esieequest/controller/commands/LoadCommand.java17
-rw-r--r--src/esieequest/controller/commands/SaveCommand.java11
-rw-r--r--src/esieequest/view/Viewable.java25
-rw-r--r--src/esieequest/view/app/UserInterface.java63
-rw-r--r--src/esieequest/view/text/TextInterface.java34
-rw-r--r--src/esieequest/view/web/WebInterface.java41
7 files changed, 180 insertions, 12 deletions
diff --git a/src/esieequest/controller/commands/Command.java b/src/esieequest/controller/commands/Command.java
index a29b79b..906a312 100644
--- a/src/esieequest/controller/commands/Command.java
+++ b/src/esieequest/controller/commands/Command.java
@@ -24,6 +24,7 @@ public enum Command {
24 QUIT(new QuitCommand()), 24 QUIT(new QuitCommand()),
25 HELP(new HelpCommand()), 25 HELP(new HelpCommand()),
26 ALEA(new AleaCommand()), 26 ALEA(new AleaCommand()),
27 SOUND(new NewCommand()),
27 28
28 // map related 29 // map related
29 GO(new GoCommand()), 30 GO(new GoCommand()),
diff --git a/src/esieequest/controller/commands/LoadCommand.java b/src/esieequest/controller/commands/LoadCommand.java
index 896d85b..26e18e2 100644
--- a/src/esieequest/controller/commands/LoadCommand.java
+++ b/src/esieequest/controller/commands/LoadCommand.java
@@ -18,7 +18,16 @@ public class LoadCommand implements Executable {
18 @Override 18 @Override
19 public void execute(final String argument, final Game game, final Viewable view) { 19 public void execute(final String argument, final Game game, final Viewable view) {
20 20
21 final String jsonString = argument; 21 if (argument == null) {
22 return;
23 }
24
25 final String jsonString;
26 if (argument.startsWith("{")) {
27 jsonString = argument;
28 } else {
29 jsonString = view.load(argument);
30 }
22 31
23 JSONObject datas = new JSONObject(); 32 JSONObject datas = new JSONObject();
24 try { 33 try {
@@ -39,12 +48,12 @@ public class LoadCommand implements Executable {
39 return; 48 return;
40 } 49 }
41 50
42 // view.updateQuest(); 51 view.enableInput();
43 // view.updateInventory(game.getPlayer().getItems()); 52 view.updateQuest(game.getPlayer().getCurrentQuest());
53 view.updateInventory(game.getPlayer().getInventory());
44 view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer() 54 view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer()
45 .getCurrentDirection(), game.getPlayer().getCurrentSide(), game.getPlayer() 55 .getCurrentDirection(), game.getPlayer().getCurrentSide(), game.getPlayer()
46 .canGoBack()); 56 .canGoBack());
47 57
48 } 58 }
49
50} 59}
diff --git a/src/esieequest/controller/commands/SaveCommand.java b/src/esieequest/controller/commands/SaveCommand.java
index 9e32762..3332059 100644
--- a/src/esieequest/controller/commands/SaveCommand.java
+++ b/src/esieequest/controller/commands/SaveCommand.java
@@ -13,7 +13,16 @@ public class SaveCommand implements Executable {
13 @Override 13 @Override
14 public void execute(final String argument, final Game game, final Viewable view) { 14 public void execute(final String argument, final Game game, final Viewable view) {
15 15
16 view.echo(game.serialise().toJSONString()); 16 if (argument == null) {
17 return;
18 }
19
20 if (argument.isEmpty()) {
21 return;
22 }
23
24 final String serialisedGame = game.serialise().toJSONString();
25 view.save(argument, serialisedGame);
17 26
18 } 27 }
19 28
diff --git a/src/esieequest/view/Viewable.java b/src/esieequest/view/Viewable.java
index fa53e1a..c650759 100644
--- a/src/esieequest/view/Viewable.java
+++ b/src/esieequest/view/Viewable.java
@@ -88,4 +88,29 @@ public interface Viewable {
88 */ 88 */
89 public void stopMusic(); 89 public void stopMusic();
90 90
91 /**
92 * Toggles the sound.
93 */
94 public void toggleSound();
95
96 /**
97 * Loads a saved game file.
98 *
99 * @param path
100 * the path of the file
101 *
102 * @return the serialised Game
103 */
104 public String load(final String path);
105
106 /**
107 * Saves a game as a file.
108 *
109 * @param path
110 * the path of the file
111 * @param serialisedGame
112 * the serialised Game
113 */
114 public void save(final String path, final String serialisedGame);
115
91} 116}
diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java
index 0f426bd..b00673c 100644
--- a/src/esieequest/view/app/UserInterface.java
+++ b/src/esieequest/view/app/UserInterface.java
@@ -12,8 +12,12 @@ import java.awt.event.FocusEvent;
12import java.awt.event.FocusListener; 12import java.awt.event.FocusListener;
13import java.awt.event.KeyEvent; 13import java.awt.event.KeyEvent;
14import java.awt.image.BufferedImage; 14import java.awt.image.BufferedImage;
15import java.io.FileWriter;
15import java.io.IOException; 16import java.io.IOException;
16import java.net.URL; 17import java.net.URL;
18import java.nio.file.Files;
19import java.nio.file.Path;
20import java.nio.file.Paths;
17import java.util.HashMap; 21import java.util.HashMap;
18import java.util.Map.Entry; 22import java.util.Map.Entry;
19import java.util.Timer; 23import java.util.Timer;
@@ -22,12 +26,14 @@ import java.util.TimerTask;
22import javax.swing.AbstractAction; 26import javax.swing.AbstractAction;
23import javax.swing.JButton; 27import javax.swing.JButton;
24import javax.swing.JComponent; 28import javax.swing.JComponent;
29import javax.swing.JFileChooser;
25import javax.swing.JLabel; 30import javax.swing.JLabel;
26import javax.swing.JPanel; 31import javax.swing.JPanel;
27import javax.swing.JTextField; 32import javax.swing.JTextField;
28import javax.swing.JTextPane; 33import javax.swing.JTextPane;
29import javax.swing.KeyStroke; 34import javax.swing.KeyStroke;
30import javax.swing.border.EmptyBorder; 35import javax.swing.border.EmptyBorder;
36import javax.swing.filechooser.FileNameExtensionFilter;
31 37
32import lombok.Getter; 38import lombok.Getter;
33 39
@@ -62,6 +68,9 @@ abstract class UserInterface implements Viewable, ActionListener {
62 private static final String SOUND_DIR = "res/snd/"; 68 private static final String SOUND_DIR = "res/snd/";
63 private static final String SOUND_EXT = ".ogg"; 69 private static final String SOUND_EXT = ".ogg";
64 70
71 private static final String SAVE_LABEL = "ESIEEquest Game";
72 private static final String SAVE_EXT = ".eqg";
73
65 private GameEngine gameEngine; 74 private GameEngine gameEngine;
66 75
67 @Getter 76 @Getter
@@ -154,6 +163,7 @@ abstract class UserInterface implements Viewable, ActionListener {
154 163
155 this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString()); 164 this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString());
156 this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString()); 165 this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString());
166 this.newButton.setActionCommand(Command.SOUND.name());
157 this.gamePanel.add(this.soundButton); 167 this.gamePanel.add(this.soundButton);
158 168
159 this.filePanel = new JPanel(); 169 this.filePanel = new JPanel();
@@ -232,6 +242,7 @@ abstract class UserInterface implements Viewable, ActionListener {
232 242
233 this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString()); 243 this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString());
234 this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString()); 244 this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString());
245 this.inventoryButton.setActionCommand(Command.INVENTORY.name());
235 this.inventoryButton.setFont(font); 246 this.inventoryButton.setFont(font);
236 this.inventoryButton.setPreferredSize(squareButton); 247 this.inventoryButton.setPreferredSize(squareButton);
237 this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST); 248 this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST);
@@ -283,6 +294,7 @@ abstract class UserInterface implements Viewable, ActionListener {
283 this.newButton.addActionListener(actionListener); 294 this.newButton.addActionListener(actionListener);
284 this.soundButton.addActionListener(actionListener); 295 this.soundButton.addActionListener(actionListener);
285 this.loadButton.addActionListener(actionListener); 296 this.loadButton.addActionListener(actionListener);
297 this.saveButton.addActionListener(actionListener);
286 this.forwardButton.addActionListener(actionListener); 298 this.forwardButton.addActionListener(actionListener);
287 this.inventoryButton.addActionListener(actionListener); 299 this.inventoryButton.addActionListener(actionListener);
288 this.actionButton.addActionListener(actionListener); 300 this.actionButton.addActionListener(actionListener);
@@ -535,10 +547,31 @@ abstract class UserInterface implements Viewable, ActionListener {
535 */ 547 */
536 @Override 548 @Override
537 public void actionPerformed(final ActionEvent actionEvent) { 549 public void actionPerformed(final ActionEvent actionEvent) {
538 if (actionEvent.getActionCommand() == Text.INVENTORY_BUTTON.toString()) { 550 if (actionEvent.getActionCommand() == Command.INVENTORY.name()) {
539 this.toggleInventory(); 551 this.toggleInventory();
540 } else if (actionEvent.getActionCommand() == Text.TOGGLE_SOUND_BUTTON.toString()) { 552 } else if (actionEvent.getActionCommand() == Text.TOGGLE_SOUND_BUTTON.toString()) {
541 this.toggleAudio(); 553 this.toggleAudio();