Merge remote-tracking branch 'origin/master'

This commit is contained in:
no 2021-06-10 12:40:58 +02:00
commit 264f53f88d
5 changed files with 136 additions and 442 deletions

1
game.set Normal file
View File

@ -0,0 +1 @@
{"game":[{"character2":"ken","character1":"ryu","fullscreen":"true","stage":"default","width":"1920","rounds":"5","height":"1080"}]}

View File

@ -1,165 +1,112 @@
package configuration;
import java.util.Random;
public class Config {
// tester a la main (okay normalement mais ajouter des test)
public static void main(String[] args) {
int[] tempfortest = { 0, 2, 1, 1 }; // {arene/nb_joueur/perso1/perso2} 0=alatoire
config(tempfortest);
}
// les variable a configurer
// sel1 pour savoir si on a deja selectionner le joueur 1
public static String arene, perso1, perso2;
public static int nb_joueur;
private static boolean sel1 = false;
/*
* fonction config qui prend en entre un tableau d'entier chaque case
* correspondant a une variable, et ne retourne rien mais modifie la valeur des
* variables par des fonctions annexes
*/
private static void config(int[] tab) {
int i = 0;
while (i < tab.length) {
switch (i) {
case 0:
SelArene(tab[i]);
i++;
break;
case 1:
NbJoueur(tab[i]);
i++;
break;
case 2:
SelPerso(tab[i]);
i++;
break;
case 3:
SelPerso(tab[i]);
i++;
break;
/*
* case 4: ?
*/
default:
System.out.println("ERROR OUT OF BOUNDS CONFIG ARRAY");
i = tab.length;
break;
}
}
System.out.println(arene + " " + nb_joueur + " " + perso1 + " " + perso2 + " " + sel1);
}
/*
* fonction SelArene prend un entier en parametre et permet de choisir l'arene
* du jeu
*/
private static void SelArene(int s) {
switch (s) {
case 0:
SelArene(random(1, 2));
break;
case 1:
arene = "arene1.png";
break;
case 2:
arene = "arene2.png";
break;
default:
System.out.println("ERROR ARENE INEXISTANTE");
}
}
/*
* fonction NbJoueur prend un entier en parametre et permet de determiner si un
* bot sera necessaire
*/
private static void NbJoueur(int s) {
switch (s) {
case 1:
nb_joueur = 1;
break;
case 2:
nb_joueur = 2;
break;
default:
System.out.println("ERROR NUMBER OF PLAYER");
}
}
/*
* fonction SelArene prend un entier en parametre et permet de choisir le
* personnage en fonction du joueur
*/
private static void SelPerso(int s) {
if (sel1 == false) {
switch (s) {
case 0:
SelPerso(random(1, 2));
break;
case 1:
perso1 = "perso1.png";
sel1 = true;
break;
case 2:
perso1 = "perso2.png";
sel1 = true;
break;
default:
System.out.println("ERROR PERSO INEXISTANT");
}
} else if (sel1 == true) {
switch (s) {
case 0:
SelPerso(random(1, 2));
break;
case 1:
perso2 = "perso1.png";
sel1 = false;
if (perso1 == perso2) {
perso2 = "perso1_swapcolor.png";
}
break;
case 2:
perso2 = "perso2.png";
sel1 = false;
if (perso1 == perso2) {
perso2 = "perso2_swapcolor.png";
}
break;
default:
System.out.println("ERROR PERSO INEXISTANT");
}
} else {
System.out.println("ERROR SELECTION PLAYER");
}
}
// fonction nombre aleatoire entre deux borne
private static int random(int min, int max) {
Random random = new Random();
int value = random.nextInt(max - 1 + min) + min;
return value;
}
}
package configuration;
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

@ -1,75 +0,0 @@
package configuration;
import java.io.*;
import org.json.simple.*;
import org.json.simple.parser.*;
public class JsonToJava {
public static void main(String args[]) {
JsonRecover();
}
private static void JsonRecover() {
// initialize the parser
JSONParser jsonP = new JSONParser();
try {
// read the json document
JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json"));
// 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);
// arena selection
// select an element on the list
JSONArray arena = (JSONArray) jsonO.get("arena");
// print a case of this element
System.out.println("arena : " + arena.get(1));
// nb players selection
JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
System.out.println("nb_player : " + nb_players.get(1));
// character selection
JSONArray character1 = (JSONArray) jsonO.get("character1");
System.out.println("players 1 : " + character1.get(1));
JSONArray character2 = (JSONArray) jsonO.get("character2");
System.out.println("players 2 : " + character2.get(1));
// resolution
JSONArray resolution = (JSONArray) jsonO.get("resolution");
// resolution string " width x heigth"
JSONObject reso = (JSONObject) resolution.get(1);
String heightStr = (String) reso.get("height");
int height = Integer.parseInt(heightStr); // String to int
String widthStr = (String) reso.get("width");
int width = Integer.parseInt(widthStr);
System.out.println("heigth : " + height + " width : " + width);
// 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();
}
}
}

View File

@ -1,55 +0,0 @@
{
"arena": [
"random",
"arena1.png",
"arena2.png"
],
"nb_players": [
"1",
"2"
],
"character1": [
"random",
"character1.png",
"character2.png"
],
"character2": [
"random",
"character1.png",
"character1_swapcolor.png",
"character2.png",
"character2_swapcolor.png"
],
"resolution": [
{
"width": "800",
"height": "600"
},
{
"width": "1280",
"height": "1024"
},
{
"width": "1680",
"height": "1050"
},
{
"width": "1920",
"height": "1080"
},
{
"persoWidth": "800",
"persoHeight": "600"
}
],
"button": [
"UP",
"DOWN",
"RIGTH",
"LEFT",
"A",
"B",
"X",
"Y"
]
}

View File

@ -3,162 +3,38 @@ package launcher;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import configuration.*;
public class Settings {
private class Player {
private int controller_id;
private String character;
private int color;
public Player(int cid, String ch, int col) {
controller_id = cid;
character = ch;
color = col;
}
protected int getController_id() {
return controller_id;
}
protected void setController_id(int controller_id) {
this.controller_id = controller_id;
}
protected String getCharacter() {
return character;
}
protected void setCharacter(String character) {
this.character = character;
}
protected int getColor() {
return color;
}
protected void setColor(int color) {
this.color = color;
}
}
private Config config;
private FileInputStream f_is;
private int width, height, r_x, r_y, rounds;
private boolean fullscreen;
private String stage;
private Player p1, p2;
public Settings() throws Exception {
public Settings() {
try {
f_is = new FileInputStream("game.set");
} catch (FileNotFoundException e) {
File f = new File("game.set");
f.createNewFile();
} finally {
f_is = new FileInputStream("game.set");
config = new Config();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
parseSettings();
} finally {
setDefaultSettings();
}
}
private void parseSettings() throws Exception {
//TODO: parseSettings() (Needs settings syntax fixed)
}
public void setSettings() throws Exception {
//TODO: parseSettings() (Needs settings syntax fixed)
}
private void setDefaultSettings() {
width = 800;
height = 600;
r_x = 4;
r_y = 3;
rounds = 3;
fullscreen = false;
p1 = new Player(0, "base", 0);
p2 = new Player(0, "base", 0);
}
protected FileInputStream getF_is() {
return f_is;
}
protected int getWidth() {
return width;
}
protected int getHeight() {
return height;
}
protected int getR_x() {
return r_x;
}
protected int getR_y() {
return r_y;
}
protected int getRounds() {
return rounds;
}
protected boolean isFullscreen() {
return fullscreen;
}
protected String getStage() {
return stage;
}
protected Player getP1() {
return p1;
}
protected Player getP2() {
return p2;
}
protected void setF_is(FileInputStream f_is) {
this.f_is = f_is;
}
protected void setWidth(int width) {
this.width = width;
}
protected void setHeight(int height) {
this.height = height;
}
protected void setR_x(int r_x) {
this.r_x = r_x;
}
protected void setR_y(int r_y) {
this.r_y = r_y;
}
protected void setRounds(int rounds) {
this.rounds = rounds;
}
protected void setFullscreen(boolean fullscreen) {
this.fullscreen = fullscreen;
}
protected void setStage(String stage) {
this.stage = stage;
}
protected void setP1(Player p1) {
this.p1 = p1;
}
protected void setP2(Player p2) {
this.p2 = p2;
HashMap<String, Object> set = Launcher.pointer.getArraysettings();
int width = (Integer) set.get("width");
int height = (Integer) set.get("height");
int rounds = (Integer) set.get("rounds");
boolean fullscreen = (Boolean) set.get("fullscreen");
String character1 = (String) set.get("character1");
String character2 = (String) set.get("character2");
String stage = (String) set.get("stage");
try {
config.write(width, height, rounds, fullscreen, character1, character2, stage);
} catch (Exception e) {
e.printStackTrace();
}
}
}