DEADSOFTWARE

Fix json load
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / menu / MenuRenderer.java
1 package ru.deadsoftware.cavedroid.menu;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.utils.Array;
5 import ru.deadsoftware.cavedroid.CaveGame;
6 import ru.deadsoftware.cavedroid.GameScreen;
7 import ru.deadsoftware.cavedroid.game.GameItems;
8 import ru.deadsoftware.cavedroid.game.GameSaver;
9 import ru.deadsoftware.cavedroid.menu.objects.Button;
10 import ru.deadsoftware.cavedroid.misc.AppState;
11 import ru.deadsoftware.cavedroid.misc.Assets;
12 import ru.deadsoftware.cavedroid.misc.Renderer;
14 public class MenuRenderer extends Renderer {
16 public Array<Button> menuMainBtns;
17 public Array<Button> menuNGBtns;
19 public MenuRenderer(int width) {
20 super(width, width * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
21 //main menu
22 menuMainBtns = new Array<Button>();
23 menuMainBtns.add(new Button("New game", getWidth() / 2 - 100, getHeight() / 4));
24 menuMainBtns.add(new Button("Load game", getWidth() / 2 - 100, getHeight() / 4 + 30, GameSaver.exists() ? 1 : 0));
25 menuMainBtns.add(new Button("Quit", getWidth() / 2 - 100, getHeight() / 4 + 60));
26 //new game menu
27 menuNGBtns = new Array<Button>();
28 menuNGBtns.add(new Button("Survival", getWidth() / 2 - 100, getHeight() / 4, 0));
29 menuNGBtns.add(new Button("Creative", getWidth() / 2 - 100, getHeight() / 4 + 30));
30 menuNGBtns.add(new Button("Back", getWidth() / 2 - 100, getHeight() / 4 + 60));
32 }
34 public void buttonClicked(Button button) {
35 if (button.getLabel().toLowerCase().equals("new game")) {
36 CaveGame.STATE = AppState.MENU_NEW_GAME;
37 } else if (button.getLabel().toLowerCase().equals("load game")) {
38 CaveGame.STATE = AppState.GOTO_LOAD_GAME;
39 } else if (button.getLabel().toLowerCase().equals("quit")) {
40 Gdx.app.exit();
41 } else if (button.getLabel().toLowerCase().equals("survival")) {
42 GameScreen.NEW_GAME_MODE = 0;
43 CaveGame.STATE = AppState.GOTO_NEW_GAME;
44 } else if (button.getLabel().toLowerCase().equals("creative")) {
45 GameScreen.NEW_GAME_MODE = 1;
46 CaveGame.STATE = AppState.GOTO_NEW_GAME;
47 } else if (button.getLabel().toLowerCase().equals("back")) {
48 CaveGame.STATE = AppState.MENU_MAIN;
49 }
50 }
52 private void drawButton(Button button) {
53 spriter.draw(Assets.menuBtn[button.getType()], button.getX(), button.getY());
54 setFontColor(255, 255, 255);
55 drawString(button.getLabel(),
56 (button.getX() + button.getWidth() / 2) - (float) Assets.getStringWidth(button.getLabel()) / 2,
57 (button.getY() + button.getHeight() / 2) - (float) Assets.getStringHeight(button.getLabel()) / 2);
58 }
60 private void drawButtons(Array<Button> buttons) {
61 for (Button button : buttons) {
62 if (button.getType() > 0) {
63 if (button.getRect().contains(Gdx.input.getX() * getWidth() / GameScreen.getWidth(),
64 Gdx.input.getY() * getHeight() / GameScreen.getHeight()) &&
65 (!CaveGame.TOUCH || Gdx.input.isTouched()))
66 button.setType(2);
67 else button.setType(1);
68 }
69 drawButton(button);
70 }
71 }
73 private void drawLabel(String str) {
74 drawString(str);
75 }
77 @Override
78 public void render() {
79 spriter.begin();
80 for (int x = 0; x <= getWidth() / 16; x++) {
81 for (int y = 0; y <= getHeight() / 16; y++) {
82 spriter.draw(GameItems.getBlock(3).getTex(), x * 16, y * 16);
83 spriter.draw(Assets.shade, x * 16, y * 16);
84 }
85 }
86 spriter.draw(Assets.gameLogo, getWidth() / 2 - (float) Assets.gameLogo.getWidth() / 2, 8);
88 switch (CaveGame.STATE) {
89 case MENU_MAIN:
90 drawButtons(menuMainBtns);
91 break;
92 case MENU_NEW_GAME:
93 drawButtons(menuNGBtns);
94 break;
95 case GOTO_NEW_GAME:
96 case GOTO_LOAD_GAME:
97 drawLabel("Generating World...");
98 break;
99 case GOTO_MENU:
100 drawLabel("Saving Game...");
101 break;
104 drawString("CaveDroid " + CaveGame.VERSION, 0,
105 getHeight() - Assets.getStringHeight("CaveDroid " + CaveGame.VERSION) * 1.5f);
106 spriter.end();
108 switch (CaveGame.STATE) {
109 case GOTO_NEW_GAME:
110 CaveGame.STATE = AppState.NEW_GAME;
111 break;
112 case GOTO_LOAD_GAME:
113 CaveGame.STATE = AppState.LOAD_GAME;
114 break;
115 case GOTO_MENU:
116 CaveGame.STATE = AppState.SAVE_GAME;
117 break;