diff options
Diffstat (limited to 'src/main/java/space/m0e/quga/oop/lab56maven/Main.java')
| -rw-r--r-- | src/main/java/space/m0e/quga/oop/lab56maven/Main.java | 110 |
1 files changed, 102 insertions, 8 deletions
diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/Main.java b/src/main/java/space/m0e/quga/oop/lab56maven/Main.java index 1549ff5..25ba617 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/Main.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/Main.java @@ -10,18 +10,25 @@ import javafx.scene.input.KeyCode; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; +import javafx.stage.FileChooser; import javafx.stage.Stage; +import space.m0e.quga.oop.lab56maven.dialogs.CountDialog; import space.m0e.quga.oop.lab56maven.dialogs.InsertDialog; import space.m0e.quga.oop.lab56maven.dialogs.SearchDialog; import space.m0e.quga.oop.lab56maven.entities.common.Ability; +import space.m0e.quga.oop.lab56maven.entities.common.Utils; import space.m0e.quga.oop.lab56maven.entities.macro.Fortress; import space.m0e.quga.oop.lab56maven.entities.macro.ThroneRoom; import space.m0e.quga.oop.lab56maven.entities.macro.Workstation; import space.m0e.quga.oop.lab56maven.entities.micro.Dwarf; import space.m0e.quga.oop.lab56maven.entities.micro.Immigrant; +import space.m0e.quga.oop.lab56maven.entities.micro.Nobel; +import space.m0e.quga.oop.lab56maven.threads.DwarfThread; +import space.m0e.quga.oop.lab56maven.threads.ImmigrantThread; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.concurrent.Executors; @@ -35,9 +42,17 @@ public class Main extends Application { public static ArrayList<Fortress> fortresses = new ArrayList<>(); public static Label status = new Label(); + public static final double width = 3555; + public static final double height = 2000; + @Override public void start(Stage stage) throws IOException, InterruptedException { - Rectangle rect = new Rectangle(3000, 2000, Color.WHITE); + Rectangle rect = new Rectangle(width, height, Color.WHITE); +// rect.setStroke(Color.BLUE); +// rect.setStrokeWidth(10); + + FileChooser fileChooser = new FileChooser(); + fileChooser.setInitialFileName("serial.csv"); root.getChildren().add(rect); Fortress fortress = new Fortress("Eartha", 150, 150, new ArrayList<>(List.of(new Workstation("Torgrus"))), new ArrayList<>(List.of(new ThroneRoom("Halzorga")))); @@ -47,13 +62,8 @@ public class Main extends Application { immigrants.getFirst().setX(100); immigrants.getFirst().setY(100); -// BorderPane borderPane = new BorderPane(); -// borderPane.setCenter(root); -// borderPane.setTop(status); - -// root.setStyle("-fx-background-color:rgb(186,153,122,0.7);"); root.getChildren().add(status); - Scene scene = new Scene(root, 1500, 700); + Scene scene = new Scene(root, width/4, height/4); scene.setOnKeyPressed(event -> { switch (event.getCode()) { @@ -123,6 +133,90 @@ public class Main extends Application { alert.setContentText(immigrants.stream().map(Immigrant::toString).collect(Collectors.joining("\n"))); alert.showAndWait(); } + + case W -> { + if (root.getLayoutY() + 20 <= 0) + root.setLayoutY(root.getLayoutY()+20); + } + + case S -> { + if (root.getLayoutY() - 20 - stage.getHeight() >= -height) + root.setLayoutY(root.getLayoutY()-20); + } + + case A -> { + if (root.getLayoutX() + 20 <= 0) + root.setLayoutX(root.getLayoutX()+20); + } + + case D -> { + if (root.getLayoutX() - 20 - stage.getWidth() >= -width) + root.setLayoutX(root.getLayoutX()-20); + } + + case F5 -> { + File file = fileChooser.showSaveDialog(stage); + String finalSerializeData = ""; // "class,name,age,hp,max_hp,items,x,y\n"; + finalSerializeData += immigrants.stream().map(Immigrant::serialize).collect(Collectors.joining("\n")); + finalSerializeData += "\n" + fortresses.stream().map(Fortress::serialize).collect(Collectors.joining("\n")); + System.out.println(finalSerializeData); + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write(finalSerializeData); + writer.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + case F6 -> { + File file = fileChooser.showOpenDialog(stage); + try { + immigrants.forEach(immigrant -> { + if (Main.root.getChildren().contains(immigrant.getGroup())) { + Main.root.getChildren().remove(immigrant.getGroup()); + } else { + fortresses.forEach(fortress1 -> { + fortress1.removeFromContainer((Dwarf) immigrant); + fortress1.remove((Dwarf) immigrant); + }); + } + immigrant.getThread().interrupt(); + }); + immigrants.clear(); + StringBuilder finalDeserializeData = new StringBuilder(); + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line; + while ((line = reader.readLine()) != null) { + finalDeserializeData.append(line).append("\n"); + } + Arrays.stream(finalDeserializeData.toString().split("\n")).forEach(data -> { + String[] params = data.split(","); + switch (params[0]) { + case "Immigrant" -> { + Immigrant immigrant = new Immigrant(data); + immigrant.setThread(new ImmigrantThread(immigrant)); + immigrant.getThread().start(); + immigrants.add(immigrant); + } + case "Dwarf" -> { + Dwarf dwarf = new Dwarf(data); + dwarf.setThread(new DwarfThread(dwarf)); + dwarf.getThread().start(); + immigrants.add(dwarf); + } + case "Nobel" -> { + Dwarf nobel = new Nobel(data); + nobel.setThread(new DwarfThread(nobel)); + nobel.getThread().start(); + immigrants.add(nobel); + } + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } } }); |
