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/build.gradle b/app/build.gradle index b253582..ee76748 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -29,7 +29,7 @@ dependencies { application { // Define the main class for the application. - mainClass = 'school_project.Controller' + mainClass = project.hasProperty("mainClass") ? project.getProperty("mainClass") : 'school_project.Controller' } javafx { @@ -41,3 +41,7 @@ tasks.named('test') { // Use JUnit Platform for unit tests. useJUnitPlatform() } + +run{ +standardInput = System.in +} diff --git a/app/src/main/java/school_project/Controller.java b/app/src/main/java/school_project/Controller.java index c9636a0..0f827ab 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; @@ -19,6 +22,7 @@ public class Controller extends Application { @Override public void start(Stage primaryStage) throws IOException { + new File("save.slevel"); stage = primaryStage; screen_size = new Vec2( (int) Screen.getPrimary().getBounds().getWidth(), @@ -41,6 +45,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 821ad54..d83ef99 100644 --- a/app/src/main/java/school_project/GameUI.java +++ b/app/src/main/java/school_project/GameUI.java @@ -13,8 +13,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); @@ -29,17 +32,17 @@ public class GameUI extends Group{ for (Piece p : level.getPieces()) { MatrixShape _piece = new MatrixShape(p); - _piece.setLayoutX(piece_space.x); - _piece.setLayoutY(piece_space.y); - - piece_space.y += _piece.boundary_size.y; - - if(piece_space.y >= Controller.screen_size.y){ + if(piece_space.y + _piece.boundary_size.y >= Controller.screen_size.y){ column++; piece_space.y = SPACE_SIZE; piece_space.x = (SEGMENT_SIZE*3 + SPACE_SIZE*4 )* column; } + _piece.setLayoutX(piece_space.x); + _piece.setLayoutY(piece_space.y); + + piece_space.y += _piece.boundary_size.y; + // Pieces Events _piece.setOnMouseClicked(event -> { if(event.getButton() == MouseButton.SECONDARY){ @@ -59,6 +62,7 @@ public class GameUI extends Group{ _piece.setOnMouseReleased(event -> { if(event.getButton() != MouseButton.PRIMARY) return; + p.setPosition(null); if(event.getSceneX() > grid.getLayoutX() && event.getSceneX() < grid.getLayoutX() + grid.boundary_size.x && event.getSceneY() > grid.getLayoutY() && event.getSceneY() < grid.getLayoutY() + grid.boundary_size.y ) { @@ -67,7 +71,7 @@ public class GameUI extends Group{ (int) (_piece.getLayoutY() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutY())/(SEGMENT_SIZE+SPACE_SIZE), (int) (_piece.getLayoutX() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutX())/(SEGMENT_SIZE+SPACE_SIZE) ); - System.out.println(level.placePiece(p, piece_position_in_grid) + piece_position_in_grid.toString()); + level.placePiece(p, piece_position_in_grid); if(p.getPosition() != null){ _piece.setLayoutX(grid.getLayoutX() + p.getPosition().y * (SEGMENT_SIZE+SPACE_SIZE)); _piece.setLayoutY(grid.getLayoutY() + p.getPosition().x * (SEGMENT_SIZE+SPACE_SIZE)); @@ -77,8 +81,11 @@ public class GameUI extends Group{ } } }); - getChildren().add(_piece); } } + + public Map getLevel() { + return level; + } } \ No newline at end of file diff --git a/app/src/main/java/school_project/Menu/MenuAccueil.java b/app/src/main/java/school_project/Menu/MenuAccueil.java index e9fa22a..dc33456 100644 --- a/app/src/main/java/school_project/Menu/MenuAccueil.java +++ b/app/src/main/java/school_project/Menu/MenuAccueil.java @@ -10,10 +10,13 @@ import javafx.scene.text.Font; import school_project.Controller; import school_project.GameUI; import school_project.MapGenerator; +import school_project.Parsers.FileParserFactory; + +import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; public class MenuAccueil extends StackPane { - public MenuAccueil(){ super(); //create all the objet that I need @@ -21,7 +24,9 @@ public class MenuAccueil extends StackPane { SlctDifficulty.getItems().addAll("Easy", "Medium", "Difficult"); Label RdmLvl = new Label("Random Level : "); + Button LoadLvl = new Button("Load game"); Button SelectLevel= new Button("Select Level"); + Label Title = new Label("Welcome to Road to Master"); SlctDifficulty.setOnAction(event -> { String choosediff = SlctDifficulty.getSelectionModel().getSelectedItem(); @@ -52,12 +57,14 @@ public class MenuAccueil extends StackPane { }); //set up all the Button where I need - getChildren().addAll(Title,SlctDifficulty,SelectLevel,RdmLvl); + + getChildren().addAll(Title,SlctDifficulty,SelectLevel,RdmLvl,LoadLvl); RdmLvl.setFont(Font.font(25)); RdmLvl.setTextFill(Color.GOLD); Title.setFont(Font.font(40)); Title.setTextFill(Color.RED); setAlignment(Title, Pos.TOP_CENTER); + setAlignment(LoadLvl,Pos.BOTTOM_CENTER); setAlignment(SlctDifficulty,Pos.CENTER_LEFT); setAlignment(SelectLevel,Pos.CENTER_RIGHT); setAlignment(RdmLvl, Pos.CENTER_LEFT); @@ -66,16 +73,18 @@ public class MenuAccueil extends StackPane { setMargin(SlctDifficulty,new Insets(0,0,0,300)); setMargin(SelectLevel,new Insets(0,300,0,0)); setMargin(Title,new Insets(200,0,0,0)); + setMargin(LoadLvl,new Insets(0,0,200,0)); SelectLevel.setOnAction(event -> Controller.switchRoot(new MenuLevel(1))); + LoadLvl.setOnAction(event -> { + try { + FileParserFactory.loadMapFromFile(new File("save.slevel")); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); getStyleClass().add("BorderPane"); getStylesheets().add(String.valueOf(getClass().getResource("StyleMenuAcceuil.css"))); - - - } - - - -} +} \ No newline at end of file diff --git a/app/src/main/java/school_project/Menu/MenuLevel.java b/app/src/main/java/school_project/Menu/MenuLevel.java index aff5f6d..6987f63 100644 --- a/app/src/main/java/school_project/Menu/MenuLevel.java +++ b/app/src/main/java/school_project/Menu/MenuLevel.java @@ -7,6 +7,11 @@ import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.RowConstraints; import school_project.Controller; +import school_project.GameUI; +import school_project.Parsers.FileParserFactory; + +import java.io.File; +import java.io.IOException; public class MenuLevel extends GridPane { private int StartLevel; @@ -60,30 +65,38 @@ public class MenuLevel extends GridPane { //It's here that I put all buttons where I need (base on column not row) for (int i = 0; i < 3; i++) { for (int j = 1; j < 5; j++) { + Button levelButton = new Button("level "+(StartLevel)); + levelButton.setOnAction(event -> { + try { + String levelName = ((Button)event.getSource()).getText().replace(" ", "") + ".level"; + System.out.println(levelName); + GameUI level = new GameUI(FileParserFactory.loadMapFromFile(new File(Controller.class.getResource("levels/" + levelName).getFile()))); + Controller.switchRoot(level); + } catch (IOException e) { + System.out.println("Le niveau " + StartLevel + "n'existe pas."); + } + }); if(i==0){ - Button Level = new Button("level "+(StartLevel)); StartLevel+=3; - add(Level,i,j); - setHalignment(Level,HPos.CENTER); + add(levelButton,i,j); + setHalignment(levelButton,HPos.CENTER); if(j==4){ StartLevel-=11; } } else if(i==1&&j!=4) { - Button Level = new Button("level "+(StartLevel)); StartLevel += 3; - add(Level, i, j); - setHalignment(Level,HPos.CENTER); + add(levelButton, i, j); + setHalignment(levelButton,HPos.CENTER); if (j == 3) { StartLevel -=8; } } else if(i==2&&j!=4){ - Button Level = new Button("level "+(StartLevel)); StartLevel+=3; - add(Level,i,j); - setHalignment(Level,HPos.CENTER); + add(levelButton,i,j); + setHalignment(levelButton,HPos.CENTER); } } } diff --git a/app/src/main/resources/school_project/levels/level1.level b/app/src/main/resources/school_project/levels/level1.level new file mode 100644 index 0000000..a3cdeb3 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level1.level @@ -0,0 +1 @@ +SMSààSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level10.level b/app/src/main/resources/school_project/levels/level10.level new file mode 100644 index 0000000..793e499 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level10.level @@ -0,0 +1 @@ +SMSêÿ"ð"°"p1àSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/level11.level b/app/src/main/resources/school_project/levels/level11.level similarity index 100% rename from app/src/main/resources/school_project/level11.level rename to app/src/main/resources/school_project/levels/level11.level diff --git a/app/src/main/resources/school_project/levels/level12.level b/app/src/main/resources/school_project/levels/level12.level new file mode 100644 index 0000000..64d2c52 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level12.level @@ -0,0 +1 @@ +SMSÿÿÿ€"°1àà2ü"p3í€SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level13.level b/app/src/main/resources/school_project/levels/level13.level new file mode 100644 index 0000000..b4a7301 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level13.level @@ -0,0 +1 @@ +SMS?ÿÿýðà"°3í€à"ð2ü3í€SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level14.level b/app/src/main/resources/school_project/levels/level14.level new file mode 100644 index 0000000..f209ed5 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level14.level @@ -0,0 +1 @@ +SMSfÿÿŸ< 1à"°"p€&ÿð"ð"ð"°àSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level15.level b/app/src/main/resources/school_project/levels/level15.level new file mode 100644 index 0000000..ec54527 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level15.level @@ -0,0 +1 @@ +SMSÿÿð"°1à€"p#üÀ!ÀSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level16.level b/app/src/main/resources/school_project/levels/level16.level new file mode 100644 index 0000000..71222e8 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level16.level @@ -0,0 +1 @@ +SMSÿÿÿ€"ðà€"p3í€3o€SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level17.level b/app/src/main/resources/school_project/levels/level17.level new file mode 100644 index 0000000..c2808d1 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level17.level @@ -0,0 +1 @@ +SMSÿÿÿÿð3ÿ€3í€1à1à#ü#üSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level18.level b/app/src/main/resources/school_project/levels/level18.level new file mode 100644 index 0000000..fdb4461 Binary files /dev/null and b/app/src/main/resources/school_project/levels/level18.level differ diff --git a/app/src/main/resources/school_project/levels/level19.level b/app/src/main/resources/school_project/levels/level19.level new file mode 100644 index 0000000..bccddfc --- /dev/null +++ b/app/src/main/resources/school_project/levels/level19.level @@ -0,0 +1 @@ +SMSóÿÿóàà3ü€3o€4ð"°SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level2.level b/app/src/main/resources/school_project/levels/level2.level new file mode 100644 index 0000000..f37c5bd --- /dev/null +++ b/app/src/main/resources/school_project/levels/level2.level @@ -0,0 +1 @@ +SMSà1àSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level20.level b/app/src/main/resources/school_project/levels/level20.level new file mode 100644 index 0000000..62732e5 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level20.level @@ -0,0 +1 @@ +SMSÿÿÿ€À3í€"ð2ø3ü€SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level21.level b/app/src/main/resources/school_project/levels/level21.level new file mode 100644 index 0000000..43446e8 Binary files /dev/null and b/app/src/main/resources/school_project/levels/level21.level differ diff --git a/app/src/main/resources/school_project/levels/level22.level b/app/src/main/resources/school_project/levels/level22.level new file mode 100644 index 0000000..40da38f --- /dev/null +++ b/app/src/main/resources/school_project/levels/level22.level @@ -0,0 +1 @@ +SMSÿÿÿ€"pàÀ"°#ü#è#èSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level23.level b/app/src/main/resources/school_project/levels/level23.level new file mode 100644 index 0000000..c5c94f1 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level23.level @@ -0,0 +1 @@ +SMSÿÿÿ€2Üà"Ð#è#¼BSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level24.level b/app/src/main/resources/school_project/levels/level24.level new file mode 100644 index 0000000..2bc28e5 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level24.level @@ -0,0 +1 @@ +SMSvÿÿ€#쀀#ì#¼BSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level25.level b/app/src/main/resources/school_project/levels/level25.level new file mode 100644 index 0000000..bc3e531 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level25.level @@ -0,0 +1 @@ +SMSÿÿÿÿð#¼#|!À#\#ü2ô2äBSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level26.level b/app/src/main/resources/school_project/levels/level26.level new file mode 100644 index 0000000..e4355f0 Binary files /dev/null and b/app/src/main/resources/school_project/levels/level26.level differ diff --git a/app/src/main/resources/school_project/levels/level27.level b/app/src/main/resources/school_project/levels/level27.level new file mode 100644 index 0000000..032baad --- /dev/null +++ b/app/src/main/resources/school_project/levels/level27.level @@ -0,0 +1 @@ +SMSÿÿÿÿð#ø#´!À!À#¼4ð3o€SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level28.level b/app/src/main/resources/school_project/levels/level28.level new file mode 100644 index 0000000..e6e3b4b --- /dev/null +++ b/app/src/main/resources/school_project/levels/level28.level @@ -0,0 +1 @@ +SMSÿÿÿÿÿÿ€2ôB#|3?€3ÿ€#ü&ÿðSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level29.level b/app/src/main/resources/school_project/levels/level29.level new file mode 100644 index 0000000..37f4982 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level29.level @@ -0,0 +1 @@ +SMSyïÿyàBø!ÀBø$ÿ#´#èSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level3.level b/app/src/main/resources/school_project/levels/level3.level new file mode 100644 index 0000000..e3c2d52 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level3.level @@ -0,0 +1 @@ +SMSÿ€#üàSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level30.level b/app/src/main/resources/school_project/levels/level30.level new file mode 100644 index 0000000..8024650 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level30.level @@ -0,0 +1 @@ +SMSý÷ß}÷߀ $«3Û€2ø€€"°2x"p2ì"ð2äSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level4.level b/app/src/main/resources/school_project/levels/level4.level new file mode 100644 index 0000000..e7cebe7 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level4.level @@ -0,0 +1 @@ +SMSÿÿ#ü#ü€1àSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level5.level b/app/src/main/resources/school_project/levels/level5.level new file mode 100644 index 0000000..d3adad1 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level5.level @@ -0,0 +1 @@ +SMS÷p€€1à#ü#üSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level6.level b/app/src/main/resources/school_project/levels/level6.level new file mode 100644 index 0000000..b6466fa --- /dev/null +++ b/app/src/main/resources/school_project/levels/level6.level @@ -0,0 +1 @@ +SMSÿ3ÿ€à1àSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level7.level b/app/src/main/resources/school_project/levels/level7.level new file mode 100644 index 0000000..165247f --- /dev/null +++ b/app/src/main/resources/school_project/levels/level7.level @@ -0,0 +1 @@ +SMS3ÿ2üà"°SME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level8.level b/app/src/main/resources/school_project/levels/level8.level new file mode 100644 index 0000000..56abddb --- /dev/null +++ b/app/src/main/resources/school_project/levels/level8.level @@ -0,0 +1 @@ +SMSÿÿÿ€2ü"°"°"p1à€1à"pSME \ No newline at end of file diff --git a/app/src/main/resources/school_project/levels/level9.level b/app/src/main/resources/school_project/levels/level9.level new file mode 100644 index 0000000..d636478 --- /dev/null +++ b/app/src/main/resources/school_project/levels/level9.level @@ -0,0 +1 @@ +SMSÿÿ߀1à1à3ÿ€#ü"°SME \ No newline at end of file