summaryrefslogtreecommitdiff
path: root/src/main/java/space/m0e/quga/oop/lab56maven/entities
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/space/m0e/quga/oop/lab56maven/entities')
-rw-r--r--src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java76
-rw-r--r--src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java99
-rw-r--r--src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java8
3 files changed, 115 insertions, 68 deletions
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;
}
}