summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/states/StateEngage.java
blob: 32c4c42decbf1b24121dc8508f3bf00ab8278eae (plain)
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
package ch.asynk.rustanddust.game.states;

import ch.asynk.rustanddust.engine.Orientation;
import ch.asynk.rustanddust.ui.Position;
import ch.asynk.rustanddust.game.Hex;
import ch.asynk.rustanddust.game.Unit;
import ch.asynk.rustanddust.game.Order;
import ch.asynk.rustanddust.game.Ctrl.MsgType;
import ch.asynk.rustanddust.game.hud.ActionButtons.Buttons;

public class StateEngage extends StateCommon
{
    // selectedUnit -> fire leader
    // activeUnit -> target / break

    private boolean breakMove = false;

    @Override
    public void enterFrom(StateType prevState)
    {
        if (prevState == StateType.SELECT) {
            breakMove = false;
            if ((to != null) && (activeUnit() != null)) {
                // quick fire
                selectTarget(activeUnit(), to);
            }
            selectedUnit().showAttack();
            map.hexSelect(selectedHex);
        } else {
            breakMove = true;
            activate(null);
            ctrl.hud.actionButtons.show(Buttons.DONE.b);
            ctrl.hud.notify("Break Through possible", Position.MIDDLE_CENTER);
            map.unitsActivableShow();
            map.hexUnselect(selectedHex);
        }
    }

    @Override
    public boolean processMsg(MsgType msg, Object data)
    {
        switch(msg)
        {
            case OK:
                if (breakMove)
                    abortBreakMove();
                return true;
        }

        return false;
    }

    @Override
    public void touch(Hex hex)
    {
        Unit unit = hex.getUnit();

        if (!breakMove) {
            if (unit == selectedUnit())
                abort();
            else if ((activeUnit() == null) && map.unitsTargetContains(unit))
                selectTarget(unit, hex);
            else if (unit == activeUnit())
                engage();
            else if ((activeUnit() != null) && map.unitsActivableContains(unit))
                map.toggleAssist(unit);
        } else {
            if (activeUnit() == null) {
                if (map.unitsActivableContains(unit))
                    selectBreakUnit(unit);
            } else {
                Orientation o = Orientation.fromAdj(to, hex);
                if (o == Orientation.KEEP)
                    unselectBreakUnit();
                else
                    doBreakMove(o);
            }

        }
    }

    private void selectTarget(Unit unit, Hex hex)
    {
        to = hex;
        activate(unit);
        map.unitsTargetHide();
        activeUnit().showTarget();
        map.collectAssists(selectedUnit(), activeUnit(), ctrl.battle.getPlayer().units);
        map.unitsAssistShow();
    }

    private void engage()
    {
        activeUnit().hideTarget();
        selectedUnit().hideAttack();
        map.unitsAssistHide();
        map.hexUnselect(selectedHex);
        Order order = map.getEngageOrder(selectedUnit(), activeUnit());

        if (order.cost == 0)
            ctrl.postOrder(order, StateType.ENGAGE);
        else {
            order.cost = order.engagement.cost;
            ctrl.postOrder(order);
        }
    }

    private void abort()
    {
        map.unitsAssistHide();
        map.unitsTargetHide();
        activeUnit().hideTarget();
        selectedUnit().hideAttack();
        map.hexUnselect(selectedHex);
        map.unitsActivatedClear();
        ctrl.postActionAborted();
    }

    private void selectBreakUnit(Unit unit)
    {
        activate(unit);
        map.hexMoveShow(to);
        map.hexMoveShow(unit.getHex());
        map.hexDirectionsShow(to);
        map.unitsActivableHide();
    }

    private void unselectBreakUnit()
    {
        map.hexMoveHide(to);
        map.hexMoveHide(activeUnit().getHex());
        map.hexDirectionsHide(to);
        map.unitsActivableShow();
        activate(null);
    }

    private void doBreakMove(Orientation o)
    {
        map.hexMoveHide(to);
        map.hexMoveHide(activeUnit().getHex());
        map.hexDirectionsHide(to);
        map.pathsInit(activeUnit());
        map.pathsBuildShortest(to);
        map.pathsSetOrientation(o);
        ctrl.postOrder(map.getMoveOrder(activeUnit(), false));
    }

    private void abortBreakMove()
    {
        if (activeUnit() != null)
            map.hexMoveHide(activeUnit().getHex());
        map.hexMoveHide(to);
        map.hexDirectionsHide(to);
        map.unitsActivableHide();
        ctrl.postOrder(Order.END);
    }
}