summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/game/Hud.java
blob: 2f0e0c6e9b508e96dc500c6c84054c3520bd9720 (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
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.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

import ch.asynk.tankontank.engine.gfx.Image;

import ch.asynk.tankontank.TankOnTank;

class ActionBtn implements Disposable
{
    boolean enabled;
    private Image on;
    private Image off;

    public ActionBtn(TextureAtlas atlas, String off, String on)
    {
        this.enabled = false;
        this.on = new Image(atlas.findRegion(on));
        this.off = new Image(atlas.findRegion(off));
    }

    public void toggle()
    {
        enabled = !enabled;
    }

    public Image getImage()
    {
        return (enabled ? on : off);
    }

    public void setPosition(float x, float y)
    {
        on.setPosition(x, y);
        off.setPosition(x, y);
    }

    public boolean hit(float x, float y)
    {
        return ((x > on.getX()) && (x < on.getX() + on.getWidth()) && (y > on.getY()) && (y < on.getY() + on.getHeight()));
    }

    @Override
    public void dispose()
    {
        on.dispose();
        off.dispose();
    }

    public float getX() { return on.getX(); }
    public float getY() { return on.getY(); }
    public float getWidth() { return on.getWidth(); }
    public float getHeight() { return on.getHeight(); }
}

public class Hud implements Disposable
{
    private final TankOnTank game;

    private ActionBtn flagAct;
    private ActionBtn moveAct;
    private ActionBtn rotateAct;
    private ActionBtn attackAct;
    private ActionBtn cancelAct;

    private Rectangle rect;
    private float elapsed;

    public Hud(final TankOnTank game)
    {
        this.game = game;

        TextureAtlas atlas = game.manager.get("data/assets.atlas", TextureAtlas.class);

        flagAct = new ActionBtn(atlas, "us-flag", "ge-flag");
        moveAct = new ActionBtn(atlas, "act-move", "act-move-on");
        rotateAct = new ActionBtn(atlas, "act-rotate", "act-rotate-on");
        attackAct = new ActionBtn(atlas, "act-attack", "act-attack-on");
        cancelAct = new ActionBtn(atlas, "act-cancel", "act-cancel-on");

        flagAct.setPosition(5, (Gdx.graphics.getHeight() - flagAct.getHeight() - 5));
        moveAct.setPosition(flagAct.getX(), ( flagAct.getY() - moveAct.getHeight() - 5));
        rotateAct.setPosition(flagAct.getX(), ( moveAct.getY() - rotateAct.getHeight() - 5));
        attackAct.setPosition(flagAct.getX(), ( rotateAct.getY() - attackAct.getHeight() - 5));
        cancelAct.setPosition(flagAct.getX(), ( attackAct.getY() - cancelAct.getHeight() - 5));

        rect = new Rectangle(cancelAct.getX(), cancelAct.getY(), flagAct.getWidth(),
                (flagAct.getY() + flagAct.getHeight() - cancelAct.getY()));

        elapsed = 0f;
    }

    @Override
    public void dispose()
    {
        flagAct.dispose();
        moveAct.dispose();
        rotateAct.dispose();
        attackAct.dispose();
        cancelAct.dispose();
    }

    public void animate(float delta)
    {
        elapsed += delta;
        if (elapsed > 5f) {
            elapsed = 0f;
            flagAct.toggle();
        }
    }

    public void draw(Batch batch)
    {
        flagAct.getImage().draw(batch);
        moveAct.getImage().draw(batch);
        rotateAct.getImage().draw(batch);
        attackAct.getImage().draw(batch);
        cancelAct.getImage().draw(batch);
    }

    public boolean touchDown(float x, float y)
    {
        if (!rect.contains(x,y)) return false;

        if (moveAct.hit(x, y)) {
            moveAct.toggle();
        } else if (rotateAct.hit(x, y)) {
            rotateAct.toggle();
        } else if (attackAct.hit(x, y)) {
            attackAct.toggle();
        } else if (cancelAct.hit(x, y)) {
            cancelAct.toggle();
        }

        return true;
    }

    public boolean touchUp(float x, float y)
    {
        if (!rect.contains(x,y)) return false;

        if (moveAct.hit(x, y)) {
            moveAct.toggle();
        } else if (rotateAct.hit(x, y)) {
            rotateAct.toggle();
        } else if (attackAct.hit(x, y)) {
            attackAct.toggle();
        } else if (cancelAct.hit(x, y)) {
            cancelAct.toggle();
        }

        return true;
    }
}