summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/screens/MenuScreen.java
blob: cc6d5678d5545df1656ce42fc53fc883210da772 (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
package ch.asynk.tankontank.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.Interpolation;

import ch.asynk.tankontank.TankOnTank;
import ch.asynk.tankontank.ui.Position;
import ch.asynk.tankontank.menu.MainMenu;
import ch.asynk.tankontank.menu.OptionsMenu;
import ch.asynk.tankontank.menu.ScenariosMenu;
import ch.asynk.tankontank.menu.TutorialsMenu;

public class MenuScreen implements Screen
{
    private final TankOnTank game;

    private final int OFFSET = 20;
    private final int V_WIDTH = 1600;
    private final int V_HEIGHT = 1125;
    private final int V_CENTER_X = 1000;
    private final int V_CENTER_Y = 890;

    private float percent;
    private float delay = 0.0f;
    private float dx;
    private float dy;
    private int[] xPath = { 369, 558, 747, 936, 1125, 1030, 936, 1125, 1314, 1408, 1597};
    private int[] yPath = { 565, 565, 565, 565,  565,  729, 892,  892,  892, 1056, 1056};
    private int n = xPath.length;

    private boolean ready;
    private boolean gameAssetsLoading;
    private Texture bg;

    private Sprite unit;
    private Sprite move;
    private Sprite from;
    private Sprite to;
    private Sprite geFlag;
    private Sprite usFlag;
    private Sprite lnl;
    private Sprite logo;

    private MainMenu mainMenu;
    private OptionsMenu optionsMenu;
    private ScenariosMenu scenariosMenu;
    private TutorialsMenu tutorialsMenu;

    private final MenuCamera camera;
    private final SpriteBatch batch;
    private Vector3 touch = new Vector3();

    public MenuScreen(final TankOnTank game)
    {
        this.game = game;
        this.batch = new SpriteBatch();

        float width = Gdx.graphics.getWidth();
        float height = Gdx.graphics.getHeight();

        this.camera = new MenuCamera(V_CENTER_X, V_CENTER_Y, V_WIDTH, V_HEIGHT);

        this.gameAssetsLoading = false;

        this.bg = game.manager.get("data/map_a.png", Texture.class);

        this.unit = new Sprite(game.menuAtlas.findRegion("unit"));
        this.move = new Sprite(game.menuAtlas.findRegion("move"));
        this.from = new Sprite(game.menuAtlas.findRegion("from"));
        this.to = new Sprite(game.menuAtlas.findRegion("to"));
        this.usFlag = new Sprite(game.menuAtlas.findRegion("us-flag"));
        this.geFlag = new Sprite(game.menuAtlas.findRegion("ge-flag"));
        this.lnl = new Sprite(game.menuAtlas.findRegion("lnl"));
        this.logo = new Sprite(game.menuAtlas.findRegion("logo"));

        this.mainMenu = new MainMenu(game.fontB, game.uiAtlas);
        this.optionsMenu = new OptionsMenu(game, game.fontB, game.uiAtlas);
        this.scenariosMenu = new ScenariosMenu(game, game.fontB, game.uiAtlas);
        this.tutorialsMenu = new TutorialsMenu(game, game.fontB, game.uiAtlas);

        this.game.config.battle = null;

        Gdx.input.setInputProcessor(new InputAdapter() {
            @Override
            public boolean touchDown(int x, int y, int pointer, int button)
            {
                camera.uiUnproject(x, y, touch);
                return hit(touch.x, touch.y);
            }
        });
    }

    private boolean hit(float x, float y)
    {
        if (mainMenu.hit(x, y)) {
            mainMenu.visible = false;
            showNextMenu();
            return true;
        } else if (optionsMenu.hit(x, y)) {
            mainMenu.visible = true;
            optionsMenu.visible = false;
            return true;
        } else if (scenariosMenu.hit(x, y)) {
            mainMenu.visible = true;
            scenariosMenu.visible = false;
            if (game.config.battle != null)
                startLoading();
            return true;
        } else if (tutorialsMenu.hit(x, y)) {
            mainMenu.visible = true;
            tutorialsMenu.visible = false;
            return true;
        }

        return false;
    }

    private void showNextMenu()
    {
        MainMenu.Menu menu = mainMenu.getMenu();

        if (menu == MainMenu.Menu.OPTIONS)
            optionsMenu.visible = true;
        else if (menu == MainMenu.Menu.SCENARIOS)
            scenariosMenu.visible = true;
        else if (menu == MainMenu.Menu.TUTORIALS)
            tutorialsMenu.visible = true;
    }

    private void startLoading()
    {
        mainMenu.visible = false;
        game.loadGameAssets();
        gameAssetsLoading = true;
    }

    private void gameAssetsLoadingCompleted()
    {
        TankOnTank.debug("LoadScreen", "assets ready : " + (Gdx.app.getJavaHeap()/1024.0f) + "KB");
        game.switchToGame();
        dispose();
    }

    @Override
    public void render(float delta)
    {
        float x = xPath[0];
        float y = yPath[0];
        if (gameAssetsLoading) {
            if (game.manager.update()) {
                delay += delta;
                if (delay >= 0.6f)
                    gameAssetsLoadingCompleted();
            }

            percent = Interpolation.linear.apply(percent, game.manager.getProgress(), 0.1f);
            int idx = (int) (percent * 10);
            float fraction = ((percent * 100 ) % 10 / 10);
            x = (xPath[idx] + ((xPath[idx + 1] - xPath[idx]) * fraction));
            y = (yPath[idx] + ((yPath[idx + 1] - yPath[idx]) * fraction));
        }

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(bg, 0, 0);
        from.draw(batch);
        to.draw(batch);
        usFlag.draw(batch);
        geFlag.draw(batch);
        for (int i = 1; i < (n - 1); i++)
            drawCentered(batch, move, xPath[i], yPath[i]);
        drawCentered(batch, unit, (int) (x + dx), (int) (y + dy));
        batch.end();

        batch.setProjectionMatrix(camera.uiCombined());
        batch.begin();
        batch.draw(logo, OFFSET, (camera.getScreenHeight() - logo.getRegionHeight() - OFFSET));
        batch.draw(lnl, (camera.getScreenWidth() - lnl.getRegionWidth() - (2 * OFFSET)), (2 * OFFSET));
        mainMenu.draw(batch);
        optionsMenu.draw(batch);
        scenariosMenu.draw(batch);
        tutorialsMenu.draw(batch);
        batch.end();
    }

    private void drawCentered(SpriteBatch batch, TextureRegion region, int x, int y)
    {
        batch.draw(region, (x - (region.getRegionWidth() / 2f)), (y - (region.getRegionHeight() / 2f)));
    }

    private void setCenteredPosition(Sprite sprite, int x, int y)
    {
        sprite.setPosition((x - (sprite.getWidth() / 2f)), (y - (sprite.getHeight() / 2f)));
    }

    private void update(int width, int height)
    {
        camera.updateViewport(width, height);
        Position.update(width, height);

        setCenteredPosition(from, xPath[0], yPath[0]);
        setCenteredPosition(to, xPath[n - 1], yPath[n - 1]);
        setCenteredPosition(usFlag, xPath[0], yPath[0]);
        setCenteredPosition(geFlag, xPath[n - 1], yPath[n - 1]);

        mainMenu.setPosition();
        optionsMenu.setPosition();
        scenariosMenu.setPosition();
        tutorialsMenu.setPosition();
    }

    @Override
    public void resize(int width, int height)
    {
        update(width, height);
    }

    @Override
    public void dispose()
    {
        mainMenu.dispose();
        optionsMenu.dispose();
        scenariosMenu.dispose();
        tutorialsMenu.dispose();
    }

    @Override
    public void show()
    {
        int width = (int) Gdx.graphics.getWidth();
        int height = (int) Gdx.graphics.getHeight();
        update(width, height);
    }

    @Override
    public void hide()
    {
        // TankOnTank.debug("MenuScreen", "hide()");
    }

    @Override
    public void pause()
    {
        // TankOnTank.debug("MenuScreen", "pause()");
    }

    @Override
    public void resume()
    {
        // TankOnTank.debug("MenuScreen", "resume()");
    }
}