summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/states/StateSelect.java
blob: 2a4f537509e1132ec66d2eeeaeed71a201a09c90 (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
package ch.asynk.rustanddust.game.states;

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

public class StateSelect extends StateCommon
{
    private boolean isEnemy;

    @Override
    public void enterFrom(StateType prevState)
    {
        clear();
    }

    @Override
    public boolean processMsg(MsgType msg, Object data)
    {
        switch(msg)
        {
            case OK:
                clear();
                ctrl.postTurnDone();
                return true;
            case PROMOTE:
                changeTo(StateType.PROMOTE);
                return true;
            case CANCEL:
                clear();
                return true;
        }

        return false;
    }

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

        if (!isEnemy && (selectedUnit() != null)) {
            if (map.movesContains(hex)) {
                // quick move
                to = hex;
                changeTo(StateType.MOVE);
                return;
            }
            if (map.unitsTargetContains(unit)) {
                // quick fire
                to = hex;
                activate(unit);
                changeTo(StateType.ENGAGE);
                return;
            }
        }

        hide();

        if ((unit == null) || hex.isOffMap()) {
            select(null);
            return;
        }

        isEnemy = ctrl.battle.getPlayer().isEnemy(unit);

        if (!isEnemy && (selectedUnit() == unit) && unit.canMove()) {
            if (unit.isHq() && (map.unitsActivableSize() > 1)) {
                ctrl.hud.notify("HQ activation", Position.MIDDLE_CENTER);
                to = null;
            } else
                to = hex;
            changeTo(StateType.MOVE);
        } else {
            select(hex, unit);
            ctrl.hud.notify(selectedUnit().toString());
        }
    }

    private void select(Hex hex, Unit unit)
    {
        selectedHex = hex;
        select(unit);
        RustAndDust.debug(String.format("  %s - %s", selectedUnit(), selectedHex));

        map.hexSelect(selectedHex);

        if (isEnemy && !cfg.showEnemyPossibilities)
            return;

        if(map.movesCollect(selectedUnit()) > 0) {
            map.collectMoveable(selectedUnit());
            if (cfg.showMoves) map.movesShow();
            if (cfg.showMoveAssists) map.unitsActivableShow();
            unit.hideActiveable();
        }

        if (map.collectTargets(selectedUnit(), (isEnemy ? ctrl.battle.getPlayer() : ctrl.battle.getOpponent()).units) > 0) {
            if (cfg.showTargets) map.unitsTargetShow();
            unit.hideActiveable();
        }

        ctrl.hud.actionButtons.show((ctrl.battle.getPlayer().canPromote(selectedUnit())) ? Buttons.PROMOTE.b : 0 );
    }

    private void changeTo(StateType nextState)
    {
        hide();
        ctrl.post(nextState);
    }

    private void hide()
    {
        if (selectedHex != null)
            map.hexUnselect(selectedHex);
        map.movesHide();
        map.unitsTargetHide();
        map.unitsActivableHide();
        ctrl.hud.actionButtons.hide();
    }

    private void clear()
    {
        hide();
        map.clearMoves();
        map.clearUnits();
        to = null;
        isEnemy = false;
        selectedHex = null;
        select(null);
        activate(null);
    }
}