package space.m0e.quga.oop.lab56maven.entities.micro; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Platform; import javafx.geometry.Bounds; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.util.Duration; import space.m0e.quga.oop.lab56maven.Main; 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.common.Item; 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.handlers.DwarfHandler; import java.util.*; 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; workstation.getDwarves().add(this); } @Override public String toString() { return String.format("%s{Name=%s, Age=%d, Ability=%s, HP: %f/%f, Items: [%s], Position=(%f,%f), MacroObject=%s}", this.getClass().getSimpleName(), this.getFullName(), this.getAge(), this.getAbility(), this.getHp(), this.getMaxHp(), this.getItems().entrySet().stream().map(item -> String.format("%s (%d)", item.getKey().toString(), item.getValue())).collect(Collectors.joining(", ")), getX(), getY(), getFortress()); } private Fortress fortress = null; private Workstation workstation = null; public Image spriteImage = new Image(Objects.requireNonNull(Immigrant.class.getResourceAsStream("dwarf.png")));; public Label getItemLabel() { return itemLabel; } private Label itemLabel = new Label(); 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.setHandler(new DwarfHandler(this)); setTimeline(new Timeline(new KeyFrame(Duration.millis(20), this.getHandler()))); getTimeline().setCycleCount(Animation.INDEFINITE); Platform.runLater(() -> { getGroup().getChildren().add(itemLabel); }); Platform.runLater(this::updateItemLabel); itemLabel.setStyle("-fx-text-fill: white; -fx-font-size: 16px; -fx-background-color: grey"); } public Dwarf() { this("Edzul", "Èrithbesmar", 35, 0, 0, Ability.values()[new Random().nextInt(Ability.values().length)]); } public Dwarf(Immigrant immigrant, Fortress fortress) { this(immigrant.getFirstName(), immigrant.getLastName(), immigrant.getAge(), immigrant.getX(), immigrant.getY(), immigrant.getAbility()); this.setItems(new HashMap<>(immigrant.getItems())); this.setHp(this.getHp()); this.setMaxHp(this.getMaxHp()); this.fortress = fortress; } public void updateItemLabel() { itemLabel.setText(this.getItems().entrySet().stream().map(item -> String.format("%s (%d)", item.getKey().toString(), item.getValue())).collect(Collectors.joining("|"))); } public Dwarf(String serializedData) { super(serializedData); } public Workstation findNearestWorkstation() { // System.out.println(fortress.getWorkstations()); ArrayList searchWorkstations = (ArrayList) fortress.getWorkstations().stream().filter(w -> w.getDwarves().size() <= 3 && w.getRequiredAbility() == this.getAbility() ).collect(Collectors.toList()); searchWorkstations.sort((o1, o2) -> (int) ((Math.abs(o1.getGroup().getLayoutX() - this.getX()) + Math.abs(o1.getGroup().getLayoutY() - this.getY())) - ((Math.abs(o2.getGroup().getLayoutX() - this.getX()) + Math.abs(o2.getGroup().getLayoutY() - this.getY()))))); if (searchWorkstations.isEmpty()) { return null; } else { return searchWorkstations.get(0); } } public ThroneRoom findNearestThroneRoom() { ArrayList searchThroneRooms = (ArrayList) fortress.getThroneRooms().stream().filter(w -> fortress.getInsideGroup().getChildren().contains(w.getGroup()) && w.getNobels().size() <= 3 ).collect(Collectors.toList()); searchThroneRooms.sort((o1, o2) -> (int) ((Math.abs(o1.getGroup().getLayoutX() - this.getX()) + Math.abs(o1.getGroup().getLayoutY() - this.getY())) - ((Math.abs(o2.getGroup().getLayoutX() - this.getX()) + Math.abs(o2.getGroup().getLayoutY() - this.getY()))))); if (searchThroneRooms.isEmpty()) { return null; } else { return searchThroneRooms.get(0); } } public Fortress findNearestFortressToTrade(Item materialToSell, int amount) { ArrayList searchFortresses = (ArrayList) Main.fortresses.clone(); searchFortresses.sort((o1, o2) -> { return (int) ((Math.abs(o1.getX() - this.getX()) + Math.abs(o1.getY() - this.getY())) - (Math.abs(o2.getX() - this.getX()) + Math.abs(o2.getY() - this.getY()))); }); return searchFortresses.stream().filter(f -> { if (f == this.fortress) return false; switch (materialToSell) { case WOOD -> { return f.getStoneCount() >= amount; } case STONE -> { return f.getWoodCount() >= amount; } } return false; }).findFirst().orElse(null); } // public Coordinates getCoordinatesToWorkstation(Workstation workstation) { // double workstationX = workstation.getGroup().getLayoutX() + workstation.getGroup().getBoundsInParent().getWidth() - this.getGroup().getBoundsInParent().getWidth(); // double workstationY = workstation.getGroup().getLayoutY() + workstation.getGroup().getBoundsInParent().getHeight() - this.getGroup().getBoundsInParent().getHeight(); // return new Coordinates(workstationX, workstationY); // } public Coordinates getCoordinatesToWorkstation(Workstation workstation) { // Отримання меж групи робочої станції в координатах сцени Bounds workstationBoundsInScene = workstation.getGroup().localToScene(workstation.getGroup().getBoundsInLocal()); // Отримання меж кореневої групи в координатах сцени Bounds rootBoundsInScene = this.getGroup().getScene().getRoot().localToScene(this.getGroup().getScene().getRoot().getBoundsInLocal()); // Обчислення відносних координат робочої станції відносно кореневої групи double workstationX = workstationBoundsInScene.getMinX() - rootBoundsInScene.getMinX(); double workstationY = workstationBoundsInScene.getMinY() - rootBoundsInScene.getMinY(); return new Coordinates(workstationX, workstationY); } // public Coordinates getCoordinatesToThroneRoom(ThroneRoom throneRoom) { // double throneRoomX = throneRoom.getGroup().getLayoutX() + throneRoom.getGroup().getBoundsInParent().getWidth() / 2 + this.getGroup().getBoundsInParent().getWidth(); // double throneRoomY = throneRoom.getGroup().getLayoutY() + throneRoom.getGroup().getBoundsInParent().getHeight() / 2 + this.getGroup().getBoundsInParent().getHeight(); // return new Coordinates(throneRoomX, throneRoomY); // } public Coordinates getCoordinatesToThroneRoom(ThroneRoom workstation) { // Отримання меж групи робочої станції в координатах сцени Bounds workstationBoundsInScene = workstation.getGroup().localToScene(workstation.getGroup().getBoundsInLocal()); // Отримання меж кореневої групи в координатах сцени Bounds rootBoundsInScene = this.getGroup().getScene().getRoot().localToScene(this.getGroup().getScene().getRoot().getBoundsInLocal()); // Обчислення відносних координат робочої станції відносно кореневої групи double workstationX = workstationBoundsInScene.getMinX() - rootBoundsInScene.getMinX(); double workstationY = workstationBoundsInScene.getMinY() - rootBoundsInScene.getMinY(); return new Coordinates(workstationX, workstationY); } public void free() { if (fortress == null) { return; } this.fortress.remove(this); fortress = null; } }