package space.m0e.quga.oop.lab56maven.decisions; import javafx.application.Platform; 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 space.m0e.quga.oop.lab56maven.handlers.DwarfHandler; public class GoTradingDecision extends Decision { private Dwarf dwarf; private Item tradingItem; private int amountOfItem; public GoTradingDecision(int executeWhen, Dwarf dwarf, Item tradingItem, int amountOfItem) { super(executeWhen); this.dwarf = dwarf; this.tradingItem = tradingItem; this.amountOfItem = amountOfItem; } @Override public void cycle() { Fortress near = dwarf.findNearestFortressToTrade(tradingItem, amountOfItem); if (near != null) { dwarf.getFortress().removeFromContainer(dwarf); Coordinates nearCoordinates = dwarf.getCoordinatesToFortress(near); dwarf.move(nearCoordinates); if (nearCoordinates.isThere(dwarf)) { dwarf.getItems().remove(tradingItem); Item getItem = null; switch (tradingItem) { case WOOD -> { near.setWoodCount(near.getWoodCount() + amountOfItem); near.setStoneCount(near.getStoneCount() - amountOfItem); getItem = Item.STONE; } case STONE -> { near.setStoneCount(near.getStoneCount() + amountOfItem); near.setWoodCount(near.getWoodCount() - amountOfItem); getItem = Item.WOOD; } } if (getItem != null) { dwarf.getItems().put(getItem, amountOfItem); } Platform.runLater(dwarf::updateItemLabel); DwarfHandler dwarfHandler = (DwarfHandler) dwarf.getHandler(); dwarfHandler.setDecision(new GoToDwarvesContainerDecision(dwarfHandler.getTimer() + 1, dwarf)); setComplete(true); } } else { DwarfHandler dwarfHandler = (DwarfHandler) dwarf.getHandler(); dwarfHandler.setDecision(new GoToDwarvesContainerDecision(dwarfHandler.getTimer() + 1, dwarf)); setComplete(true); } } }