summaryrefslogtreecommitdiff
path: root/src/main/java/space/m0e/quga/oop/lab56maven/decisions/GoToWorkstationDecision.java
blob: 9002fc53439251aca21aa971c8f966cd97f56298 (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
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.macro.Workstation;
import space.m0e.quga.oop.lab56maven.entities.micro.Dwarf;
import space.m0e.quga.oop.lab56maven.handlers.DwarfHandler;

public class GoToWorkstationDecision extends Decision {

    private Dwarf dwarf;

    public GoToWorkstationDecision(int executeWhen, Dwarf dwarf) {
        super(executeWhen);
        this.dwarf = dwarf;
    }

    @Override
    public void cycle() {
        Workstation near = dwarf.findNearestWorkstation();
        if (near != null) {
            Platform.runLater(dwarf::updateItemLabel);
            dwarf.getFortress().removeFromContainer(dwarf);
            Coordinates nearCoordinates = dwarf.getCoordinatesToWorkstation(near);
            dwarf.move(nearCoordinates);
            if (nearCoordinates.isThere(dwarf)) {
                dwarf.setWorkstation(near);

                DwarfHandler dwarfHandler = (DwarfHandler) dwarf.getHandler();
                dwarfHandler.setDecision(new WorkAtWorkstationDecision(dwarfHandler.getTimer() + 1, dwarf));
                near.getCountLabel().setText(String.valueOf(near.getDwarves().size()));
                setComplete(true);
            }
        }
    }
}