Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. /**
  2.  * The main menu.
  3.  */
  4.  
  5. package uk.connorwright.Pulse.gamestates;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Font;
  9. import java.awt.Graphics2D;
  10. import java.awt.image.BufferedImage;
  11. import java.util.ArrayList;
  12. import java.util.Random;
  13.  
  14. import uk.connorwright.Pulse.entity.Bubble;
  15. import uk.connorwright.Pulse.entity.GameButton;
  16. import uk.connorwright.Pulse.handlers.GameStateManager;
  17. import uk.connorwright.Pulse.handlers.ImageLoader;
  18. import uk.connorwright.Pulse.handlers.Mouse;
  19. import uk.connorwright.Pulse.main.Game;
  20. import uk.connorwright.Pulse.main.GamePanel;
  21.  
  22. public class MenuState extends GameState {
  23.        
  24.     int listIndex;
  25.     static String list;
  26.    
  27.     // bg image
  28.     private BufferedImage bg;
  29.    
  30.     // bubbles
  31.     private ArrayList<Bubble> bubbles;
  32.     private int bubbleTimer;
  33.    
  34.     // buttons
  35.     private int currentChoice = 0;
  36.     private GameButton[] options;
  37.    
  38.     // fonts and colors
  39.     private Color titleColor;
  40.     private Font titleFont;
  41.     private Font font;
  42.     private Font font2;
  43.    
  44.     // fading
  45.     private int fadeInTimer;
  46.     private int fadeInDelay;
  47.     private int fadeOutTimer;
  48.     private int fadeOutDelay;
  49.     private int alpha;
  50.     private int nextState;
  51.    
  52.     public boolean displayed;
  53.    
  54.     public MenuState(GameStateManager gsm) {
  55.         super(gsm);
  56.         init();
  57.     }
  58.    
  59.     public void init() {
  60.        
  61.         // load bg image
  62.         bg = ImageLoader.BG;
  63.        
  64.         // load fonts
  65.         try {
  66.             Font scFont = Font.createFont(
  67.                     Font.TRUETYPE_FONT,
  68.                     getClass().getResourceAsStream("/fonts/SECRCODE.TTF"));
  69.             titleColor = Color.WHITE;
  70.             titleFont = scFont.deriveFont(64f);
  71.             font = scFont.deriveFont(26f);
  72.             font2 = scFont.deriveFont(14f);
  73.            
  74.         }
  75.         catch(Exception e) {
  76.             e.printStackTrace();
  77.         }
  78.        
  79.         // show mouse cursor
  80.         Game.setCursor(Game.VISIBLE);
  81.        
  82.         // set up buttons
  83.         options = new GameButton[2];
  84.         options[0] = new GameButton(320, 300, 100, 50);
  85.         options[0].setText("s t a r t", font);
  86.         options[1] = new GameButton(320, 350, 100, 50);
  87.         options[1].setText("q u i t", font);
  88.        
  89.         // fade timer
  90.         fadeInTimer = 0;
  91.         fadeInDelay = 60;
  92.         fadeOutTimer = -1;
  93.         fadeOutDelay = 60;
  94.        
  95.         // music
  96.        
  97.         // bubbles
  98.         bubbles = new ArrayList<Bubble>();
  99.         bubbleTimer = 0;
  100.        
  101.     }
  102.    
  103.     public void update() {
  104.        
  105.         // check keys
  106.         handleInput();
  107.        
  108.         // check buttons for hover
  109.         for(int i = 0; i < options.length; i++) {
  110.             if(currentChoice == i) {
  111.                 options[i].setHover(true);
  112.             }
  113.             else {
  114.                 options[i].setHover(false);
  115.             }
  116.         }
  117.        
  118.         // update fade
  119.         if(fadeInTimer >= 0) {
  120.             fadeInTimer++;
  121.             alpha = (int) (255.0 * fadeInTimer / fadeInDelay);
  122.             if(fadeInTimer == fadeInDelay) {
  123.                 fadeInTimer = -1;
  124.             }
  125.         }
  126.         if(fadeOutTimer >= 0) {
  127.             fadeOutTimer++;
  128.             alpha = (int) (255.0 * fadeOutTimer / fadeOutDelay);
  129.             if(fadeOutTimer == fadeOutDelay) {
  130.                 gsm.setState(nextState);
  131.             }
  132.         }
  133.         if(alpha < 0) alpha = 0;
  134.         if(alpha > 255) alpha = 255;
  135.        
  136.         // bubbles
  137.         bubbleTimer++;
  138.         if(bubbleTimer == 60) {
  139.             bubbles.add(new Bubble(Math.random() * 540 - 100, Math.random() * 100 + 480));
  140.             bubbleTimer = 0;
  141.         }
  142.         for(int i = 0; i < bubbles.size(); i++) {
  143.             if(bubbles.get(i).update()) {
  144.                 bubbles.remove(i);
  145.                 i--;
  146.             }
  147.         }
  148.        
  149.     }
  150.    
  151.     public void draw(Graphics2D g) {
  152.        
  153.    
  154.         // draw background
  155.         g.drawImage(bg, 0, 0, null);
  156.        
  157.         // draw bubbles
  158.         for(int i = 0; i < bubbles.size(); i++) {
  159.             bubbles.get(i).draw(g);
  160.         }
  161.        
  162.         // draw title
  163.         g.setColor(titleColor);
  164.         g.setFont(titleFont);
  165.         g.drawString("P  U  L  S  E", 100, 180);
  166.        
  167.         // draw menu options
  168.         g.setFont(font);
  169.         g.setColor(Color.WHITE);
  170.        
  171.         // draw buttons
  172.         for(int i = 0; i < options.length; i++) {
  173.             options[i].draw(g);
  174.         }
  175.        
  176.         // other
  177.         g.setFont(font2);
  178.         g.setColor(Color.BLACK);
  179.         g.drawString("2014 Connor W.", 10, 470);
  180.        
  181.         // draw splash text
  182.         drawSplash(g);
  183.        
  184.         // draw fade
  185.         if(fadeInTimer >= 0) {
  186.             g.setColor(new Color(255, 255, 255, 255 - alpha));
  187.             g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
  188.         }
  189.         if(fadeOutTimer >= 0) {
  190.             g.setColor(new Color(255, 255, 255, alpha));
  191.             g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
  192.         }
  193.        
  194.        
  195.        
  196.     }
  197.    
  198.     private void drawSplash(Graphics2D g){
  199.        
  200.         if(displayed == false){
  201.         Random r = new Random();
  202.         String items[] = {"Buggy!", "New Game!", "Roll up, roll up!", "Made from Scratch"};
  203.        
  204.        
  205.         amount = (int) (Math.random()*25+1);
  206.        
  207.         list = items[r.nextInt(3+1)];
  208.  
  209.         System.out.println(list);
  210.        
  211.         g.setFont(font2);
  212.         g.setColor(Color.BLACK);
  213.         g.drawString(list, 320, 240);
  214.  
  215.         displayed = true;
  216.    
  217.         }
  218.     }
  219.    
  220.     private void select() {
  221.        
  222.         // already transitioning, cannot select
  223.         if(fadeOutTimer != -1) return;
  224.        
  225.         // go to level select
  226.         if(currentChoice == 0) {
  227.             nextState = GameStateManager.LEVEL_SELECT_STATE;
  228.             fadeInTimer = -1;
  229.             fadeOutTimer = 0;
  230.    
  231.         }
  232.        
  233.         // quit
  234.         else if(currentChoice == 1) {
  235.             System.exit(0);
  236.         }
  237.     }
  238.    
  239.     public void handleInput() {
  240.        
  241.         // see if button is clicked
  242.         if(Mouse.isPressed()) {
  243.             select();
  244.         }
  245.        
  246.         // check if mouse is hovering over buttons
  247.         boolean hit = false;
  248.         for(int i = 0; i < options.length; i++) {
  249.             if(options[i].isHovering(Mouse.x, Mouse.y)) {
  250.                 currentChoice = i;
  251.                 hit = true;
  252.                 break;
  253.             }
  254.         }
  255.         if(!hit) currentChoice = -1;
  256.        
  257.     }
  258.    
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement