summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/game/Hud.java
blob: f1de5eb998f1634ab043350566f73c86a5b69f61 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package ch.asynk.tankontank.game;

import com.badlogic.gdx.Gdx;

import com.badlogic.gdx.utils.Disposable;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Rectangle;

import ch.asynk.tankontank.engine.gfx.Image;
import ch.asynk.tankontank.game.State.StateType;
import ch.asynk.tankontank.game.hud.Msg;
import ch.asynk.tankontank.game.hud.Bg;
import ch.asynk.tankontank.game.hud.Button;
import ch.asynk.tankontank.game.hud.TextImage;
import ch.asynk.tankontank.game.hud.UnitDock;
import ch.asynk.tankontank.game.hud.Dialog;
import ch.asynk.tankontank.game.hud.Position;

import ch.asynk.tankontank.TankOnTank;

public class Hud implements Disposable
{
    private static final float OFFSET =10f;
    private static final float PADDING = 5f;

    private final TankOnTank game;
    private final Ctrl ctrl;

    private Bg actionsBg;
    private Msg msg;

    private Button btn;
    private Object hit;
    public Button moveBtn;
    public Button rotateBtn;
    public Button promoteBtn;
    public Button attackBtn;
    public Button checkBtn;
    public Button cancelBtn;

    private Image flag;
    private Image usFlag;
    private Image geFlag;
    private TextImage turns;
    private TextImage aps;
    private TextImage reinforcement;
    private UnitDock unitDock;
    public Dialog dialog;

    private Vector2 corner;

    public Hud(final Ctrl ctrl, final TankOnTank game)
    {
        this.game = game;
        this.ctrl = ctrl;
        this.corner = new Vector2((Gdx.graphics.getWidth() - OFFSET), OFFSET);

        TextureAtlas atlas = game.factory.hudAtlas;

        moveBtn = new Button(atlas, "btn-move");
        rotateBtn = new Button(atlas, "btn-rotate");
        promoteBtn = new Button(atlas, "btn-promote");
        attackBtn = new Button(atlas, "btn-attack");
        checkBtn = new Button(atlas, "btn-check");
        cancelBtn = new Button(atlas, "btn-cancel");
        actionsBg = new Bg(atlas.findRegion("disabled"));

        msg = new Msg(game.skin.getFont("default-font"), atlas.findRegion("disabled"));

        usFlag = new Image(atlas.findRegion("us-flag"));
        geFlag = new Image(atlas.findRegion("ge-flag"));
        turns = new TextImage(atlas.findRegion("turns"), game.skin.getFont("default-font"), "0");
        aps = new TextImage(atlas.findRegion("aps"), game.skin.getFont("default-font"), "0");
        reinforcement = new TextImage(atlas.findRegion("reinforcement"), game.skin.getFont("default-font"), "0");
        unitDock = new UnitDock(ctrl, atlas.findRegion("disabled"), atlas.findRegion("reinforcement-selected"));

        dialog = new Dialog(game.skin.getFont("default-font"), atlas.findRegion("disabled"), atlas);

        float x = OFFSET;
        float y = (Gdx.graphics.getHeight() - OFFSET);
        usFlag.setPosition(x, (y - usFlag.getHeight()));
        geFlag.setPosition(x, (y - geFlag.getHeight()));
        turns.setPosition((usFlag.getX() + usFlag.getWidth() + 10), usFlag.getY());
        aps.setPosition((turns.getX() + turns.getWidth() + 10), turns.getY());
        aps.setTextPosition((aps.getX() + aps.getWidth() - 15), (aps.getY() + aps.getHeight() - 20));
        reinforcement.setPosition(x, usFlag.getY() - reinforcement.getHeight() - 0);
        reinforcement.setTextPosition((reinforcement.getX() + 5), (reinforcement.getY() + reinforcement.getHeight() - 20));
        unitDock.setTopLeft(OFFSET, reinforcement.getY() - 5);
    }

    @Override
    public void dispose()
    {
        moveBtn.dispose();
        rotateBtn.dispose();
        promoteBtn.dispose();
        attackBtn.dispose();
        checkBtn.dispose();
        cancelBtn.dispose();
        actionsBg.dispose();
        msg.dispose();

        turns.dispose();
        aps.dispose();
        usFlag.dispose();
        geFlag.dispose();
        reinforcement.dispose();
    }

    public void changeState(StateType from, StateType to)
    {
        if (to != StateType.ENTRY);
            unitDock.hide();

        if ((to == StateType.SELECT) || (to == StateType.ENTRY))
            reinforcement.blocked = false;
        else
            reinforcement.blocked = true;
    }

    public void update()
    {
        unitDock.hide();
        turns.write("" + ctrl.player.getTurn());
        aps.write("" + ctrl.player.getAp());
        int r = ctrl.player.getReinforcement().size();
        if (r == 0) {
            reinforcement.visible = false;
        } else {
            reinforcement.visible = true;
            reinforcement.write("" + r);
        }

        if (ctrl.player.getFaction() == Army.GE)
            flag = geFlag;
        else
            flag = usFlag;
    }

    public void animate(float delta)
    {
        msg.animate(delta);
        unitDock.animate(delta);
    }

    public void draw(Batch batch)
    {
        flag.draw(batch);
        turns.draw(batch);
        aps.draw(batch);
        reinforcement.draw(batch);
        unitDock.draw(batch);

        actionsBg.draw(batch);
        moveBtn.draw(batch);
        rotateBtn.draw(batch);
        promoteBtn.draw(batch);
        attackBtn.draw(batch);
        checkBtn.draw(batch);
        cancelBtn.draw(batch);
        msg.draw(batch);
        dialog.draw(batch);
    }

    public Unit getDockUnit()
    {
        return (Unit) unitDock.selectedPawn;
    }

    public void pushNotify(String s)
    {
        notify(s, 1, Position.MIDDLE_CENTER, true);
    }

    public void notify(String s)
    {
        notify(s, 1, Position.MIDDLE_CENTER, false);
    }

    public void notify(String s, float duration, Position position, boolean push)
    {
        if (push) msg.pushWrite(s, duration, position);
        else msg.write(s, 1, position);
    }

    private float setButton(Button btn, float x, float y)
    {
        // btn.setOff();
        btn.visible = true;
        btn.setPosition(x, y);
        return (y + btn.getHeight() + PADDING);
    }

    public void show(boolean promote, boolean rotate, boolean move, boolean attack, boolean check, boolean cancel)
    {
        float x =  (corner.x - checkBtn.getWidth());
        float y =  corner.y;

        if (move)   y = setButton(moveBtn, x, y);
        else moveBtn.hide();
        if (rotate) y = setButton(rotateBtn, x, y);
        else rotateBtn.hide();
        if (attack) y = setButton(attackBtn, x, y);
        else attackBtn.hide();
        if (promote) y = setButton(promoteBtn, x, y);
        else promoteBtn.hide();
        if (cancel) y = setButton(cancelBtn, x, y);
        else cancelBtn.hide();
        if (check)  y = setButton(checkBtn, x, y);
        else checkBtn.hide();

        actionsBg.set((x - PADDING), (corner.y - PADDING), (checkBtn.getWidth() + (2 * PADDING)), (y - corner.y));
    }

    public void hide()
    {
        moveBtn.hide();
        rotateBtn.hide();
        promoteBtn.hide();
        attackBtn.hide();
        checkBtn.hide();
        cancelBtn.hide();
        actionsBg.set(0, 0, 0, 0);
    }

    public void hideUnitDock()
    {
        unitDock.hide();
    }

    public boolean touchDown(float x, float y)
    {
        btn = null;
        hit = null;

        if (turns.hit(x,y))
            hit = turns;
        else if (unitDock.hit(x, y))
            hit = unitDock;
        else if (reinforcement.hit(x, y))
            hit = reinforcement;
        else if (dialog.hit(x, y))
            hit = dialog;
        else if (actionsBg.hit(x,y)) {
            if (moveBtn.hit(x, y))
                btn = moveBtn;
            else if (rotateBtn.hit(x, y))
                btn = rotateBtn;
            else if (promoteBtn.hit(x, y))
                btn = promoteBtn;
            else if (attackBtn.hit(x, y))
                btn = attackBtn;
            else if (checkBtn.hit(x, y))
                btn = checkBtn;
            else if (cancelBtn.hit(x, y))
                btn = cancelBtn;
        } else
            return false;

        if (btn != null)
            btn.setDown();

        return true;
    }

    public boolean touchUp(float x, float y)
    {
        if (btn != null) {
            if (actionsBg.hit(x, y)) {
                if ((btn == moveBtn) && moveBtn.hit(x, y))
                    ctrl.setState(StateType.MOVE);
                else if ((btn == rotateBtn) && rotateBtn.hit(x, y))
                    ctrl.setState(StateType.ROTATE);
                else if ((btn == promoteBtn) && promoteBtn.hit(x, y))
                    ctrl.setState(StateType.PROMOTE);
                else if ((btn == attackBtn) && attackBtn.hit(x, y))
                    ctrl.setState(StateType.ATTACK);
                else if ((btn == checkBtn) && checkBtn.hit(x, y))
                    ctrl.done();
                else if ((btn == cancelBtn) && cancelBtn.hit(x, y)) {
                    notify("Action canceled");
                    ctrl.abort();
                } else
                    btn.setOff();
            } else
                btn.setOff();
            btn = null;
        } else if (hit != null) {
            if ((hit == turns) && turns.hit(x, y))
                askEndTurn();
            else if ((hit == reinforcement) && reinforcement.hit(x, y))
                unitDock.toggle();
            else if ((hit == unitDock) && unitDock.hit(x, y))
                ctrl.setState(StateType.ENTRY);
            else if ((hit == dialog) && dialog.hit(x, y))
                leaveEndTurn();
            hit = null;
        } else
            return false;

        return true;
    }

    private void askEndTurn()
    {
        ctrl.blockMap = true;
        dialog.show("You still have Action Points left. End your Turn ?", Position.MIDDLE_CENTER);
    }

    private void leaveEndTurn()
    {
        dialog.visible = false;
        ctrl.blockMap = false;
        if (dialog.ok)
            ctrl.endPlayerTurn();
    }
}