summaryrefslogtreecommitdiff
path: root/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java
blob: a36f66aad3f4024d1c7c1bd747ea49a0942a543f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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<Workstation> searchWorkstations = (ArrayList<Workstation>) 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<ThroneRoom> searchThroneRooms = (ArrayList<ThroneRoom>) 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<Fortress> searchFortresses = (ArrayList<Fortress>) 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;
    }
}