From 4572a1659f80a91346a2fd899aa539bf964b0a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Thu, 10 Jun 2021 13:31:36 +0200 Subject: [PATCH] Refactored launcher.Settings to use new configuration.Config Simpler, safer design. --- src/launcher/Settings.java | 170 +++++-------------------------------- 1 file changed, 23 insertions(+), 147 deletions(-) diff --git a/src/launcher/Settings.java b/src/launcher/Settings.java index d2e2060..c52bc7a 100644 --- a/src/launcher/Settings.java +++ b/src/launcher/Settings.java @@ -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 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(); + } } }