summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/menu/PlayMenu.java
blob: da38dbb79db56e01c8f6fd6b00726b4534de2cc7 (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
package ch.asynk.rustanddust.menu;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;

import ch.asynk.rustanddust.ui.Label;
import ch.asynk.rustanddust.ui.Bg;
import ch.asynk.rustanddust.ui.Patch;
import ch.asynk.rustanddust.ui.OkCancel;
import ch.asynk.rustanddust.RustAndDust;

public class PlayMenu extends Patch
{
    public static int PADDING = 40;
    public static int BTN_PADDING = 10;
    public static int TITLE_PADDING = 30;
    public static int VSPACING = 20;

    private final RustAndDust game;
    private final BitmapFont font;

    private Label title;
    private Label gameMode;
    private Label gameModeValue;
    private float gameModeWidth;
    private int battleIdx;
    private Label battle;
    private Label battleValue;
    protected Bg okBtn;
    protected Bg cancelBtn;
    private OkCancel okCancel;

    public boolean launch;

    public PlayMenu(RustAndDust game, BitmapFont font, TextureAtlas atlas)
    {
        super(atlas.createPatch("typewriter"));
        this.game = game;
        this.font = font;
        this.title = new Label(font);
        this.title.write("- Play");
        this.gameMode = new Label(font);
        this.gameMode.write("Game mode");
        this.gameModeValue = new Label(font);
        this.okBtn = new Bg(atlas.findRegion("ok"));
        this.cancelBtn = new Bg(atlas.findRegion("cancel"));
        this.battle = new Label(font);
        this.battle.write("Scenario");
        this.battleValue = new Label(font);
        this.okCancel = new OkCancel(font, atlas);

        if (game.config.battle == null) {
            battleIdx = 0;
            game.config.battle = game.factory.battles[0];
        } else {
            for (int i = 0; i < game.factory.battles.length; i++) {
                if (game.config.battle == game.factory.battles[i]) {
                    battleIdx = i;
                    break;
                }
            }
        }
        battleValue.write(game.config.battle.getName());

        float w = 0;
        for(int i = game.config.gameMode.i; ;) {
            gameModeValue.write(game.config.gameMode.s);
            if (w < gameModeValue.getWidth())
                w = gameModeValue.getWidth();
            game.config.gameMode = game.config.gameMode.next();
            if (i == game.config.gameMode.i) break;
        }
        this.gameModeValue.write(game.config.gameMode.s);
        this.gameModeWidth = w + 10 + gameMode.getWidth();

        this.visible = false;
        this.launch = false;
    }

    public void setPosition()
    {
        float h = (title.getHeight() + TITLE_PADDING + (2 * PADDING));
        h += (gameMode.getHeight() + VSPACING);
        h += (battle.getHeight());

        float w = gameModeWidth + (2 * PADDING);

        float x = position.getX(w);
        float y = position.getY(h);
        setPosition(x, y, w, h);

        okBtn.setPosition((x + w - okBtn.getWidth() + BTN_PADDING), (y - BTN_PADDING));
        cancelBtn.setPosition((x - BTN_PADDING), okBtn.getY());

        y += PADDING;
        x += PADDING;
        float dy = (VSPACING + battle.getHeight());

        battle.setPosition(x, y);
        battleValue.setPosition((x + battle.getWidth() + 10), y);
        y += dy;
        gameMode.setPosition(x, y);
        gameModeValue.setPosition((x + gameMode.getWidth() + 10), y);
        y += dy;

        y += (TITLE_PADDING - VSPACING);
        title.setPosition(x, y);
    }

    @Override
    public boolean hit(float x, float y)
    {
        if (okCancel.hit(x, y)) {
            this.visible = true;
            okCancel.visible = false;
            return false;
        }

        if (!visible) return false;

        if (okBtn.hit(x, y)) {
            return apply();
        } else if (cancelBtn.hit(x, y)) {
            return true;
        } else if (gameMode.hit(x, y) || gameModeValue.hit(x, y)) {
            cycleGameMode();
        } else if (battle.hit(x, y) || battleValue.hit(x, y)) {
            cycleBattle();
        }

        return false;
    }

    private boolean apply() {
        if (!game.config.gameModeImplemented()) {
            this.visible = false;
            okCancel.show(String.format("'%s' Game Mode not implemented yet.", game.config.gameMode.s));
            okCancel.noCancel();
        } else
            this.launch = true;
        return true;
    }

    private void cycleGameMode()
    {
        game.config.gameMode = game.config.gameMode.next();
        float fx = gameModeValue.getX();
        float fy = gameModeValue.getY();
        gameModeValue.write(game.config.gameMode.s);
        gameModeValue.setPosition(fx, fy);
    }

    private void cycleBattle()
    {
        battleIdx += 1;
        if (battleIdx >= game.factory.battles.length)
            battleIdx = 0;
        game.config.battle = game.factory.battles[battleIdx];
        float fx = battleValue.getX();
        float fy = battleValue.getY();
        battleValue.write(game.config.battle.getName());
        battleValue.setPosition(fx, fy);
    }

    @Override
    public void dispose()
    {
        super.dispose();
        title.dispose();
        gameMode.dispose();
        gameModeValue.dispose();
        battle.dispose();
        battleValue.dispose();
        okBtn.dispose();
        cancelBtn.dispose();
        okCancel.dispose();
    }

    @Override
    public void draw(Batch batch)
    {
        okCancel.draw(batch);

        if (!visible) return;
        super.draw(batch);
        title.draw(batch);
        gameMode.draw(batch);
        gameModeValue.draw(batch);
        battle.draw(batch);
        battleValue.draw(batch);
        okBtn.draw(batch);
        cancelBtn.draw(batch);
    }
}