From 48c5a687ee3ea75e42f3b485f185de2b735147f8 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 20 Nov 2018 18:44:18 +0100 Subject: Move Map loading into Game (out of graphics) --- src/subconscious/Game.java | 13 ++++++- src/subconscious/MapLoader.java | 56 ++++++++++++++++++++---------- src/subconscious/graphics/BattleScene.java | 4 +-- src/subconscious/graphics/MapScene.java | 10 ++++-- 4 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/subconscious/Game.java b/src/subconscious/Game.java index 7fd8b33..61c0836 100644 --- a/src/subconscious/Game.java +++ b/src/subconscious/Game.java @@ -26,6 +26,9 @@ public class Game { private ArrayList maps; private Map currentMap; + // TODO: load audio? + private MapLoader mapLoader = new MapLoader(); + public Game() { this.setState(State.MENU); @@ -34,13 +37,21 @@ public class Game { this.actors = new ArrayList<>(); this.maps = new ArrayList<>(); + + // TODO: this will be replaced with a dynamic mechanism based + // on the progress within the game + Map testMap = this.mapLoader.parse("../testmap.json"); + + this.currentMap = testMap; + this.maps.add(testMap); } public void start() { this.setState(State.DREAM); + } - public Map getCurrentMap() { + public Map getMap() { return currentMap; } diff --git a/src/subconscious/MapLoader.java b/src/subconscious/MapLoader.java index 2bc320c..b1c3973 100644 --- a/src/subconscious/MapLoader.java +++ b/src/subconscious/MapLoader.java @@ -14,26 +14,34 @@ import java.awt.Dimension; import com.google.gson.Gson; +// TODO: remove +import java.lang.UnsupportedOperationException; + + // TODO: this class loads AND saves classes, refractor name public class MapLoader { - File path; - public MapLoader(String path) { - this.path = new File(path); + private Gson gson = new Gson(); + + public MapLoader() { + } - public MapLoader(File file) { - this.path = file; + @Deprecated + public MapLoader(Object o) { + throw new UnsupportedOperationException(); } - public Map getMap() { + public Map parse(String path) { + File file = new File(path); + String mapText = ""; String line = null; FileReader fr = null; BufferedReader bf = null; try { - fr = new FileReader(this.path); + fr = new FileReader(file); bf = new BufferedReader(fr); // TODO: read all at once while ((line = bf.readLine()) != null) { @@ -44,8 +52,7 @@ public class MapLoader { ex.printStackTrace(); } - Gson gson = new Gson(); - Map importMap = gson.fromJson(mapText, Map.class); + Map importMap = this.gson.fromJson(mapText, Map.class); // TODO: ask @mafaldo why is there a copy? // update map with new classes @@ -56,16 +63,27 @@ public class MapLoader { return importMap; } + + @Deprecated + public Map getMap() { + throw new UnsupportedOperationException(); + } + + @Deprecated 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(); + throw new UnsupportedOperationException(); } + + // 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(); + // } } diff --git a/src/subconscious/graphics/BattleScene.java b/src/subconscious/graphics/BattleScene.java index 2c0efa4..6366d18 100644 --- a/src/subconscious/graphics/BattleScene.java +++ b/src/subconscious/graphics/BattleScene.java @@ -55,8 +55,8 @@ public class BattleScene extends MapScene implements ActionListener { super(g); // TODO: this should be handled in MapScene - MapLoader mapLoader = new MapLoader("../testmap.json"); - this.map = mapLoader.getMap(); + // MapLoader mapLoader = new MapLoader("../testmap.json"); + // this.map = mapLoader.getMap(); this.selectedActor = this.map.getNextActor(); diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index 80b577c..acbf7b6 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -26,6 +26,11 @@ import java.awt.geom.NoninvertibleTransformException; */ @SuppressWarnings("serial") public abstract class MapScene extends Scene { + + protected Game game; + + // TODO: remove + // @Deprecated protected Map map; // used for rendering @@ -46,7 +51,8 @@ public abstract class MapScene extends Scene { public MapScene(Game g) { super(g); - // this.map = g.getCurrentMap(); + this.game = g; + this.map = this.game.getMap(); if (this.canvasSize.width < this.canvasSize.height) { this.shorterCanvasLenght = this.canvasSize.width; @@ -64,7 +70,7 @@ public abstract class MapScene extends Scene { public MapScene() { } protected void renderTiles(Graphics2D g) { - for (Tile tile : this.map.getGrid()) { + for (Tile tile : this.game.getMap().getGrid()) { switch (tile.getType()) { case CLEAR: continue; -- cgit v1.2.1