package com.lucasdnd.ags.system; import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; import com.lucasdnd.ags.ui.NewGamePanel; import com.lucasdnd.ags.ui.TextButton; public class MenuState extends BasicGameState { private int id; Image titleImage; private TextButton startGameButton, loadGameButton, optionsButton, creditsButton, exitButton; private float startingMenuPos, menuSpacing; final float patternScale = 2f; private NewGamePanel newGamePanel; public MenuState() { id = GameSystem.MENU_STATE; } @Override public void init(GameContainer container, StateBasedGame game) throws SlickException { titleImage = ResourceLoader.getInstance().title; startingMenuPos = container.getHeight() / 2f; menuSpacing = 48f; float buttonWidth = 160f; float buttonHeight = 32f; startGameButton = new TextButton("New Game", container.getWidth() / 2f - buttonWidth / 2f, startingMenuPos, buttonWidth, buttonHeight); loadGameButton = new TextButton("Load Game", container.getWidth() / 2f - buttonWidth / 2f, startingMenuPos + menuSpacing * 1, buttonWidth, buttonHeight); optionsButton = new TextButton("Options", container.getWidth() / 2f - buttonWidth / 2f, startingMenuPos + menuSpacing * 2, buttonWidth, buttonHeight); creditsButton = new TextButton("Credits", container.getWidth() / 2f - buttonWidth / 2f, startingMenuPos + menuSpacing * 3, buttonWidth, buttonHeight); exitButton = new TextButton("Exit", container.getWidth() / 2f - buttonWidth / 2f, startingMenuPos + menuSpacing * 4, buttonWidth, buttonHeight); loadGameButton.setEnabled(false); optionsButton.setEnabled(false); creditsButton.setEnabled(false); float newGamePanelWidth = 480f; float newGamePanelHeight = 270f; newGamePanel = new NewGamePanel("New Game", container.getWidth() / 2f - newGamePanelWidth / 2f, container.getHeight() / 2f - newGamePanelHeight / 2f, newGamePanelWidth, newGamePanelHeight); } @Override public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException { // Get input Input input = container.getInput(); int mouseX = input.getMouseX(); int mouseY = input.getMouseY(); boolean leftMouseClicked = input.isMousePressed(Input.MOUSE_LEFT_BUTTON); boolean leftMouseDown = input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON); // Update buttons startGameButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); loadGameButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); optionsButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); creditsButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); exitButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); // Clicks if (leftMouseClicked && newGamePanel.isVisible() == false) { if (startGameButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) { newGamePanel.setVisible(true); } else if (loadGameButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) { } else if (optionsButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) { } else if (creditsButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) { } else if (exitButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) { container.exit(); } else if (newGamePanel.gotLeftClicked(leftMouseClicked, mouseX, mouseY) == false) { newGamePanel.setVisible(false); } leftMouseClicked = false; // Consumes the click event so it doesn't propagate into the newGamePanel in the same update cycle } // Esc to hide the New Game Panel if (input.isKeyPressed(Input.KEY_ESCAPE)) { newGamePanel.setVisible(false); } // Update New Game Panel if (newGamePanel.isVisible()) { newGamePanel.getStoreNameTextInput().setEnabled(true); newGamePanel.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); } else { newGamePanel.getStoreNameTextInput().setEnabled(false); } } @Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { g.setBackground(Color.white); // Background pattern g.scale(patternScale, patternScale); for (int i = 0; i < container.getWidth() / 32 / patternScale + 1; i++) { for (int j = 0; j < container.getHeight() / 16 / patternScale + 1; j++) { ResourceLoader.getInstance().groundSpriteSheet.getSprite(0, 0).draw(i * 32, j * 16); } } g.scale(1/patternScale, 1/patternScale); titleImage.draw(container.getWidth() / 2f - titleImage.getWidth() / 2f - 10f, 70f); // Buttons startGameButton.render(container, game, g); loadGameButton.render(container, game, g); optionsButton.render(container, game, g); creditsButton.render(container, game, g); exitButton.render(container, game, g); // New Game Panel g.setLineWidth(4f); newGamePanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyLightGrayFont); // Version g.setFont(ResourceLoader.getInstance().tinyBlackFont); g.drawString("v" + GameSystem.VERSION, container.getWidth() - 100f, container.getHeight() - 30f); } @Override public int getID() { return id; } }