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
|
package space.m0e.quga.oop.lab56maven;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import space.m0e.quga.oop.lab56maven.dialogs.InsertDialog;
import space.m0e.quga.oop.lab56maven.entities.common.Ability;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class Main extends Application {
public static Group root = new Group();
public static ArrayList<Immigrant> immigrants = new ArrayList<>();
public static ArrayList<Fortress> fortresses = new ArrayList<>();
public static Label status = new Label();
@Override
public void start(Stage stage) throws IOException, InterruptedException {
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"))));
fortresses.add(fortress);
immigrants.add(new Immigrant("1", "0", 50, 0,0, Ability.MASONIST));
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.setOnKeyPressed(event -> {
switch (event.getCode()) {
case LEFT -> moveImmigrants(-10, 0);
case RIGHT -> moveImmigrants(10, 0);
case UP -> moveImmigrants(0, -10);
case DOWN -> moveImmigrants(0, 10);
case INSERT -> {
try {
InsertDialog.display();
} catch (IOException e) {
e.printStackTrace();
}
}
case DELETE -> {
Iterator<Immigrant> iterator = immigrants.iterator();
while (iterator.hasNext()) {
Immigrant immigrant = iterator.next();
if (immigrant.isActive()) {
iterator.remove();
root.getChildren().remove(immigrant.getGroup());
}
}
processImmigrants();
}
case ESCAPE -> {
immigrants.forEach(immigrant -> {
immigrant.setActive(false);
});
}
case TAB -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Object list");
alert.setHeaderText("Object list");
alert.setContentText(immigrants.stream().map(Immigrant::toString).collect(Collectors.joining("\n")));
alert.showAndWait();
}
}
});
stage.setTitle("Hello!");
stage.setScene(scene);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(() -> immigrants.forEach(immigrant -> {
immigrant.getThread().start();
}), 1, 1, TimeUnit.SECONDS);
stage.show();
}
private void moveImmigrants(double dx, double dy) {
immigrants.forEach(immigrant -> {
if (immigrant.isActive()) {
Platform.runLater(() -> {
immigrant.setX(immigrant.getX() + dx);
immigrant.setY(immigrant.getY() + dy);
});
}
});
}
public static void processImmigrants() {
status.setText("Active:");
immigrants.forEach(immigrant -> {
if (immigrant.isActive()) {
status.setText(status.getText() + " " + immigrant.getFullName());
}
});
}
public static void main(String[] args) {
launch();
}
}
|