Package beaver.game

Source Code of beaver.game.MainMenuState

package beaver.game;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.GameContainer;
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 org.newdawn.slick.Music;

public class MainMenuState extends BasicGameState {

  // Initialize stateID to an "impossible" value
  int stateID = -1;
 
  // Initialize our menu images
  Image bg = null;
  Image start = null;
  int startWidth;
  int startHeight;
  Image options = null;
  int optionsWidth;
  int optionsHeight;
  Image exit = null;
  int exitWidth;
  int exitHeight;
 
  // The scale of our menu images, because they get bigger when we hover over them
  float startScale = 1;
  float optionsScale = 1;
  float exitScale = 1;
  float scaleStep = 0.01f;
 
  // Menu positioning
  int startX;
  int optionsX;
  int exitX;
  int menuTopAlign = 235;
  int spacing = 100;
 
  //Background music (not yet loaded)
    private Music song = null;
 
  MainMenuState(int stateID)
  {
    this.stateID = stateID;
  }
 
  @Override
  public int getID() {
    return stateID;
  }
 
  public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    bg = new Image("data/menu/menu.png");
   
    start = new Image("data/menu/local.png");
    startWidth = start.getWidth();
    startHeight = start.getHeight();
    startX = 400-(startWidth/2);
   
    options = new Image("data/menu/network.png");
    optionsWidth = options.getWidth();
    optionsHeight = options.getHeight();
    optionsX = 400-(optionsWidth/2);
   
    exit = new Image("data/menu/quit.png");
    exitWidth = exit.getWidth();
    exitHeight = exit.getHeight();
    exitX = 400-(exitWidth/2);
   
    song = new Music("data/song.ogg");
  }
 
  // Start Music when we enter this state!
  public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
    song.loop();
  }
 
  // Stop Music when we leave this state!
  public void leave(GameContainer gc, StateBasedGame sbg) throws SlickException {
    song.stop();
  }
 
  public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
     bg.draw(0,0);
     start.draw(startX, menuTopAlign, startScale);
     options.draw(optionsX, menuTopAlign + spacing, optionsScale);
     exit.draw(exitX, menuTopAlign + (2*spacing), exitScale);
  }
 
  public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    Input input = gc.getInput();
   
    int mouseX = input.getMouseX();
    int mouseY = input.getMouseY();
   
    boolean insideStart = false;
    boolean insideOptions = false;
    boolean insideExit = false;
   
    /* Handle case where the user hovers over the "Start Game" button */
    if( (mouseX >= startX && mouseX <= startX + startWidth) &&
      (mouseY >= menuTopAlign && mouseY <= menuTopAlign + startHeight) )
        insideStart = true;
   
    if(insideStart) {
      // scale up by 5%
      if(startScale < 1.05f)
        startScale += scaleStep * delta;
      // make button clickable
      if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
          sbg.enterState(BeaversGame.GAMEPLAYSTATE);
        }
    }
    else {
      // once we move out of the "Start Game" box, scale back down to original size
      if(startScale > 1.0f)
        startScale -= scaleStep * delta;
    }
   
    /* Handle case where the user hovers over the "Options" button */
    if( (mouseX >= optionsX && mouseX <= optionsX + optionsWidth) &&
        (mouseY >= menuTopAlign+spacing && mouseY <= menuTopAlign+spacing + optionsHeight) )
      insideOptions = true;

    if(insideOptions) {
      // scale up by 5%
      if(optionsScale < 1.05f)
        optionsScale += scaleStep * delta;
      if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
        //If we click on "Network", then popup a "Is Host?" dialog
        int isHost = JOptionPane.showConfirmDialog(null,
            "Will you be hosting this game?",
            "Choose one", JOptionPane.YES_NO_OPTION);
        System.out.println(isHost);
        boolean host = false;
        if (isHost == 0) host = true;
        System.out.println(host);
        String ip = "localhost";
       
        if(!host){
          //Not host, no need for server
          ip = (String)JOptionPane.showInputDialog(
              "Enter IP address");
        }
        else{
          // Start server
          Runnable serv = new Runnable(){
            public void run(){
              GameServer s = new GameServer();
              s.run();
            }
          };
         
          new Thread(serv).start();
         
          // Sleep current thread for a while, while server boots up
          try {
            Thread.sleep(5000);
          }
          catch (InterruptedException ie) {}
         
          /*Thread t = new Thread(){
            public void run(){
              GameServer s = new GameServer();
              s.run();
            }
          };*/
          //t.start();
        }
       
        try{
          sbg.addState(new ClientState(BeaversGame.CLIENTSTATE,ip));
          sbg.getState(BeaversGame.CLIENTSTATE).init(gc, sbg);
          sbg.enterState(BeaversGame.CLIENTSTATE);
        }catch(Exception e){
          JOptionPane.showMessageDialog(null,"An Error occured while establishing a connection");
          sbg.enterState(BeaversGame.MAINMENUSTATE);
        }
        }
    }
    else {
      // once we move out of the "Options" box, scale back down to original size
      if(optionsScale > 1.0f)
        optionsScale -= scaleStep * delta;
    }
   
    /* Handle case where the user hovers over the "Exit" button */
    if( (mouseX >= exitX && mouseX <= exitX + exitWidth) &&
        (mouseY >= menuTopAlign+2*spacing && mouseY <= menuTopAlign+2*spacing + exitHeight) )
      insideExit = true;

    if(insideExit) {
      // scale up by 5%
      if(exitScale < 1.05f)
        exitScale += scaleStep * delta;
      // make button clickable
      if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
          gc.exit();
        }
    }
    else {
      // once we move out of the "Options" box, scale back down to original size
      if(exitScale > 1.0f)
        exitScale -= scaleStep * delta;
    }
  }
TOP

Related Classes of beaver.game.MainMenuState

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.