summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/hud/UnitDock.java
blob: a34049d68d221349675d964752513c2a6978c61b (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
package ch.asynk.rustanddust.game.hud;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.HdpiUtils;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;

import ch.asynk.rustanddust.RustAndDust;
import ch.asynk.rustanddust.engine.gfx.Animation;
import ch.asynk.rustanddust.engine.Orientation;
import ch.asynk.rustanddust.game.Player;
import ch.asynk.rustanddust.game.Ctrl;
import ch.asynk.rustanddust.game.Unit;
import ch.asynk.rustanddust.game.UnitList;
import ch.asynk.rustanddust.ui.Bg;
import ch.asynk.rustanddust.ui.Position;

public class UnitDock extends Bg implements Animation
{
    private static final float STEP = 5f;
    private static final float BOUNCE_SPEED = 5;
    private static final float SCISSORS_BOTTOM = 50f;
    private final Ctrl ctrl;

    private int n;
    private float y;
    private float to;
    private float dx;
    private float dy;
    private float step;
    private float scale;
    private boolean show;
    private boolean mvtDone;
    public Unit selectedUnit;
    private Bg selected;
    private UnitList units;
    private Vector3 point;
    private Matrix4 saved;
    private Matrix4 transform;
    private Rectangle scaledRect;
    private Rectangle scissors;

    public UnitDock(RustAndDust game, float padding)
    {
        super(game.factory.getHudRegion(game.factory.DISABLED));
        this.ctrl = game.ctrl;
        this.padding = padding;
        this.mvtDone = true;
        this.point = new Vector3();
        this.saved = new Matrix4();
        this.transform = new Matrix4();
        this.scaledRect = new Rectangle();
        this.scissors = new Rectangle();
        this.selected = new Bg(game.factory.getHudRegion(game.factory.ENABLED));
        this.visible = false;
        this.dx = 0f;
        this.dy = 0f;
        this.scale = Math.max((Gdx.graphics.getHeight() * 0.0005f), 0.4f);
    }

    @Override
    public void translate(float _dx, float _dy)
    {
        this.y += _dy;
        if (!visible) return;
        super.translate(_dx, _dy);
        for (Unit unit : units)
            unit.translate(_dx, _dy);
        to = position.getX(rect.width * scale);
        compute();
    }

    private void compute()
    {
        transform.idt();
        transform.translate((rect.x + dx), (rect.y + dy + rect.height), 0).scale(scale, scale, 0).translate(-rect.x, - (rect.y + rect.height), 0);
        point.set(rect.x, rect.y, 0).mul(transform);
        scaledRect.x = point.x;
        scaledRect.y = point.y;
        point.set((rect.x + rect.width), (rect.y + rect.height), 0).mul(transform);
        scaledRect.width = point.x - scaledRect.x;
        scaledRect.height = point.y - scaledRect.y;
        scissors.set(0, SCISSORS_BOTTOM, Gdx.graphics.getWidth(), (y - SCISSORS_BOTTOM));
    }

    public void setPosition(Position position, float y)
    {
        if (this.position == position)
            return;
        this.position = position;
        this.y = y;
        this.step = (position.isLeft() ? STEP : -STEP);
        this.mvtDone = true;
        this.visible = false;
        this.dx = 0f;
        scissors.set(0, SCISSORS_BOTTOM, Gdx.graphics.getWidth(), (y - SCISSORS_BOTTOM));
    }

    @Override
    public void dispose()
    {
        super.dispose();
    }

    @Override
    public boolean hit(float x, float y)
    {
        if (!visible || !scaledRect.contains(x, y))
            return false;
        int i = (int) ((scaledRect.y + scaledRect.height - y) / (scaledRect.height / units.size()));
        selectedUnit = units.get(i);
        selected.setPosition(selectedUnit.getX() - padding, selectedUnit.getY() - padding, selectedUnit.getWidth() + (2 * padding), selectedUnit.getHeight() + (2 * padding));
        return true;
    }

    public void drag(int dx, int dy)
    {
        this.dy += dy;
        compute();
    }

    public void hide()
    {
        if (!visible) return;
        resize();
        to = rect.x;

        show = false;
        mvtDone = false;
        selectedUnit = null;
    }

    public void show()
    {
        if (!resize())
            return;
        if (dy != 0) {
            dy = 0;
            compute();
        }
        to = position.getX(rect.width * scale);

        show = true;
        mvtDone = false;
        selectedUnit = null;
        visible = true;
    }

    private boolean resize()
    {
        Player player = ctrl.battle.getPlayer();
        int count = player.reinforcement();
        if (count == 0) {
            n = 0;
            return false;
        }

        if ((count == n) && (units == player.reinforcement))
            return true;

        n = count;
        units = player.reinforcement;
        rect.width = units.get(0).getWidth() + (2 * padding);
        rect.height = ((units.get(0).getHeight() * n) + ((n + 1) * padding));
        float scaledWidth = (rect.width * scale);
        to = position.getX(scaledWidth);
        rect.x = to + (position.isLeft() ? -scaledWidth : scaledWidth);
        rect.y = y - rect.height;

        float px = rect.x;
        float py = rect.y + rect.height;
        float ph = units.get(0).getHeight();
        for (Unit unit : units) {
            py -= (ph + padding);
            // unit.setPosition(px, py, Orientation.SOUTH.r());
            unit.centerOn((px + (rect.width / 2)), py + (ph / 2));
            unit.setRotation(position.isLeft() ? Orientation.NORTH.r() : Orientation.SOUTH.r());
        }

        return true;
    }

    @Override
    public boolean animate(float delta)
    {
        if (!visible) return true;
        if (mvtDone) return true;

        float x = (rect.x + dx);
        if (show) {
            if ((position.isLeft() && (x < to)) || (!position.isLeft() && x > to))
                dx += step;
            else {
                dx = (to - rect.x);
                mvtDone = true;
            }
        } else {
            if ((position.isLeft() && (x > to)) || (!position.isLeft() && x < to))
                dx -= step;
            else {
                dx = (to - rect.x);
                mvtDone = true;
                visible = false;
            }
        }

        compute();
        return false;
    }

    @Override
    public void draw(Batch batch)
    {
        if (!visible) return;

        float top = scaledRect.y + scaledRect.height;
        if ((int)top != (int)y) {
            if (top < y) {
                this.dy += Math.min(BOUNCE_SPEED, (y - top));
                compute();
            } else if (scaledRect.y > SCISSORS_BOTTOM) {
                this.dy -= Math.min(BOUNCE_SPEED, (scaledRect.y - SCISSORS_BOTTOM));
                compute();
            }
        }

        saved.set(batch.getTransformMatrix());
        batch.setTransformMatrix(transform);

        // batch.flush();
        Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);
        HdpiUtils.glScissor((int)scissors.x, (int)scissors.y, (int)scissors.width, (int)scissors.height);

        super.draw(batch);
        if (selectedUnit != null) selected.draw(batch);
        for (Unit unit : units) unit.draw(batch);

        batch.setTransformMatrix(saved);

        Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
        // batch.flush();
    }

    @Override
    public void drawDebug(ShapeRenderer shapes)
    {
        if (!visible) return;

        saved.set(shapes.getTransformMatrix());
        shapes.setTransformMatrix(transform);

        shapes.rect(rect.x, rect.y, rect.width, rect.height);

        shapes.setTransformMatrix(saved);

        shapes.rect(scissors.x, scissors.y, scissors.width, scissors.height);
    }
}