summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/Hud.java
blob: e53dcace9caa6306422e16ce0991dfea4e2efc3f (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
321
322
323
324
325
326
package ch.asynk.rustanddust.game;

import java.util.ArrayDeque;

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.graphics.glutils.ShapeRenderer;

import ch.asynk.rustanddust.engine.gfx.Animation;
import ch.asynk.rustanddust.ui.Position;
import ch.asynk.rustanddust.ui.Bg;
import ch.asynk.rustanddust.ui.Msg;
import ch.asynk.rustanddust.ui.OkCancel;
import ch.asynk.rustanddust.ui.Widget;
import ch.asynk.rustanddust.game.hud.PlayerInfo;
import ch.asynk.rustanddust.game.hud.ActionButtons;
import ch.asynk.rustanddust.game.hud.StatisticsPanel;
import ch.asynk.rustanddust.game.hud.EngagementPanel;
import ch.asynk.rustanddust.game.hud.OptionsPanel;

import ch.asynk.rustanddust.RustAndDust;

public class Hud implements Disposable, Animation
{
    public static final float OFFSET = 10f;
    public static final float NOTIFY_DURATION = 2f;
    private static final float CLOSE_DELAY = 0.8f;

    private final RustAndDust game;
    private final Ctrl ctrl;

    public PlayerInfo playerInfo;
    public ActionButtons actionButtons;

    private float delay;
    private boolean delayOn;
    private Msg msg;
    private Bg optionsBtn;
    private OptionsPanel optionsPanel;
    private StatisticsPanel stats;
    private EngagementPanel engagement;
    private OkCancel okCancel;
    private ArrayDeque<Widget> dialogs = new ArrayDeque<Widget>();

    public enum OkCancelAction
    {
        EXIT_BOARD,
        ABORT_TURN,
        END_DEPLOYMENT,
        EXIT_BATTLE,
    }
    private OkCancelAction okCancelAction;

    public Hud(final RustAndDust game)
    {
        this.game = game;
        this.ctrl = game.ctrl;
        this.delayOn = false;

        TextureAtlas hudAtlas = game.factory.hudAtlas;
        playerInfo = new PlayerInfo(game);
        actionButtons = new ActionButtons(game);
        actionButtons.hide();
        msg = new Msg(game.font, game.bgPatch, 20f);
        okCancel = new OkCancel(game.font, game.bgPatch, game.factory.getHudRegion(game.factory.ACT_DONE), game.factory.getHudRegion(game.factory.ACT_ABORT));
        optionsBtn = new Bg(game.factory.getHudRegion(game.factory.ACT_OPTIONS));
        optionsPanel = new OptionsPanel(game);
        stats = new StatisticsPanel(game);
        engagement = new EngagementPanel(game);
    }

    @Override
    public void dispose()
    {
        playerInfo.dispose();
        actionButtons.dispose();
        msg.dispose();
        okCancel.dispose();
        optionsBtn.dispose();
        optionsPanel.dispose();
        stats.dispose();
        engagement.dispose();
    }

    public void resize(int left, int bottom, int width, int height)
    {
        Position.update(left, bottom, width, height);
        playerInfo.updatePosition();
        actionButtons.updatePosition();
        msg.updatePosition();
        okCancel.updatePosition();
        optionsBtn.setPosition(ctrl.battle.getHudPosition().verticalMirror().horizontalMirror());
        optionsPanel.updatePosition();
        stats.updatePosition();
        engagement.updatePosition();
    }

    public void update()
    {
        Position position = ctrl.battle.getHudPosition();
        playerInfo.update(ctrl.battle.getPlayer(), position);
        actionButtons.update(position.horizontalMirror());
        optionsBtn.setPosition(position.verticalMirror().horizontalMirror());
    }

    @Override
    public boolean animate(float delta)
    {
        if (delayOn) {
            delay -= delta;
            if (delay < 0f) {
                delayOver();
            }
        }
        msg.animate(delta);
        playerInfo.animate(delta);
        engagement.animate(delta);
        return false;
    }

    public void draw(Batch batch, boolean debug)
    {
        draw(batch);
        if (debug)
            game.font.draw(batch, String.format("FPS: %d - %s", Gdx.graphics.getFramesPerSecond(), ctrl.map.getStats()), 80, 25);
    }

    @Override
    public void draw(Batch batch)
    {
        playerInfo.draw(batch);
        actionButtons.draw(batch);
        msg.draw(batch);
        okCancel.draw(batch);
        optionsBtn.draw(batch);
        optionsPanel.draw(batch);
        stats.draw(batch);
        engagement.draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        playerInfo.drawDebug(debugShapes);
        actionButtons.drawDebug(debugShapes);
        msg.drawDebug(debugShapes);
        okCancel.drawDebug(debugShapes);
        optionsBtn.drawDebug(debugShapes);
        optionsPanel.drawDebug(debugShapes);
        stats.drawDebug(debugShapes);
        engagement.drawDebug(debugShapes);
    }

    public void pushNotify(String s)
    {
        notify(s, NOTIFY_DURATION, Position.TOP_CENTER, true);
    }

    public void notify(String s)
    {
        notify(s, NOTIFY_DURATION, Position.TOP_CENTER, false);
    }

    public void notify(String s, Position pos)
    {
        notify(s, NOTIFY_DURATION, pos, false);
    }

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

    public boolean drag(float x, float y, int dx, int dy)
    {
        return playerInfo.drag(x, y, dx, dy);
    }

    public boolean hit(float x, float y, boolean isInAnimation)
    {
        if (optionsBtn.hit(x, y)) {
            if (toggleOptionsPanel())
                game.playType();
            return true;
        }

        if (dialogs.size() > 0) {
            Widget dialog = dialogs.peek();
            if (dialog.hit(x, y)) {
                closeDialog();
                return true;
            }
            return false;
        }

        if (isInAnimation)
            return false;

        if (actionButtons.hit(x, y)) {
            game.playType();
            return true;
        } else if (playerInfo.hit(x, y)) {
            game.playType();
            return true;
        }

        return false;
    }

    private void closeDialog()
    {
        Widget dialog = dialogs.pop();
        dialog.visible = false;

        if (dialog == okCancel) {
            boolean ok = false;
            if (okCancel.ok) {
                ok = true;
                game.playEnter();
            } else
                game.playType();
            if (ok) {
                if (this.okCancelAction == OkCancelAction.EXIT_BATTLE)
                    ctrl.postEvent(Ctrl.EventType.EXIT_BATTLE);
                else
                    ctrl.sendMsg(Ctrl.MsgType.OK);
            } else
                ctrl.sendMsg(Ctrl.MsgType.CANCEL);
        } else if (dialog == stats) {
            game.playEnter();
            ctrl.postEvent(Ctrl.EventType.EXIT_BATTLE);
        } else
            game.playType();

        if (dialogs.size() > 0)
            dialogs.peek().visible = true;
        else
            ctrl.blockMap = false;
    }

    public void notifyAnimationsDone()
    {
        delay = CLOSE_DELAY;
        delayOn = true;
    }

    private void delayOver()
    {
        delayOn = false;
        Widget dialog = dialogs.peek();
        if (dialog == engagement)
            closeDialog();
    }

    public boolean dialogActive()
    {
        return (dialogs.size() > 0);
    }

    private void pushDialog(Widget dialog)
    {
        ctrl.blockMap = true;
        if (dialogs.size() != 0)
            dialogs.peek().visible = false;
        dialogs.push(dialog);
    }

    private boolean toggleOptionsPanel()
    {
        if (dialogs.size() > 0) {
            if (dialogs.peek() != optionsPanel)
                return false;
            optionsPanel.close();
            closeDialog();
        } else {
            optionsPanel.show();
            pushDialog(optionsPanel);
        }
        return true;
    }

    public void askExitBoard()
    {
        this.okCancelAction = OkCancelAction.EXIT_BOARD;
        okCancel.show("Do you want this unit to escape the battle field ?");
        pushDialog(okCancel);
    }

    public void askEndOfTurn()
    {
        this.okCancelAction = OkCancelAction.ABORT_TURN;
        okCancel.show("You still have Action Points left.\nEnd your Turn anyway ?");
        pushDialog(okCancel);
    }

    public void askEndDeployment()
    {
        this.okCancelAction = OkCancelAction.END_DEPLOYMENT;
        okCancel.show("Deployment unit count reached.\nEnd Deployment phase ?");
        pushDialog(okCancel);
    }

    public void askExitBattle()
    {
        this.okCancelAction = OkCancelAction.EXIT_BATTLE;
        okCancel.show("Exit this battle ?");
        pushDialog(okCancel);
    }

    public void engagementSummary(Engagement e)
    {
        engagement.show(e, Position.BOTTOM_CENTER, game.config.fxVolume);
        pushDialog(engagement);
    }

    public void victory(Player winner, Player loser)
    {
        stats.show(winner, loser, Position.MIDDLE_CENTER);
        pushDialog(stats);
    }
}