summaryrefslogtreecommitdiff
path: root/src/main/java/space/m0e/quga/oop/lab56maven/decisions/GoTradingDecision.java
blob: 784f06161337139582a760dfceb0f7e5811859b5 (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
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);
        }
    }
}