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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
package space.m0e.quga.oop.lab56maven;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import space.m0e.quga.oop.lab56maven.dialogs.CountDialog;
import space.m0e.quga.oop.lab56maven.dialogs.InsertDialog;
import space.m0e.quga.oop.lab56maven.dialogs.SearchDialog;
import space.m0e.quga.oop.lab56maven.entities.common.Ability;
import space.m0e.quga.oop.lab56maven.entities.common.MiniMap;
import space.m0e.quga.oop.lab56maven.entities.common.Utils;
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 space.m0e.quga.oop.lab56maven.entities.micro.Nobel;
import space.m0e.quga.oop.lab56maven.handlers.DwarfHandler;
import space.m0e.quga.oop.lab56maven.handlers.ImmigrantHandler;
import java.io.*;
import java.util.*;
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 Group group = new Group();
public static ArrayList<Immigrant> immigrants = new ArrayList<>();
public static ArrayList<Fortress> fortresses = new ArrayList<>();
public static Label status = new Label();
public static MiniMap miniMap;
public static final double width = 5000;
public static final double height = 2812;
public static ImageView imgviewmap;
public static Rectangle rectMinimap;
@Override
public void start(Stage stage) throws IOException, InterruptedException {
Rectangle rect = new Rectangle(width, height, Color.WHITE);
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialFileName("serial.csv");
root.getChildren().add(rect);
fortresses.add(new Fortress("Eartha", 150, 150,
new ArrayList<>(List.of(
new Workstation("Torgrus", Ability.MASONIST),
new Workstation("Abbacus", Ability.WOODCUTTER)
)),
new ArrayList<>(List.of(
new ThroneRoom("Halzorga"))
)));
fortresses.add(new Fortress("Aboba", 1000, 150,
new ArrayList<>(List.of(
new Workstation("Torgrass", Ability.WOODCUTTER)
)),
new ArrayList<>(List.of(
))));
fortresses.add(new Fortress("Eldoria", 2300, 500,
new ArrayList<>(List.of(
new Workstation("Eldoria Workstation", Ability.MASONIST)
)),
new ArrayList<>(List.of(
new ThroneRoom("Eldoria Throne Room"))
)));
fortresses.add(new Fortress("Valeria", 800, 1600,
new ArrayList<>(List.of(
new Workstation("Valeria Workstation", Ability.WOODCUTTER)
)),
new ArrayList<>(List.of(
new ThroneRoom("Valeria Throne Room"))
)));
fortresses.add(new Fortress("Dunewood", 2700, 2100,
new ArrayList<>(List.of(
new Workstation("Dunewood Workstation", Ability.MASONIST)
)),
new ArrayList<>(List.of(
new ThroneRoom("Dunewood Throne Room"))
)));
fortresses.add(new Fortress("Glimmerstone", 3300, 200,
new ArrayList<>(List.of(
new Workstation("Glimmerstone Workstation", Ability.WOODCUTTER)
)),
new ArrayList<>(List.of(
new ThroneRoom("Glimmerstone Throne Room"))
)));
fortresses.add(new Fortress("Stormwatch", 4000, 1000,
new ArrayList<>(List.of(
new Workstation("Stormwatch Workstation", Ability.MASONIST)
)),
new ArrayList<>(List.of(
new ThroneRoom("Stormwatch Throne Room"))
)));
fortresses.get(0).setWoodCount(1000);
fortresses.get(1).setStoneCount(1000);
immigrants.add(new Immigrant("Melbil", "Lancerpaddle", 50, 0,50, Ability.COMMUNICATION));
immigrants.add(new Immigrant("Atír", "Razorpaddle", 50, 0,50, Ability.COMMUNICATION));
immigrants.add(new Immigrant("Onget", "Sabreletter", 52, 200,140, Ability.MASONIST));
immigrants.add(new Immigrant("Limul", "Rockrack", 52, 700,150, Ability.WOODCUTTER));
// for (int i = 0; i < 20; i++) {
// immigrants.add(new Immigrant())
// }
root.getChildren().add(status);
group.getChildren().add(root);
Scene scene = new Scene(group, width/4, height/4);
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 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 F -> {
try {
SearchDialog.display();
} catch (IOException e) {
e.printStackTrace();
}
}
case C -> {
try {
CountDialog.display();
} catch (IOException e) {
e.printStackTrace();
}
}
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();
}
case W -> {
if (root.getLayoutY() + 20 <= 0)
root.setLayoutY(root.getLayoutY()+20);
}
case S -> {
if (root.getLayoutY() - 20 - stage.getHeight() >= -height)
root.setLayoutY(root.getLayoutY()-20);
}
case A -> {
if (root.getLayoutX() + 20 <= 0)
root.setLayoutX(root.getLayoutX()+20);
}
case D -> {
if (root.getLayoutX() - 20 - stage.getWidth() >= -width)
root.setLayoutX(root.getLayoutX()-20);
}
case F5 -> {
File file = fileChooser.showSaveDialog(stage);
String finalSerializeData = ""; // "class,name,age,hp,max_hp,items,x,y\n";
finalSerializeData += immigrants.stream().map(Immigrant::serialize).collect(Collectors.joining("\n"));
finalSerializeData += "\n" + fortresses.stream().map(Fortress::serialize).collect(Collectors.joining("\n"));
System.out.println(finalSerializeData);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(finalSerializeData);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
case F6 -> {
File file = fileChooser.showOpenDialog(stage);
try {
immigrants.forEach(immigrant -> {
if (Main.root.getChildren().contains(immigrant.getGroup())) {
Main.root.getChildren().remove(immigrant.getGroup());
} else {
fortresses.forEach(fortress1 -> {
fortress1.removeFromContainer((Dwarf) immigrant);
fortress1.remove((Dwarf) immigrant);
});
}
immigrant.getTimeline().stop();
});
immigrants.clear();
StringBuilder finalDeserializeData = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
finalDeserializeData.append(line).append("\n");
}
Arrays.stream(finalDeserializeData.toString().split("\n")).forEach(data -> {
String[] params = data.split(",");
switch (params[0]) {
case "Immigrant" -> {
Immigrant immigrant = new Immigrant(data);
immigrant.getTimeline().play();
immigrants.add(immigrant);
}
case "Dwarf" -> {
Dwarf dwarf = new Dwarf(data);
dwarf.getTimeline().play();
immigrants.add(dwarf);
}
case "Nobel" -> {
Dwarf nobel = new Nobel(data);
nobel.getTimeline().play();
immigrants.add(nobel);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
root.setOnMouseClicked(event -> {
switch (event.getButton()) {
case SECONDARY -> {
try {
Utils.findImmigrants("Dwarves outside MacroObjects:", immigrant -> fortresses.stream().noneMatch(fortress1 -> fortress1.getDwarves().contains(immigrant)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
stage.setTitle("Hello!");
stage.setScene(scene);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(() -> immigrants.forEach(immigrant -> {
immigrant.getTimeline().play();
}), 1, 1, TimeUnit.SECONDS);
// isMiniMapVisible = !isMiniMapVisible;
// if (isMiniMapVisible){
//
// mainGroup.getChildren().add(miniMap);
// mainGroup.getChildren().add(miniMap.rect);
// UpdateTimer.play();
// }
// else{
// mainGroup.getChildren().remove(miniMap);
// mainGroup.getChildren().remove(miniMap.rect);
// UpdateTimer.stop();
// }
miniMap = new MiniMap(400, 225, stage.getWidth()/10, stage.getHeight()/10, root);
mapTimer = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Platform.runLater(() -> {
miniMap.updateMap();
miniMap.setHeight(height/10);
miniMap.setWidth(width/10);
miniMap.rect.setHeight(stage.getHeight()/10);
miniMap.rect.setWidth(stage.getWidth()/10);
});
miniMap.rect.setX(-root.getLayoutX() / 10);
miniMap.rect.setY(-root.getLayoutY() / 10);
}
}));
mapTimer.setCycleCount(Timeline.INDEFINITE);
group.getChildren().add(miniMap);
group.getChildren().add(miniMap.rect);
mapTimer.play();
stage.show();
}
public static Timeline mapTimer;
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();
}
}
|