Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created SaveMap class to save map as .txt file #68

Merged
merged 2 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/app/controller/FileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/controller/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/app/controller/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class Settings
{
private boolean unlocked = true;
private String name;
Jolijn2020 marked this conversation as resolved.
Show resolved Hide resolved
private int gamemode;
private int height;
private int width;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -172,6 +179,11 @@ public void lock()
this.unlocked = false;
}

public String getName()
{
return this.name;
}

public int getGamemode()
{
return this.gamemode;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +19,8 @@ public class Map
private ArrayList<Agent> agents;
private Human human;

private Settings setting;

public Map(Settings settings)
{
System.out.print("Loading settings ... ");
Expand All @@ -36,6 +39,8 @@ public Map(Settings settings)
.stream()
.forEach(e -> addFurniture(Furniture.SHADE, e));

this.setting = settings;

System.out.println("done.");
}

Expand All @@ -49,6 +54,8 @@ public void run(Vector v)
human.run(v);
}

public Settings getSetting(){return setting;}

public ArrayList<Placeable> getObjects()
{
return objects;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/app/model/map/SaveMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +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/resources/";

public void saveMap(Map map) {

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");
}
}
2 changes: 1 addition & 1 deletion src/main/java/app/view/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/view/Renderer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/jgfx/javagradlefx/SaveMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package jgfx.javagradlefx;

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;
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 {

String filePathName = "src/main/java/resources/createFileTest.txt";

@BeforeAll
void createMapFile()
{
Settings settings = new Settings();
settings.setName("createFileTest");
Map map = new Map(settings);

SaveMap safeMap = new SaveMap();
safeMap.saveMap(map);

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);
Jolijn2020 marked this conversation as resolved.
Show resolved Hide resolved
}
catch(Exception e)
{
fail("an exception occured why trying to delete the file");
}
}
}