From 76f96a2bada13d75db3661b9db62b34def714808 Mon Sep 17 00:00:00 2001 From: Antoine Date: Thu, 24 Jun 2021 03:55:43 +0200 Subject: [PATCH] =?UTF-8?q?Premi=C3=A8re=20impl=C3=A9mentation=20du=20came?= =?UTF-8?q?raPushBack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gameplay/match/match.java | 25 +++++++ src/launcher/Config.java | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/launcher/Config.java diff --git a/src/gameplay/match/match.java b/src/gameplay/match/match.java index ab524fe..cd4a951 100644 --- a/src/gameplay/match/match.java +++ b/src/gameplay/match/match.java @@ -407,6 +407,7 @@ public class match { updatePos(p2,!p1LooksRight); pushBox(); + cameraPushBack(); if(p1LooksRight) { @@ -757,7 +758,31 @@ public class match { p2.setPos((int) (p2.getPosX() - sizeIntersection/2), p2.getPosY()); } } + } + /** + * + */ + private static void cameraPushBack(){ + boolean lookRight = p1.getPosX() < p2.getPosX(); + Character left; + Character right; + ObjectGl rightObj; + if (lookRight) { + left = p1; + right = p2; + rightObj = objP2; + } else { + left = p2; + right = p1; + rightObj = objP1; + } + float leftOutOfView = left.getPosX() - (-engine.getViewXPos() - engine.getCamera().getDimension()); + float rightOutOfView = (right.getPosX() + rightObj.getWidth() * rightObj.getScalingFactor()) - (-engine.getViewXPos() + engine.getCamera().getDimension()); + if(leftOutOfView < 0 && rightOutOfView > 0){ + left.setPos((int) (left.getPosX() - leftOutOfView), left.getPosY()); + right.setPos((int) (right.getPosX() - rightOutOfView), right.getPosY()); + } } /* diff --git a/src/launcher/Config.java b/src/launcher/Config.java new file mode 100644 index 0000000..9d95420 --- /dev/null +++ b/src/launcher/Config.java @@ -0,0 +1,118 @@ +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, hitboxes; + 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"); + + // rounds + rounds = Integer.parseInt((String) settings.get("rounds")); + + // 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; + + String hb = (String) settings.get("hitboxes"); + if (hb == null) hb = "false"; + if (hb.equals("true")) { + hitboxes = true; + } else hitboxes = 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; + } + + } catch (FileNotFoundException e) { + File f = new File("game.set"); + try { + f.createNewFile(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + System.exit(1); + } + parse(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + System.out.println("Empty config file"); + } + } + + @SuppressWarnings("unchecked") + public void write(int width, int height, int rounds, boolean fullscreen, boolean hitboxes, 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("hitboxes", Boolean.toString(hitboxes)); + 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(); + } + } +}