diff options
| author | Andriy Cherniy <qugalet@m0e.space> | 2024-06-15 03:04:36 +0300 |
|---|---|---|
| committer | Andriy Cherniy <qugalet@m0e.space> | 2024-06-15 03:04:36 +0300 |
| commit | 7356425dd4f0b09d984e794598c67cfaad351599 (patch) | |
| tree | 136422c006606a7e079c11fa8e1a89d6403842ef | |
| parent | 81d17026cd28445e8a4b8d3d44009bdf6de451ec (diff) | |
| download | oop-kursach-7356425dd4f0b09d984e794598c67cfaad351599.tar.gz oop-kursach-7356425dd4f0b09d984e794598c67cfaad351599.zip | |
tilepane + thread for dwarf
5 files changed, 195 insertions, 69 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 b337de8..ae203eb 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/Main.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/Main.java @@ -39,7 +39,7 @@ public class Main extends Application { Rectangle rect = new Rectangle(3000, 2000, Color.WHITE); root.getChildren().add(rect); - Fortress fortress = new Fortress("Eartha", 200, 200, new ArrayList<>(List.of(new Workstation("Torgrus"))), new ArrayList<>(List.of(new ThroneRoom("Halzorga")))); + Fortress fortress = new Fortress("Eartha", 150, 150, new ArrayList<>(List.of(new Workstation("Torgrus"))), new ArrayList<>(List.of(new ThroneRoom("Halzorga")))); fortresses.add(fortress); immigrants.add(new Immigrant("1", "0", 50, 0,0, Ability.MASONIST)); @@ -86,6 +86,23 @@ public class Main extends Application { }); } + case END -> { + immigrants.stream().filter(immigrant -> immigrant.isActive() && immigrant instanceof Dwarf).forEach(immigrant -> { + Dwarf dwarf = (Dwarf) immigrant; + if (dwarf.getFortress() != null) + Platform.runLater(() -> { + dwarf.setX(dwarf.getFortress().getX()-50); + dwarf.setY(dwarf.getFortress().getY()-50); + dwarf.setActive(false); + dwarf.free(); + }); + }); + } + + case M -> { + + } + case TAB -> { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Object list"); diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java b/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java index 584b367..9ab9114 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java @@ -15,7 +15,7 @@ import java.util.stream.Collectors; public class Fortress { private Image spriteImage = new Image(Objects.requireNonNull(Fortress.class.getResourceAsStream("fortress.jpeg")));; - private GridPane insideGroup = new GridPane(10, 10); + private TilePane insideGroup = new TilePane(10, 10); private Label nameLabel = new Label(); private Label countLabel = new Label(); @@ -29,7 +29,7 @@ public class Fortress { private double y; private VBox vBox = new VBox(); - private GridPane dwarvesContainer = new GridPane(10, 10); + private TilePane dwarvesContainer = new TilePane(10, 10); public Fortress(String name, double x, double y, ArrayList<Workstation> workstations, ArrayList<ThroneRoom> throneRooms) { this.x = x; @@ -40,39 +40,48 @@ public class Fortress { nameLabel.setStyle("-fx-text-fill: red; -fx-font-size: 16px;"); - RowConstraints insideRow = new RowConstraints(); - insideRow.setPrefHeight(200); - ColumnConstraints insideColumn = new ColumnConstraints(); - insideColumn.setPrefWidth(200); - for (int i = 0; i < 3; i++) { - insideGroup.getRowConstraints().add(insideRow); - insideGroup.getColumnConstraints().add(insideColumn); - } +// RowConstraints insideRow = new RowConstraints(); +// insideRow.setPrefHeight(200); +// ColumnConstraints insideColumn = new ColumnConstraints(); +// insideColumn.setPrefWidth(200); +// for (int i = 0; i < 3; i++) { +// insideGroup.getRowConstraints().add(insideRow); +// insideGroup.getColumnConstraints().add(insideColumn); +// } insideGroup.setBackground(new Background(new BackgroundImage(spriteImage, BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT))); + insideGroup.setPrefColumns(2); + insideGroup.setPrefRows(2); + insideGroup.setPrefTileHeight(200); + insideGroup.setPrefTileWidth(200); this.workstations = workstations; this.throneRooms = throneRooms; - int col = 0; - int row = 0; - for (Workstation w : this.workstations) { - if (col > 3) { - col = 0; - row++; - } - insideGroup.add(w.getGroup(), col, row); - col++; - } - for (ThroneRoom t : this.throneRooms) { - if (col > 3) { - col = 0; - row++; - } - insideGroup.add(t.getGroup(), col, row); - col++; - } + workstations.forEach(workstation -> insideGroup.getChildren().add(workstation.getGroup())); + throneRooms.forEach(throneRoom -> insideGroup.getChildren().add(throneRoom.getGroup())); + insideGroup.getChildren().add(dwarvesContainer); - insideGroup.add(dwarvesContainer, 2, 2); +// int col = 0; +// int row = 0; +// for (Workstation w : this.workstations) { +// if (col > 3) { +// col = 0; +// row++; +// } +// insideGroup.add(w.getGroup(), col, row); +// col++; +// } +// +// for (ThroneRoom t : this.throneRooms) { +// if (col > 3) { +// col = 0; +// row++; +// } +// insideGroup.add(t.getGroup(), col, row); +// col++; +// } +// +// insideGroup.add(dwarvesContainer, 2, 2); this.dwarvesContainer.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(3)))); // dwarvesContainer.setMinSize(100 * 3, 100 * 3); @@ -88,7 +97,14 @@ public class Fortress { public void add(Dwarf dwarf) { dwarves.add(dwarf); countLabel.setText(String.valueOf(dwarves.size())); - dwarvesContainer.add(dwarf.getGroup(), dwarves.size() % 3, dwarves.size() / 3); + dwarvesContainer.getChildren().add(dwarf.getGroup()); + } + + public void remove(Dwarf dwarf) { + dwarves.remove(dwarf); + countLabel.setText(String.valueOf(dwarves.size())); + dwarvesContainer.getChildren().remove(dwarf.getGroup()); + Main.root.getChildren().add(dwarf.getGroup()); } public void process() { diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java index d00f24f..5fab26b 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java @@ -6,6 +6,7 @@ import space.m0e.quga.oop.lab56maven.entities.common.Ability; import space.m0e.quga.oop.lab56maven.entities.common.Coordinates; import space.m0e.quga.oop.lab56maven.entities.macro.Fortress; import space.m0e.quga.oop.lab56maven.entities.macro.Workstation; +import space.m0e.quga.oop.lab56maven.threads.DwarfThread; import java.util.ArrayList; import java.util.Objects; @@ -14,12 +15,30 @@ import java.util.Random; import java.util.stream.Collectors; public class Dwarf extends Immigrant { + public Fortress getFortress() { + return fortress; + } + + public void setFortress(Fortress fortress) { + this.fortress = fortress; + } + + public Workstation getWorkstation() { + return workstation; + } + + public void setWorkstation(Workstation workstation) { + this.workstation = workstation; + } + private Fortress fortress = null; private Workstation workstation = null; public Image spriteImage = new Image(Objects.requireNonNull(Immigrant.class.getResourceAsStream("dwarf.png")));; public Dwarf(String firstName, String lastName, int age, double x, double y, Ability ability) { super(firstName, lastName, age, x, y, ability); getSprite().setImage(spriteImage); + this.setThread(new DwarfThread(this)); + this.getThread().start(); } public Dwarf() { @@ -50,43 +69,51 @@ public class Dwarf extends Immigrant { return new Coordinates(workstationX, workstationY); } - @Override - public void moveAI() { - if (fortress == null) { - fortress = findNearestFortress(); - fortress.getDwarves().add(this); - } - if (!fortress.getGroup().getBoundsInParent().contains(this.getGroup().getBoundsInParent())) { - super.moveAI(); - } else { - if (workstation == null) { - workstation = findNearestWorkstation(); - if (workstation == null) { - super.moveAI(); - } else { - Coordinates workstationToMove = this.getCoordinatesToWorkstation(workstation); - if (!workstationToMove.isThere(this)) { - double workstationX = workstationToMove.getX(); - double workstationY = workstationToMove.getY(); - - if (workstationX > this.getX()) { - this.setX(this.getX() + 1); - } - - if (workstationX < this.getX()) { - this.setX(this.getX() - 1); - } +// @Override +// public void moveAI() { +// if (fortress == null) { +// fortress = findNearestFortress(); +// fortress.getDwarves().add(this); +// } +// if (!fortress.getGroup().getBoundsInParent().contains(this.getGroup().getBoundsInParent())) { +// super.moveAI(); +// } else { +// if (workstation == null) { +// workstation = findNearestWorkstation(); +// if (workstation == null) { +// super.moveAI(); +// } else { +// Coordinates workstationToMove = this.getCoordinatesToWorkstation(workstation); +// if (!workstationToMove.isThere(this)) { +// double workstationX = workstationToMove.getX(); +// double workstationY = workstationToMove.getY(); +// +// if (workstationX > this.getX()) { +// this.setX(this.getX() + 1); +// } +// +// if (workstationX < this.getX()) { +// this.setX(this.getX() - 1); +// } +// +// if (workstationY > this.getY()) { +// this.setY(this.getY() + 1); +// } +// +// if (workstationY < this.getY()) { +// this.setY(this.getY() - 1); +// } +// } +// } +// } +// } +// } - if (workstationY > this.getY()) { - this.setY(this.getY() + 1); - } - - if (workstationY < this.getY()) { - this.setY(this.getY() - 1); - } - } - } - } + public void free() { + if (fortress == null) { + return; } + this.fortress.remove(this); + fortress = null; } } diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java index f91d0c2..34581f4 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java @@ -43,7 +43,11 @@ public class Immigrant implements Comparable<Immigrant>, Cloneable { private final Rectangle hpBar; private static final Image spriteImage = new Image(Objects.requireNonNull(Immigrant.class.getResourceAsStream("immigrant.png"))); - private ImmigrantThread thread = new ImmigrantThread(this); + public void setThread(Thread thread) { + this.thread = thread; + } + + private Thread thread; public Immigrant(String firstName, String lastName, int age, double x, double y, Ability ability) { this.firstName = firstName; @@ -327,7 +331,7 @@ public class Immigrant implements Comparable<Immigrant>, Cloneable { return immigrant; } - public ImmigrantThread getThread() { + public Thread getThread() { return thread; } } diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/threads/DwarfThread.java b/src/main/java/space/m0e/quga/oop/lab56maven/threads/DwarfThread.java new file mode 100644 index 0000000..a662a6a --- /dev/null +++ b/src/main/java/space/m0e/quga/oop/lab56maven/threads/DwarfThread.java @@ -0,0 +1,62 @@ +package space.m0e.quga.oop.lab56maven.threads; + +import javafx.application.Platform; +import space.m0e.quga.oop.lab56maven.Main; +import space.m0e.quga.oop.lab56maven.entities.common.Coordinates; +import space.m0e.quga.oop.lab56maven.entities.macro.Fortress; +import space.m0e.quga.oop.lab56maven.entities.micro.Dwarf; +import space.m0e.quga.oop.lab56maven.entities.micro.Immigrant; + +import java.util.concurrent.TimeUnit; + +public class DwarfThread extends Thread { + public Dwarf getDwarf() { + return dwarf; + } + + public void setDwarf(Dwarf dwarf) { + this.dwarf = dwarf; + } + + private Dwarf dwarf; + + public DwarfThread(Dwarf dwarf) { + super(); + this.setName(String.format("DwarfThread_%s", dwarf.getFullName())); + this.dwarf = dwarf; + } + + @Override + public void run() { + while (!isInterrupted()) { + if (dwarf != null && !dwarf.isActive()) { + try { + TimeUnit.MILLISECONDS.sleep(10); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } +// Fortress fortress = dwarf.findNearestFortress(); +// if (fortress != null) { +// Coordinates moveTo = dwarf.getCoordinatesToFortress(fortress); +// dwarf.move(moveTo); +// } + if (dwarf.getFortress() == null) { + Fortress fortress = dwarf.findNearestFortress(); + if (fortress != null) { + Coordinates moveTo = dwarf.getCoordinatesToFortress(fortress); + dwarf.move(moveTo); + if (moveTo.isThere(dwarf)) { + Platform.runLater(() -> { + dwarf.setFortress(fortress); + fortress.add(dwarf); + Main.root.getChildren().remove(dwarf.getGroup()); + }); + } + } + } + } + } + } + + +} |
