Deleted configuration package.

Relocated configuration.Config to launcher.Config.
This commit is contained in:
François Autin
2021-06-10 15:41:39 +02:00
parent 1f58b184b8
commit 8fc483b7e5
3 changed files with 3 additions and 6 deletions

112
src/launcher/Config.java Normal file
View File

@ -0,0 +1,112 @@
package launcher;
import java.io.*;
import org.json.simple.*;
import org.json.simple.parser.*;
public class Config {
public int width, height, rounds;
public boolean fullscreen;
public String stage;
public String p1, p2;
public Config() throws FileNotFoundException {
parse();
}
public void parse() throws FileNotFoundException {
// initialize the parser
JSONParser jsonP = new JSONParser();
try {
// read the json document
JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("game.set"));
// to print all values
// System.out.println(jsonO.values());
// isolate the "test" part and print it
// String test = (String) jsonO.get("test");
// System.out.println("ceci est un test :" + test);
JSONArray game = (JSONArray) jsonO.get("game");
JSONObject settings = (JSONObject) game.get(0);
// print a case of this element
stage = (String) settings.get("stage");
// nb players selection
/* JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
System.out.println("nb_player : " + nb_players.get(1)); */
// character selection
p1 = (String) settings.get("character1");
p2 = (String) settings.get("character2");
height = Integer.parseInt((String) settings.get("height")); // String to int
width = Integer.parseInt((String) settings.get("width"));
// fullscreen
String fs = (String) settings.get("fullscreen");
if (fs.equals("true")) {
fullscreen = true;
} else fullscreen = false;
// rounds
String temprounds = (String) settings.get("rounds");
switch (temprounds) {
case "1": rounds = 1;
case "3": rounds = 3;
case "5": rounds = 5;
default: rounds = 1;
}
// button selection
/* JSONArray allButton = (JSONArray) jsonO.get("button");
System.out.println(allButton);
String up = (String) allButton.get(0);
System.out.println("button for up is : " + up); */
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void write(int width, int height, int rounds, boolean fullscreen, String character1, String character2, String stage) throws Exception {
JSONObject metafile = new JSONObject();
JSONArray array = new JSONArray();
JSONObject set = new JSONObject();
set.put("width", Integer.toString(width));
set.put("height", Integer.toString(height));
set.put("rounds", Integer.toString(rounds));
set.put("fullscreen", Boolean.toString(fullscreen));
set.put("character1", character1);
set.put("character2", character2);
set.put("stage", stage);
array.add(set);
metafile.put("game", array);
try (FileWriter file = new FileWriter("game.set", false)) {
file.write(metafile.toJSONString());
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -45,6 +45,7 @@ public class Launcher extends Application {
* Start method is used by Launcher as an implementation of the Application class to create a JavaFX thread to display the GUI window
*/
@SuppressWarnings("unchecked")
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("launcher.fxml"));
@ -80,6 +81,7 @@ public class Launcher extends Application {
}
@SuppressWarnings("unchecked")
@FXML
public void runGame() {
try {

View File

@ -1,12 +1,7 @@
package launcher;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import configuration.*;
public class Settings {
private Config config;