Skip to content
Snippets Groups Projects
Commit 0ab5b4e1 authored by Max Wittig's avatar Max Wittig
Browse files

added settings

parent 5b853706
No related branches found
No related tags found
No related merge requests found
package de.bricked.game.settings;
import javafx.stage.Screen;
public enum GameSize
{
SMALL(800, 600),
NORMAL(1024, 768),
BIGGER(1280, 1024),
BIG(1920, 1080),
FULL_SCREEN();
private int width;
private int height;
GameSize(int width, int height)
{
this.width = width;
this.height = height;
}
GameSize()
{
this.width = (int)(Screen.getPrimary().getVisualBounds().getWidth());
this.height = (int)(Screen.getPrimary().getVisualBounds().getHeight());
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
}
package de.bricked.game.settings;
import com.google.gson.Gson;
import logger.LogLevel;
import logger.Logger;
import tools.PathUtils;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.FileSystems;
import java.nio.file.Files;
public class Settings
{
private GameSize gameSize;
private String language; //TODO change this to lanugage class maybe?
private transient final String directory = "/deadspaghetti/bricked/";
private transient final String filename = "settings.json";
private transient Gson gson;
private transient File file;
public Settings()
{
gson = new Gson();
PathUtils.checkFolder(new File(PathUtils.getOSindependentPath() + directory));
file = new File(PathUtils.getOSindependentPath() + directory + filename);
if(!file.exists())
{
try
{
initDefaultSettings();
save();
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
try
{
load();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
private void initDefaultSettings()
{
gameSize = GameSize.NORMAL;
language = "eng";
}
public void save()
{
try
{
FileWriter fileWriter = new FileWriter(file);
System.out.println(gson.toJson(this));
fileWriter.write(gson.toJson(this));
fileWriter.flush();
fileWriter.close();
}
catch (Exception e)
{
Logger.log(LogLevel.ERROR, Logger.exceptionToString(e));
}
}
public void load() throws Exception
{
String jsonContent = new String(Files.readAllBytes(FileSystems.getDefault().getPath(directory + filename)));
Settings loadedSettings = gson.fromJson(jsonContent, Settings.class);
this.gameSize = loadedSettings.gameSize;
this.language = loadedSettings.language;
//MORE settings go here
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment