daily pastebin goal
93%
SHARE
TWEET

asd

a guest Nov 17th, 2017 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Game implements Runnable {
  2.  
  3.     @Getter private Display display;
  4.     @Getter private int width, height;
  5.     private String title;
  6.  
  7.     @Getter @Setter private boolean running = false;
  8.     private Thread thread;
  9.  
  10.     private BufferStrategy bs;
  11.     private Graphics g;
  12.  
  13.     //States
  14.     public State gameState;
  15.     public State menuState;
  16.  
  17.     //Input
  18.     @Getter private KeyManager keyManager;
  19.     @Getter private MouseManager mouseManager;
  20.  
  21.     //Camera
  22.     @Getter private GameCamera gameCamera;
  23.  
  24.  
  25.     //API
  26.     @Getter private API API;
  27.  
  28.     public Game(String title, int width, int height) {
  29.         this.width = width;
  30.         this.height = height;
  31.         this.title = title;
  32.  
  33.         keyManager = new KeyManager();
  34.         mouseManager = new MouseManager();
  35.     }
  36.  
  37.     private void init() {
  38.         display = new Display(title, width, height);
  39.         display.getFrame().addKeyListener(keyManager);
  40.         display.getFrame().addMouseListener(mouseManager);
  41.         display.getFrame().addMouseMotionListener(mouseManager);
  42.         display.getCanvas().addMouseListener(mouseManager);
  43.         display.getCanvas().addMouseMotionListener(mouseManager);
  44.         display.getCanvas().addMouseWheelListener(mouseManager);
  45.  
  46.         Fonts.init();
  47.         GUI.init();
  48.         Assets.init();
  49.         Models.init();
  50.  
  51.         API = new API(this);
  52.         gameCamera = new GameCamera(API, 0, 0);
  53.  
  54.         gameState = new GameState(API, "test");
  55.         menuState = new MenuState(API);
  56.  
  57.         State.setState(menuState);
  58.     }
  59.  
  60.     private void tick() {
  61.         keyManager.tick();
  62.  
  63.         if (State.getState() != null) {
  64.             State.getState().tick();
  65.         }
  66.     }
  67.  
  68.     private void render() {
  69.         bs = display.getCanvas().getBufferStrategy();
  70.         if (bs == null) {
  71.             display.getCanvas().createBufferStrategy(3);
  72.             return;
  73.         }
  74.         g = bs.getDrawGraphics();
  75.         //Clear Screen
  76.         g.clearRect(0, 0, width, height);
  77.         //Draw Here!
  78.  
  79.         if (State.getState() != null) State.getState().render(g);
  80.  
  81.         //End Drawing!
  82.         bs.show();
  83.         g.dispose();
  84.     }
  85.  
  86.     public void run() {
  87.         init();
  88.  
  89.         int fps = 60;
  90.         double timePerTick = 1000000000 / fps;
  91.         double delta = 0;
  92.         long now;
  93.         long lastTime = System.nanoTime();
  94.         long timer = 0;
  95.         int ticks = 0;
  96.  
  97.         while (running) {
  98.             now = System.nanoTime();
  99.             delta += (now - lastTime) / timePerTick;
  100.             timer += now - lastTime;
  101.             lastTime = now;
  102.  
  103.             if (delta >= 1) {
  104.                 tick();
  105.                 render();
  106.                 ticks++;
  107.                 delta--;
  108.             }
  109.  
  110.             if (timer >= 1000000000) {
  111.                 //Log.log("FPS: " + ticks);
  112.                 ticks = 0;
  113.                 timer = 0;
  114.             }
  115.         }
  116.         stop();
  117.     }
  118.  
  119.     public synchronized void start() {
  120.         if (running) {
  121.             return;
  122.         }
  123.         running = true;
  124.         thread = new Thread(this);
  125.         thread.start();
  126.     }
  127.  
  128.     public synchronized void stop() {
  129.         if (!running) {
  130.             return;
  131.         }
  132.         running = false;
  133.         try {
  134.             thread.join();
  135.         } catch (InterruptedException e) {
  136.             e.printStackTrace();
  137.         }
  138.     }
  139. }
RAW Paste Data
Top