DEADSOFTWARE

Fix code style
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / menu / MenuRenderer.java
1 package ru.deadsoftware.cavecraft.menu;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.utils.Array;
5 import ru.deadsoftware.cavecraft.CaveGame;
6 import ru.deadsoftware.cavecraft.GameScreen;
7 import ru.deadsoftware.cavecraft.game.GameSaver;
8 import ru.deadsoftware.cavecraft.game.Items;
9 import ru.deadsoftware.cavecraft.menu.objects.Button;
10 import ru.deadsoftware.cavecraft.misc.AppState;
11 import ru.deadsoftware.cavecraft.misc.Assets;
12 import ru.deadsoftware.cavecraft.misc.Renderer;
14 public class MenuRenderer extends Renderer {
16 public Array<Button> menuMainButtons;
18 public MenuRenderer(int width) {
19 super(width, width * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
20 menuMainButtons = new Array<Button>();
21 menuMainButtons.add(new Button("New game", getWidth() / 2 - 100, getHeight() / 4));
22 menuMainButtons.add(new Button("Load game", getWidth() / 2 - 100, getHeight() / 4 + 30, GameSaver.exists() ? 1 : 0));
23 menuMainButtons.add(new Button("Quit", getWidth() / 2 - 100, getHeight() / 4 + 60));
24 }
26 public void buttonClicked(Button button) {
27 if (button.getLabel().toLowerCase().equals("new game")) {
28 CaveGame.STATE = AppState.GOTO_NEW_GAME;
29 } else if (button.getLabel().toLowerCase().equals("load game")) {
30 CaveGame.STATE = AppState.GOTO_LOAD_GAME;
31 } else if (button.getLabel().toLowerCase().equals("quit")) {
32 Gdx.app.exit();
33 }
34 }
36 private void drawButton(Button button) {
37 spriteBatch.draw(Assets.menuButton[button.getType()], button.getX(), button.getY());
38 setFontColor(255, 255, 255);
39 drawString(button.getLabel(),
40 (button.getX() + button.getWidth() / 2) - Assets.getStringWidth(button.getLabel()) / 2,
41 (button.getY() + button.getHeight() / 2) - Assets.getStringHeight(button.getLabel()) / 2);
42 }
44 private void drawMenuMain() {
45 for (Button button : menuMainButtons) {
46 if (button.getType() > 0) {
47 if (button.getRect().contains(Gdx.input.getX() * getWidth() / GameScreen.getWidth(),
48 Gdx.input.getY() * getHeight() / GameScreen.getHeight()))
49 button.setType(2);
50 else button.setType(1);
51 }
52 drawButton(button);
53 }
54 }
56 public void drawLabel(String str) {
57 drawString(str);
58 }
60 @Override
61 public void render() {
62 spriteBatch.begin();
63 for (int x = 0; x <= getWidth() / 16; x++)
64 for (int y = 0; y <= getHeight() / 16; y++) {
65 spriteBatch.draw(Assets.blockTextures[Items.BLOCKS.get("dirt").getTexture()], x * 16, y * 16);
66 spriteBatch.draw(Assets.shade, x * 16, y * 16);
67 }
68 spriteBatch.draw(Assets.gameLogo, getWidth() / 2 - Assets.gameLogo.getWidth() / 2, 0);
70 switch (CaveGame.STATE) {
71 case MENU_MAIN:
72 drawMenuMain();
73 break;
74 case GOTO_NEW_GAME:
75 case GOTO_LOAD_GAME:
76 drawLabel("Generating World...");
77 break;
78 case GOTO_MENU:
79 drawLabel("Saving Game...");
80 break;
81 }
83 drawString("CaveCraft " + CaveGame.VERSION, 0,
84 getHeight() - Assets.getStringHeight("CaveCraft " + CaveGame.VERSION) * 1.5f);
85 spriteBatch.end();
87 switch (CaveGame.STATE) {
88 case GOTO_NEW_GAME:
89 CaveGame.STATE = AppState.NEW_GAME;
90 break;
91 case GOTO_LOAD_GAME:
92 CaveGame.STATE = AppState.LOAD_GAME;
93 break;
94 case GOTO_MENU:
95 CaveGame.STATE = AppState.SAVE_GAME;
96 break;
97 }
99 }