summaryrefslogtreecommitdiffstats
path: root/src/MapLoader.java
diff options
context:
space:
mode:
authormafaldo <mafaldo@heavyhammer.home>2018-11-18 22:29:05 +0100
committermafaldo <mafaldo@heavyhammer.home>2018-11-18 22:29:05 +0100
commite2bcaecea59bdf3a4092efdc4b9624b3f83f6fec (patch)
tree90e70bb3954173c25e63b65bbc64bd78f8c854be /src/MapLoader.java
downloadSubconscious-java-e2bcaecea59bdf3a4092efdc4b9624b3f83f6fec.tar.gz
Subconscious-java-e2bcaecea59bdf3a4092efdc4b9624b3f83f6fec.zip
initial commit
Diffstat (limited to '')
-rw-r--r--src/MapLoader.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/MapLoader.java b/src/MapLoader.java
new file mode 100644
index 0000000..6478961
--- /dev/null
+++ b/src/MapLoader.java
@@ -0,0 +1,60 @@
+import java.lang.String;
+import java.io.File;
+import java.io.PrintWriter;
+import java.io.FileReader;
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.awt.Dimension;
+import com.google.gson.Gson;
+
+class MapLoader {
+ File path;
+
+ public MapLoader(Object path) {
+ if (path instanceof java.io.File) {
+ this.path = (File) path;
+ } else {
+ this.path = new File((String) path);
+ }
+ }
+
+ public Map getMap() {
+ String mapText = "";
+ String line = null;
+ FileReader fr = null;
+ BufferedReader bf = null;
+
+ try {
+ fr = new FileReader(this.path);
+ bf = new BufferedReader(fr);
+ while ((line = bf.readLine()) != null) {
+ mapText = mapText + line;
+ }
+ bf.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+
+ Gson gson = new Gson();
+ Map importMap = gson.fromJson(mapText, Map.class);
+
+ //update map with new classes
+ Map map = new Map(new Dimension(importMap.getSize(), importMap.getSize()));
+ map.update(importMap.getActors(), importMap.getGrid());
+ return map;
+ }
+
+ public void saveMap(Map map) {
+ Gson gson = new Gson();
+ String mapText = gson.toJson(map);
+ PrintWriter out = null;
+ try {
+ out = new PrintWriter(this.path + ".json");
+ } catch (FileNotFoundException ex) {
+ ex.printStackTrace();
+ }
+ out.println(mapText);
+ out.close();
+ }
+}