diff options
Diffstat (limited to '')
-rw-r--r-- | src/subconscious/Game.java | 2 | ||||
-rw-r--r-- | src/subconscious/MapLoader.java | 41 | ||||
-rw-r--r-- | src/subconscious/res/maps/testmap.json (renamed from res/maps/testmap.json) | 0 |
3 files changed, 16 insertions, 27 deletions
diff --git a/src/subconscious/Game.java b/src/subconscious/Game.java index 6f14def..7ef3e8d 100644 --- a/src/subconscious/Game.java +++ b/src/subconscious/Game.java @@ -45,7 +45,7 @@ public class Game { // TODO: this will be replaced with a dynamic mechanism based // on the progress within the game - Map testMap = this.mapLoader.parse("../res/maps/testmap.json"); + Map testMap = this.mapLoader.get("/subconscious/res/maps/testmap.json"); this.currentMap = testMap; this.maps.add(testMap); diff --git a/src/subconscious/MapLoader.java b/src/subconscious/MapLoader.java index 3ece7f0..b7e97d2 100644 --- a/src/subconscious/MapLoader.java +++ b/src/subconscious/MapLoader.java @@ -1,18 +1,16 @@ package subconscious; import java.lang.String; +import java.lang.ClassLoader; // TODO: use java.nio? http://tutorials.jenkov.com/java-nio/nio-vs-io.html -import java.io.File; -import java.io.PrintWriter; -import java.io.FileReader; -import java.io.BufferedReader; -import java.io.FileNotFoundException; +import java.io.InputStream; import java.io.IOException; import java.awt.Dimension; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; // TODO: remove import java.lang.UnsupportedOperationException; @@ -21,7 +19,7 @@ import java.lang.UnsupportedOperationException; // TODO: this class loads AND saves classes, refractor name public class MapLoader { - private Gson gson = new Gson(); + private Gson gson = new GsonBuilder().setPrettyPrinting().create();; public MapLoader() { @@ -32,35 +30,26 @@ public class MapLoader { throw new UnsupportedOperationException(); } - public Map parse(String path) { - File file = new File(path); + public Map get(final String resourceName) { + InputStream is = MapLoader.class.getResourceAsStream(resourceName); + assert is != null; - String mapText = ""; - String line = null; - FileReader fr = null; - BufferedReader bf = null; + int b; + String content = ""; try { - fr = new FileReader(file); - bf = new BufferedReader(fr); - // TODO: read all at once - while ((line = bf.readLine()) != null) { - mapText = mapText + line; + while ((b = is.read()) != -1) { + content += (char) b; } - bf.close(); } catch (IOException ex) { ex.printStackTrace(); } - Map importMap = this.gson.fromJson(mapText, Map.class); - - // TODO: ask @mafaldo why is there a copy? - // update map with new classes - // Map map = new Map(new Dimension(importMap.getSize(), importMap.getSize())); - // map.update(importMap.getActors(), importMap.getGrid()); - // return map; + return this.parse(content); + } - return importMap; + public Map parse(final String json) { + return this.gson.fromJson(json, Map.class); } diff --git a/res/maps/testmap.json b/src/subconscious/res/maps/testmap.json index 1bfb65e..1bfb65e 100644 --- a/res/maps/testmap.json +++ b/src/subconscious/res/maps/testmap.json |