summaryrefslogtreecommitdiff
path: root/src/main/java/space/m0e/quga/oop/lab56maven/handlers
diff options
context:
space:
mode:
authorAndriy Cherniy <qugalet@m0e.space>2024-06-17 02:59:32 +0300
committerAndriy Cherniy <qugalet@m0e.space>2024-06-17 02:59:32 +0300
commit3401d742ca53dce6bb1464cc12f94d107cfc285d (patch)
tree4640d50fbbb738e6ae63dac87c0ade6bd21a9523 /src/main/java/space/m0e/quga/oop/lab56maven/handlers
parentfdfd7040a283b15d7c2346b6a20bda64900244b1 (diff)
downloadoop-kursach-3401d742ca53dce6bb1464cc12f94d107cfc285d.tar.gz
oop-kursach-3401d742ca53dce6bb1464cc12f94d107cfc285d.zip
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Diffstat (limited to 'src/main/java/space/m0e/quga/oop/lab56maven/handlers')
-rw-r--r--src/main/java/space/m0e/quga/oop/lab56maven/handlers/DwarfHandler.java141
-rw-r--r--src/main/java/space/m0e/quga/oop/lab56maven/handlers/ImmigrantHandler.java52
-rw-r--r--src/main/java/space/m0e/quga/oop/lab56maven/handlers/NobelHandler.java72
3 files changed, 265 insertions, 0 deletions
diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/handlers/DwarfHandler.java b/src/main/java/space/m0e/quga/oop/lab56maven/handlers/DwarfHandler.java
new file mode 100644
index 0000000..7ba5466
--- /dev/null
+++ b/src/main/java/space/m0e/quga/oop/lab56maven/handlers/DwarfHandler.java
@@ -0,0 +1,141 @@
+package space.m0e.quga.oop.lab56maven.handlers;
+
+import javafx.application.Platform;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import space.m0e.quga.oop.lab56maven.Main;
+import space.m0e.quga.oop.lab56maven.decisions.*;
+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.micro.Dwarf;
+
+import java.util.Objects;
+import java.util.Random;
+
+public class DwarfHandler implements EventHandler<ActionEvent> {
+ public Dwarf getDwarf() {
+ return dwarf;
+ }
+
+ public int getTimer() {
+ return timer;
+ }
+
+ private int timer = 0;
+
+ public Decision getDecision() {
+ return decision;
+ }
+
+ public void setDecision(Decision decision) {
+ this.decision = decision;
+ }
+
+ private Decision decision;
+ private static final String[] availableDecisions = new String[]{
+ "GoToWorkstation",
+ "GoToDwarvesContainer",
+ "GoToThroneRoom",
+ "GoTrading"
+ };
+ private static String lastDecision;
+
+ public void setDwarf(Dwarf dwarf) {
+ this.dwarf = dwarf;
+ }
+
+ private Dwarf dwarf;
+
+ public DwarfHandler(Dwarf dwarf) {
+ super();
+// this.setName(String.format("DwarfThread_%s", dwarf.getFullName()));
+ this.dwarf = dwarf;
+ }
+
+ @Override
+ public void handle(ActionEvent e) {
+ if (dwarf != null && !dwarf.isActive()) {
+// try {
+// TimeUnit.MILLISECONDS.sleep(10);
+// } catch (InterruptedException e) {
+// break;
+// }
+ timer++;
+ if (dwarf.getFortress() == null) {
+ Fortress fortress = dwarf.findNearestFortress();
+ if (fortress != null) {
+ Coordinates moveTo = dwarf.getCoordinatesToFortress(fortress);
+ dwarf.move(moveTo);
+ if (moveTo.isThere(dwarf)) {
+ Platform.runLater(() -> {
+ dwarf.setFortress(fortress);
+ fortress.add(dwarf);
+ Main.root.getChildren().remove(dwarf.getGroup());
+ });
+ }
+ }
+ } else {
+ if (decision == null) {
+ String decisionName = availableDecisions[new Random().nextInt(availableDecisions.length)];
+ while (Objects.equals(lastDecision, decisionName)) {
+ decisionName = availableDecisions[new Random().nextInt(availableDecisions.length)];
+ }
+ lastDecision = decisionName;
+ switch (decisionName) {
+ case "GoToWorkstation" -> {
+ if (dwarf.getAge() > 12 && dwarf.getAge() < 70 && dwarf.getAbility() != Ability.COMMUNICATION ) {
+ decision = new GoToWorkstationDecision(new Random().nextInt(timer + 200, timer + 500), dwarf);
+ }
+ }
+ case "GoToDwarvesContainer" -> {
+ if (dwarf.getFortress().contains(dwarf))
+ break;
+ decision = new GoToDwarvesContainerDecision(new Random().nextInt(timer + 200, timer + 500), dwarf);
+ }
+ case "GoToThroneRoom" -> {
+ if (dwarf.getAge() > 35 && dwarf.getAge() < 70) {
+ decision = new GoToThroneRoomDecision(new Random().nextInt(timer + 200, timer + 500), dwarf);
+ }
+ }
+ case "GoTrading" -> {
+ if (dwarf.getAbility() == Ability.COMMUNICATION) {
+ Fortress fortress = dwarf.getFortress();
+ Item itemToTrade;
+ int amountToTrade;
+ if (fortress.getWoodCount() > 0 && fortress.getWoodCount() >= fortress.getStoneCount()) {
+ itemToTrade = Item.WOOD;
+ amountToTrade = new Random().nextInt(1, fortress.getWoodCount() / 2);
+ fortress.setWoodCount(fortress.getWoodCount() - amountToTrade);
+ } else if (fortress.getStoneCount() > 0 && fortress.getStoneCount() >= fortress.getWoodCount()) {
+ itemToTrade = Item.STONE;
+ amountToTrade = new Random().nextInt(1, fortress.getStoneCount() / 2);
+ fortress.setStoneCount(fortress.getStoneCount() - amountToTrade);
+ } else {
+ break;
+ }
+ if (dwarf.getItems().containsKey(itemToTrade)) {
+ dwarf.getItems().put(itemToTrade, dwarf.getItems().get(itemToTrade) + amountToTrade);
+ } else {
+ dwarf.getItems().put(itemToTrade, amountToTrade);
+ }
+ Platform.runLater(dwarf::updateItemLabel);
+ decision = new GoTradingDecision(new Random().nextInt(timer + 200, timer + 500), dwarf, itemToTrade, amountToTrade);
+ }
+ }
+ }
+ }
+ else {
+ if (decision.getExecuteWhen() < timer) {
+ decision.cycle();
+ }
+ if (decision.isComplete())
+ decision = null;
+ }
+ }
+ }
+ }
+
+
+}
diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/handlers/ImmigrantHandler.java b/src/main/java/space/m0e/quga/oop/lab56maven/handlers/ImmigrantHandler.java
new file mode 100644
index 0000000..fe9a3f0
--- /dev/null
+++ b/src/main/java/space/m0e/quga/oop/lab56maven/handlers/ImmigrantHandler.java
@@ -0,0 +1,52 @@
+package space.m0e.quga.oop.lab56maven.handlers;
+
+import javafx.animation.Timeline;
+import javafx.application.Platform;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import space.m0e.quga.oop.lab56maven.Main;
+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.micro.Dwarf;
+import space.m0e.quga.oop.lab56maven.entities.micro.Immigrant;
+
+public class ImmigrantHandler implements EventHandler<ActionEvent> {
+ public Immigrant getImmigrant() {
+ return immigrant;
+ }
+
+ public void setImmigrant(Immigrant immigrant) {
+ this.immigrant = immigrant;
+ }
+
+ private Immigrant immigrant;
+
+ public ImmigrantHandler(Immigrant immigrant) {
+ super();
+ this.immigrant = immigrant;
+ }
+
+ @Override
+ public void handle(ActionEvent e) {
+ if (immigrant != null && !immigrant.isActive()) {
+ Fortress fortress = immigrant.findNearestFortress();
+ if (fortress != null) {
+ Coordinates moveTo = immigrant.getCoordinatesToFortress(fortress);
+ immigrant.move(moveTo);
+ if (moveTo.isThere(immigrant)) {
+ Platform.runLater(() -> {
+ immigrant.getTimeline().stop();
+ Dwarf dwarf = new Dwarf(immigrant, fortress);
+ dwarf.getTimeline().play();
+ Main.immigrants.remove(immigrant);
+ Main.immigrants.add(dwarf);
+ fortress.add(dwarf);
+ Main.root.getChildren().remove(immigrant.getGroup());
+ });
+ }
+ }
+ }
+ }
+
+
+}
diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/handlers/NobelHandler.java b/src/main/java/space/m0e/quga/oop/lab56maven/handlers/NobelHandler.java
new file mode 100644
index 0000000..abb84a2
--- /dev/null
+++ b/src/main/java/space/m0e/quga/oop/lab56maven/handlers/NobelHandler.java
@@ -0,0 +1,72 @@
+package space.m0e.quga.oop.lab56maven.handlers;
+
+import javafx.application.Platform;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import space.m0e.quga.oop.lab56maven.Main;
+import space.m0e.quga.oop.lab56maven.decisions.Decision;
+import space.m0e.quga.oop.lab56maven.decisions.GoToDwarvesContainerDecision;
+import space.m0e.quga.oop.lab56maven.decisions.GoToThroneRoomDecision;
+import space.m0e.quga.oop.lab56maven.decisions.GoToWorkstationDecision;
+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.ThroneRoom;
+import space.m0e.quga.oop.lab56maven.entities.micro.Dwarf;
+import space.m0e.quga.oop.lab56maven.entities.micro.Nobel;
+
+import java.util.Objects;
+import java.util.Random;
+
+public class NobelHandler implements EventHandler<ActionEvent> {
+ public Nobel getNobel() {
+ return nobel;
+ }
+
+ private int timer = 0;
+
+ public void setNobel(Nobel nobel) {
+ this.nobel = nobel;
+ }
+
+ private Nobel nobel;
+
+ public NobelHandler(Nobel nobel) {
+ super();
+ this.nobel = nobel;
+ }
+
+ @Override
+ public void handle(ActionEvent event) {
+ if (nobel != null && !nobel.isActive()) {
+ timer++;
+ if (nobel.getFortress() == null) {
+ Fortress fortress = nobel.findNearestFortress();
+ if (fortress != null) {
+ Coordinates moveToFortress = nobel.getCoordinatesToFortress(fortress);
+ nobel.move(moveToFortress);
+ if (moveToFortress.isThere(nobel)) {
+ Platform.runLater(() -> {
+ nobel.setFortress(fortress);
+ fortress.add(nobel);
+ fortress.removeFromContainer(nobel);
+ });
+ }
+
+ }
+ } else {
+ ThroneRoom throneRoom = nobel.findNearestThroneRoom();
+ if (throneRoom != null) {
+ Coordinates moveToThroneRoom = nobel.getCoordinatesToThroneRoom(throneRoom);
+ nobel.move(moveToThroneRoom);
+ if (moveToThroneRoom.isThere(nobel)) {
+ Platform.runLater(() -> {
+ throneRoom.add(nobel);
+ });
+ }
+ }
+ }
+ }
+ }
+
+
+}