Guest User

Game

a guest
Jun 25th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. public class Game {
  2.  
  3.     private static final boolean resizable = true;
  4.     private static volatile boolean running = true;
  5.  
  6.    
  7.     private static final float zNear = 0.3f;
  8.     private static final float zFar = 100f;
  9.    
  10.     private static final int gridSize = 10;
  11.     private static final float tileSize = 0.20f;
  12.  
  13.    
  14.     private static final boolean fullscreen = false;
  15.     private static final boolean vsync = true;
  16.     private static final boolean printFPS = true;
  17.    
  18.     private static final float ceilingHeight = 10;
  19.     private static final float floorHeight = -1;
  20.    
  21.     private static final int fov = 80;
  22.     private static int fps;
  23.     private static long lastFPS;
  24.     private static Timer timer;
  25.    
  26.     private static int delta;
  27.    
  28.     private static Random random;
  29.     private static Player player;
  30.     private static BasicLevel basicLevel;
  31.     private static ArrayList<TriangleMob> triangleMob = new ArrayList<TriangleMob>();
  32.  
  33.     private static void updateFPS() {
  34.         if (timer.getTime() - lastFPS > 1000) {
  35.             if (printFPS) {
  36.                 System.out.println("FPS: " + fps);
  37.             }
  38.             fps = 0;
  39.             lastFPS += 1000;
  40.         }
  41.         fps++;
  42.     }
  43.  
  44.     public static void main(String[] args) {
  45.         try {
  46.             if (fullscreen) {
  47.                 Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
  48.             } else {
  49.                 Display.setResizable(resizable);
  50.                 Display.setDisplayMode(new DisplayMode(800, 600));
  51.             }
  52.             Display.setTitle("Minefront Pre-Alpha 0.02 LWJGL Port");
  53.             Display.setVSyncEnabled(vsync);
  54.             Display.create();
  55.         } catch (LWJGLException ex) {
  56.             ex.printStackTrace();
  57.             Display.destroy();
  58.             System.exit(1);
  59.         }
  60.  
  61.         if (fullscreen) {
  62.             Mouse.setGrabbed(true);
  63.         } else {
  64.             Mouse.setGrabbed(false);
  65.         }
  66.  
  67.         if (!GLContext.getCapabilities().OpenGL11) {
  68.             System.err.println("Your OpenGL version doesn't support the required functionality.");
  69.             Display.destroy();
  70.             System.exit(1);
  71.         }
  72.  
  73.         glMatrixMode(GL_PROJECTION);
  74.         glLoadIdentity();
  75.         gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), zNear, zFar);
  76.         glMatrixMode(GL_MODELVIEW);
  77.         glLoadIdentity();
  78.  
  79.         glEnable(GL_DEPTH_TEST);
  80.         glEnable(GL_TEXTURE_2D);
  81.         glEnable(GL_BLEND);
  82.         glEnable(GL_ALPHA_TEST);
  83.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  84.         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  85.         glEnable(GL_CULL_FACE);
  86.         glCullFace(GL_BACK);
  87.  
  88.         //Initialize Timer
  89.         timer = new Timer();
  90.         timer.getDelta();
  91.         lastFPS = timer.getTime();
  92.        
  93.         random = new Random();
  94.        
  95.         //Initialize the BasicLevel
  96.         basicLevel = new BasicLevel(gridSize, ceilingHeight, floorHeight, tileSize);
  97.        
  98.         //Initialize the Player
  99.         player = new Player();
  100.        
  101.         triangleMob.add(new TriangleMob());
  102.  
  103.         while (running) {
  104.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  105.  
  106.             delta = timer.getDelta();
  107.  
  108.  
  109.             glLoadIdentity();
  110.             glRotatef(player.getRotation().x, 1, 0, 0);
  111.             glRotatef(player.getRotation().y, 0, 1, 0);
  112.             glRotatef(player.getRotation().z, 0, 0, 1);
  113.             glTranslatef(player.getPosition().x, player.getPosition().y, player.getPosition().z);
  114.  
  115.             logic();
  116.             render();
  117.            
  118.             if (printFPS) {
  119.                 updateFPS();
  120.             }
  121.             Display.update();
  122.             if (vsync) {
  123.                 Display.sync(60);
  124.             }
  125.             if (Display.isCloseRequested()) {
  126.                 running = false;
  127.             }
  128.         }
  129.         Display.destroy();
  130.         System.exit(0);
  131.     }
  132.  
  133.     private static void logic() {
  134.         player.move(delta);
  135.         for(int i=0; i<triangleMob.size();i++) {
  136.             triangleMob.get(i).move(new Vector3f(0f,0f,0f));
  137.         }
  138.        
  139.         if(Keyboard.isKeyDown(Keyboard.KEY_C)) {
  140.             triangleMob.add(new TriangleMob(new Vector3f(50*random.nextFloat(),50*random.nextFloat(),50*random.nextFloat())));
  141.         }
  142.         System.out.println(TriangleMob.getCount());
  143.     }
  144.  
  145.     private static void render() {
  146.         basicLevel.render();
  147.         for(int i=0; i<triangleMob.size();i++) {
  148.             triangleMob.get(i).render();
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment