From 29fdb583448c9fe4c0ab7f101a4866d199afff0e Mon Sep 17 00:00:00 2001 From: Jolijn2020 Date: Tue, 8 Feb 2022 20:14:22 +0100 Subject: [PATCH 1/2] Created SaveMap class to save map as .txt file also created test for it currently only creates file in right place with right name, will enhance later. copied from original branch, to avoid merging conflicts --- src/main/java/app/controller/GameEngine.java | 2 +- src/main/java/app/controller/Settings.java | 12 ++++++ .../graphicsEngine/GraphicsEngine.java | 2 +- .../controller/graphicsEngine/RayTracing.java | 2 +- src/main/java/app/model/{ => map}/Map.java | 9 +++- src/main/java/app/model/map/SaveMap.java | 14 +++++++ src/main/java/app/view/Frame.java | 2 +- src/main/java/app/view/Renderer.java | 2 +- .../java/jgfx/javagradlefx/SaveMapTest.java | 42 +++++++++++++++++++ 9 files changed, 81 insertions(+), 6 deletions(-) rename src/main/java/app/model/{ => map}/Map.java (91%) create mode 100644 src/main/java/app/model/map/SaveMap.java create mode 100644 src/test/java/jgfx/javagradlefx/SaveMapTest.java diff --git a/src/main/java/app/controller/GameEngine.java b/src/main/java/app/controller/GameEngine.java index fabad6bf..f5ca4cc8 100644 --- a/src/main/java/app/controller/GameEngine.java +++ b/src/main/java/app/controller/GameEngine.java @@ -4,7 +4,7 @@ import app.controller.graphicsEngine.RayTracing; import app.controller.linAlg.Vector; import app.model.agents.Agent; -import app.model.Map; +import app.model.map.Map; import app.view.Renderer; import javafx.animation.Animation; import javafx.animation.KeyFrame; diff --git a/src/main/java/app/controller/Settings.java b/src/main/java/app/controller/Settings.java index 5df945ce..62ce7238 100644 --- a/src/main/java/app/controller/Settings.java +++ b/src/main/java/app/controller/Settings.java @@ -8,6 +8,7 @@ public class Settings { private boolean unlocked = true; + private String name; private int gamemode; private int height; private int width; @@ -41,6 +42,12 @@ public Settings() textureType = new ArrayList<>(); } + public void setName(String name) + { + if(unlocked) + this.name=name; + } + public void setGamemode(int gamemode) { if(unlocked) @@ -172,6 +179,11 @@ public void lock() this.unlocked = false; } + public String getName() + { + return this.name; + } + public int getGamemode() { return this.gamemode; diff --git a/src/main/java/app/controller/graphicsEngine/GraphicsEngine.java b/src/main/java/app/controller/graphicsEngine/GraphicsEngine.java index a5a7181d..3f744a12 100644 --- a/src/main/java/app/controller/graphicsEngine/GraphicsEngine.java +++ b/src/main/java/app/controller/graphicsEngine/GraphicsEngine.java @@ -1,7 +1,7 @@ package app.controller.graphicsEngine; import app.model.agents.Agent; -import app.model.Map; +import app.model.map.Map; import java.util.ArrayList; diff --git a/src/main/java/app/controller/graphicsEngine/RayTracing.java b/src/main/java/app/controller/graphicsEngine/RayTracing.java index aa95353e..7c89b9ae 100644 --- a/src/main/java/app/controller/graphicsEngine/RayTracing.java +++ b/src/main/java/app/controller/graphicsEngine/RayTracing.java @@ -2,7 +2,7 @@ import app.controller.linAlg.Vector; import app.model.agents.Agent; -import app.model.Map; +import app.model.map.Map; import app.model.boundary.Placeable; import java.util.ArrayList; diff --git a/src/main/java/app/model/Map.java b/src/main/java/app/model/map/Map.java similarity index 91% rename from src/main/java/app/model/Map.java rename to src/main/java/app/model/map/Map.java index f47ced48..9892c65c 100644 --- a/src/main/java/app/model/Map.java +++ b/src/main/java/app/model/map/Map.java @@ -1,7 +1,8 @@ -package app.model; +package app.model.map; import app.controller.Settings; import app.controller.linAlg.Vector; +import app.model.Furniture; import app.model.agents.Agent; import app.model.agents.Human; import app.model.boundary.BoundaryFactory; @@ -18,6 +19,8 @@ public class Map private ArrayList agents; private Human human; + private Settings setting; + public Map(Settings settings) { System.out.print("Loading settings ... "); @@ -36,6 +39,8 @@ public Map(Settings settings) .stream() .forEach(e -> addFurniture(Furniture.SHADE, e)); + this.setting = settings; + System.out.println("done."); } @@ -49,6 +54,8 @@ public void run(Vector v) human.run(v); } + public Settings getSetting(){return setting;} + public ArrayList getObjects() { return objects; diff --git a/src/main/java/app/model/map/SaveMap.java b/src/main/java/app/model/map/SaveMap.java new file mode 100644 index 00000000..75a6b119 --- /dev/null +++ b/src/main/java/app/model/map/SaveMap.java @@ -0,0 +1,14 @@ +package app.model.map; + +import java.io.File; + +public class SaveMap { + + private final String FILE_PATH = "src/main/java/app/model/map/files/"; + + public void saveMap(Map map) { + String fileName = map.getSetting().getName() + ".txt"; + File mapFile = new File(FILE_PATH + fileName); + } + +} diff --git a/src/main/java/app/view/Frame.java b/src/main/java/app/view/Frame.java index 962b520b..42f623bd 100644 --- a/src/main/java/app/view/Frame.java +++ b/src/main/java/app/view/Frame.java @@ -3,7 +3,7 @@ import app.controller.FileParser; import app.controller.GameEngine; import app.controller.Settings; -import app.model.Map; +import app.model.map.Map; import javafx.scene.input.KeyEvent; import javafx.scene.layout.BorderPane; diff --git a/src/main/java/app/view/Renderer.java b/src/main/java/app/view/Renderer.java index 53594998..948e65b4 100644 --- a/src/main/java/app/view/Renderer.java +++ b/src/main/java/app/view/Renderer.java @@ -1,7 +1,7 @@ package app.view; import app.controller.graphicsEngine.Ray; -import app.model.Map; +import app.model.map.Map; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; diff --git a/src/test/java/jgfx/javagradlefx/SaveMapTest.java b/src/test/java/jgfx/javagradlefx/SaveMapTest.java new file mode 100644 index 00000000..0ce86ab4 --- /dev/null +++ b/src/test/java/jgfx/javagradlefx/SaveMapTest.java @@ -0,0 +1,42 @@ +package jgfx.javagradlefx; + +import app.controller.Settings; +import app.model.map.Map; +import app.model.map.SaveMap; +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; + +import static java.nio.file.Files.deleteIfExists; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class SaveMapTest { + + @Test + void CreateFile() + { + Settings settings = new Settings(); + settings.setName("createFileTest"); + Map map = new Map(settings); + + SaveMap safeMap = new SaveMap(); + safeMap.saveMap(map); + + String filePathName = "src/main/java/app/model/map/files/createFileTest.txt"; + + try + { + Scanner scanner = new Scanner(filePathName); + + Path path = Paths.get(filePathName); + deleteIfExists(path); + } + catch(Exception e) + { + fail("an exception occured why trying to access the file"); + } + } +} From 83692669c7b43e23c0366622fd3fb1e08e8c8ce2 Mon Sep 17 00:00:00 2001 From: Jolijn2020 Date: Tue, 8 Feb 2022 21:10:22 +0100 Subject: [PATCH 2/2] implemented feedback added "name" variable to FileParser and made back-up name moved folder to save maps to scr/java/resources added @beforeAll and @afterAll test methods to SaveMapTest --- src/main/java/app/controller/FileParser.java | 3 +++ src/main/java/app/model/map/SaveMap.java | 12 ++++++---- .../java/jgfx/javagradlefx/SaveMapTest.java | 22 ++++++++++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/app/controller/FileParser.java b/src/main/java/app/controller/FileParser.java index 4b0820cc..dea5cc6c 100644 --- a/src/main/java/app/controller/FileParser.java +++ b/src/main/java/app/controller/FileParser.java @@ -46,6 +46,9 @@ public static void parseNextLine(String nextLine, int countLines) String[] coords = val.split(" "); switch(id) { + case "name": + map.setName((String) val); + break; case "gameMode": map.setGamemode(Integer.parseInt(val)); break; diff --git a/src/main/java/app/model/map/SaveMap.java b/src/main/java/app/model/map/SaveMap.java index 75a6b119..0f859e1a 100644 --- a/src/main/java/app/model/map/SaveMap.java +++ b/src/main/java/app/model/map/SaveMap.java @@ -1,14 +1,18 @@ package app.model.map; +import javafx.util.converter.LocalTimeStringConverter; + import java.io.File; public class SaveMap { - private final String FILE_PATH = "src/main/java/app/model/map/files/"; + private final String FILE_PATH = "src/main/java/resources/"; public void saveMap(Map map) { - String fileName = map.getSetting().getName() + ".txt"; - File mapFile = new File(FILE_PATH + fileName); - } + String fileName = map.getSetting().getName(); + if(fileName== null) + fileName = "Map_" + String.valueOf(Math.round(Math.random()*100000)); + File mapFile = new File(FILE_PATH + fileName + ".txt"); + } } diff --git a/src/test/java/jgfx/javagradlefx/SaveMapTest.java b/src/test/java/jgfx/javagradlefx/SaveMapTest.java index 0ce86ab4..15331586 100644 --- a/src/test/java/jgfx/javagradlefx/SaveMapTest.java +++ b/src/test/java/jgfx/javagradlefx/SaveMapTest.java @@ -3,6 +3,8 @@ import app.controller.Settings; import app.model.map.Map; import app.model.map.SaveMap; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.nio.file.Path; @@ -15,8 +17,10 @@ public class SaveMapTest { - @Test - void CreateFile() + String filePathName = "src/main/java/resources/createFileTest.txt"; + + @BeforeAll + void createMapFile() { Settings settings = new Settings(); settings.setName("createFileTest"); @@ -25,18 +29,26 @@ void CreateFile() SaveMap safeMap = new SaveMap(); safeMap.saveMap(map); - String filePathName = "src/main/java/app/model/map/files/createFileTest.txt"; - try { Scanner scanner = new Scanner(filePathName); + } + catch(Exception e) + { + fail("an exception occured why trying to create/access the file"); + } + } + @AfterAll + void deleteMapFile() { + try + { Path path = Paths.get(filePathName); deleteIfExists(path); } catch(Exception e) { - fail("an exception occured why trying to access the file"); + fail("an exception occured why trying to delete the file"); } } }