Advertisement
Unh0ly_Tigg

LHC.java

Mar 27th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.28 KB | None | 0 0
  1. package org.unh0lytigg.lhc;
  2.  
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.input.Keyboard;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL11;
  8. import org.newdawn.slick.Color;
  9.  
  10. @SuppressWarnings("unused")
  11. public class LHC {
  12.     public static boolean DRAW_DEBUG = false;
  13.     private int width, height;
  14.     private int gridspace_size;
  15.     private int[][] levels;
  16.     private boolean gameOver = false;
  17.     private float go_alpha = 0.0F;
  18.     private long alpha_timer = 500;
  19.     public LHC() {
  20.         width = 800;
  21.         height = 800;
  22.         gridspace_size = width / 5;
  23.         levels = new int[4][4];
  24.         int level = -1;
  25.         for (int x = 0; x < 4; x++) {
  26.             for (int y = 0; y < 4; y++) {
  27.                 levels[x][y] = level;
  28.                 level++;
  29.                 level %= 11;
  30.             }
  31.         }
  32.     }
  33.    
  34.     private static int[] color_levels = {0xEEE4DA,0xEDE0C8,0xF2B179,0xF59563,0xF67C5F,0xF65E38,0xEDCF72,0xEDCC61,0xEDC850,0xEDC53F,0xEDC22E};
  35.     private static String[] text_levels = {"electron", "electron neutrino", "muon", "muon neutrino", "tau", "tau neutrino", "gluon", "photon", "Z boson", "W boson", "Higgs boson"};
  36.     private static Color[] text_colors = {Color.darkGray, Color.white};
  37.    
  38.     private static int tileColors(int level) {
  39.         if (level == -1)
  40.             return 0xCCC0B3;
  41.         return color_levels[level];
  42.     }
  43.    
  44.     private static String texts(int level) {
  45.         if (level == -1)
  46.             return "";
  47.         return text_levels[level];
  48.     }
  49.    
  50.     private static Color textColor(int level) {
  51.         if (level == -1 || level == 0 || level == 1)
  52.             return text_colors[0];
  53.         return text_colors[1];
  54.     }
  55.    
  56.     private static float[] convertRGB(int rgb) {
  57.         int r = (rgb >> 16) & 0xFF;
  58.         int g = (rgb >> 8) & 0xFF;
  59.         int b = (rgb >> 0) & 0xFF;
  60.         float[] f = new float[3];
  61.         f[0] = (float)((float)r / (float)0xFF);
  62.         f[1] = (float)((float)g / (float)0xFF);
  63.         f[2] = (float)((float)b / (float)0xFF);
  64.         return f;
  65.     }
  66.    
  67.     private void keyboard() {
  68.         while (Keyboard.next()) {
  69.             if (gameOver) continue;
  70.             char c = Keyboard.getEventCharacter();
  71.             if (c == 'd') {
  72.                 DRAW_DEBUG = !DRAW_DEBUG;
  73.                 continue;
  74.             }
  75.             int key = Keyboard.getEventKey();
  76.             if (Keyboard.getEventKeyState()) {
  77.                 boolean moved = false;
  78.                 if (key == Keyboard.KEY_UP) {
  79.                     moved = up();
  80.                 } else if (key == Keyboard.KEY_DOWN) {
  81.                     moved = down();
  82.                 } else if (key == Keyboard.KEY_LEFT) {
  83.                     moved  = left();
  84.                 } else if (key == Keyboard.KEY_RIGHT) {
  85.                     moved = right();
  86.                 }
  87.                 if (moved) checkGrid();
  88.             }
  89.         }
  90.     }
  91.    
  92.     private void doLogicUpdates() {
  93.         if (gameOver) {
  94.             if (go_alpha < 0.8F) {
  95.                 if (alpha_timer == 0) {
  96.                     go_alpha += 0.01F;
  97.                     alpha_timer = 500;
  98.                 } else {
  99.                     alpha_timer -= 1;
  100.                 }
  101.             }
  102.         }
  103.     }
  104.    
  105.     private void checkGrid() {
  106.         gameOver = true;
  107.     }
  108.    
  109.     private boolean up() {
  110.         System.out.println("up");
  111.         return false;
  112.     }
  113.    
  114.     private boolean down() {
  115.         System.out.println("down");
  116.         return false;
  117.     }
  118.    
  119.     private boolean left() {
  120.         System.out.println("left");
  121.         return false;
  122.     }
  123.    
  124.     private boolean right() {
  125.         System.out.println("right");
  126.         return true;
  127.     }
  128.    
  129.     public void run() {
  130.         try {
  131.             Display.setDisplayMode(new DisplayMode(width,height));
  132.             Display.create();
  133.             Display.setTitle("LHC");
  134.         } catch (LWJGLException e) {
  135.             e.printStackTrace();
  136.             System.exit(0);
  137.         }
  138.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  139.         GL11.glLoadIdentity();
  140.         GL11.glOrtho(0, width, height, 0, 1, -1);
  141.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  142.         while (!Display.isCloseRequested()) {
  143.             keyboard();
  144.             doLogicUpdates();
  145.             GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  146.             drawRect(0, 0, width, height, 0xBBADA0);
  147.             for (int x = 0; x < 4; x++) {
  148.                 int xOffset = (gridspace_size / 5) + (gridspace_size * x) + ((gridspace_size / 5) * x);
  149.                 for (int y = 0; y < 4; y++) {
  150.                     int yOffset = (gridspace_size / 5) + (gridspace_size * y) + ((gridspace_size / 5) * y);
  151.                     drawRoundRect(xOffset, yOffset, xOffset + gridspace_size, yOffset + gridspace_size, gridspace_size / 8, tileColors(levels[x][y]));
  152.                 }
  153.             }
  154.             if (gameOver) {
  155.                 GL11.glEnable(GL11.GL_BLEND);
  156.                 GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_SRC_ALPHA);
  157.                 GL11.glColor4f(0.5f, 0.0f, 0.0f, go_alpha);
  158.                 GL11.glBegin(GL11.GL_QUADS);
  159.                 GL11.glVertex2i(width, 0);
  160.                 GL11.glVertex2i(0, 0);
  161.                 GL11.glVertex2i(0, height);
  162.                 GL11.glVertex2i(width, height);
  163.                 GL11.glEnd();
  164.             }
  165.             Display.sync(30);
  166.             Display.update();
  167.         }
  168.         Display.destroy();
  169.     }
  170.    
  171.     private static void drawRoundRect(int x, int y, int x1, int y1, int radius, int color) {
  172.         int newX = Math.abs(x+radius);
  173.         int newY = Math.abs(y+radius);
  174.         int newX1 = Math.abs(x1-radius);
  175.         int newY1 = Math.abs(y1-radius);
  176.         drawRect(newX, newY, newX1, newY1, color);
  177.         drawRect(x, newY, newX, newY1, color);
  178.         drawRect(newX1, newY, x1, newY1, color);
  179.         drawRect(newX, y, newX1, newY, color);
  180.         drawRect(newX, newY1, newX1, y1, color);
  181.         drawQuarterCircle(newX,newY,radius,0,color);
  182.         drawQuarterCircle(newX1,newY,radius,3,color);
  183.         drawQuarterCircle(newX,newY1,radius,1,color);
  184.         drawQuarterCircle(newX1,newY1,radius,2,color);
  185.     }
  186.    
  187.     private static void drawRect(int x, int y, int x1, int y1, int color) {
  188.         drawRectModed(x, y, x1, y1, color, GL11.GL_QUADS);
  189.         if (DRAW_DEBUG)
  190.             drawRectModed(x, y, x1, y1, 0x000000, GL11.GL_LINE_LOOP);
  191.     }
  192.    
  193.     private static void drawRectModed(int x, int y, int x1, int y1, int color, int mode) {
  194.         float[] f = convertRGB(color);
  195.         GL11.glColor3f(f[0],f[1],f[2]);
  196.         GL11.glBegin(mode);
  197.         GL11.glVertex2i(x1, y);
  198.         GL11.glVertex2i(x, y);
  199.         GL11.glVertex2i(x, y1);
  200.         GL11.glVertex2i(x1, y1);
  201.         GL11.glEnd();
  202.     }
  203.    
  204.     private static void drawQuarterCircle(int x, int y, int radius, int quadriant, int color) {
  205.         drawQuarterCircleModed(x, y, radius, quadriant, color, GL11.GL_POLYGON);
  206.         if (DRAW_DEBUG)
  207.             drawQuarterCircleModed(x, y, radius, quadriant, 0x000000, GL11.GL_LINE_LOOP);
  208.     }
  209.    
  210.     private static void drawQuarterCircleModed(int x, int y, int radius, int quadriant, int color, int mode) {
  211.         float[] f = convertRGB(color);
  212.         GL11.glColor3f(f[0],f[1],f[2]);
  213.         GL11.glBegin(mode);
  214.         GL11.glVertex2d(x, y);
  215.         if (quadriant == 0) {
  216.             for (int i = 0; i <= 90; i++)
  217.                 GL11.glVertex2d(x + (Math.sin((i * Math.PI / 180)) * (radius*-1)), y + (Math.cos((i * Math.PI / 180)) * (radius*-1)));
  218.         } else if (quadriant == 1) {
  219.             for (int i = 90; i <= 180; i++)
  220.                 GL11.glVertex2d(x + (Math.sin((i * Math.PI / 180)) * (radius*-1)), y + (Math.cos((i * Math.PI / 180)) * (radius*-1)));
  221.         } else if (quadriant == 2) {
  222.             for (int i = 180; i <= 270; i++)
  223.                 GL11.glVertex2d(x + (Math.sin((i * Math.PI / 180)) * (radius*-1)), y + (Math.cos((i * Math.PI / 180)) * (radius*-1)));
  224.         } else if (quadriant == 3) {
  225.             for (int i = 270; i <= 360; i++)
  226.                 GL11.glVertex2d(x + (Math.sin((i * Math.PI / 180)) * (radius*-1)), y + (Math.cos((i * Math.PI / 180)) * (radius*-1)));
  227.         }
  228.         GL11.glEnd();
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement