diff --git a/.gitignore b/.gitignore index e102534..f1c9833 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ build .idea/ .settings/ +*.slevel diff --git a/app/src/main/java/school_project/Controller.java b/app/src/main/java/school_project/Controller.java index c9636a0..1e42ec3 100644 --- a/app/src/main/java/school_project/Controller.java +++ b/app/src/main/java/school_project/Controller.java @@ -4,11 +4,14 @@ import javafx.application.Application; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCombination; import javafx.stage.Screen; import javafx.stage.Stage; import school_project.Menu.MenuAccueil; +import school_project.Parsers.FileParserFactory; +import java.io.File; import java.io.IOException; @@ -41,6 +44,19 @@ public class Controller extends Application { public static void switchRoot(Parent root){ Scene scene = new Scene(root); + if(root instanceof GameUI){ + scene.setOnKeyPressed(event ->{ + GameUI game = (GameUI) root; + if(event.getCode() == KeyCode.ESCAPE){ + try { + FileParserFactory.saveFileFromMap(new File("save.slevel"), game.getLevel()); + switchRoot(new MenuAccueil()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }); + } stage.setScene(scene); } public static void main(String[] args) { diff --git a/app/src/main/java/school_project/GameUI.java b/app/src/main/java/school_project/GameUI.java index ccf2702..4aa8abe 100644 --- a/app/src/main/java/school_project/GameUI.java +++ b/app/src/main/java/school_project/GameUI.java @@ -12,8 +12,11 @@ public class GameUI extends Group{ public final static int SPACE_SIZE = 5; private final Vec2 piece_pos_click = new Vec2(); + private Map level; + public GameUI(Map level) throws FileNotFoundException { super(); + this.level = level; MatrixShape grid = new MatrixShape(level); @@ -77,8 +80,11 @@ public class GameUI extends Group{ } } }); - getChildren().add(_piece); } } + + public Map getLevel() { + return level; + } } \ No newline at end of file