Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package com.loyal.main;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. public class MainComponent extends Canvas implements Runnable {
  14.    
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     public static final int WIDTH = 1000,
  18.                             HEIGHT = 563;
  19.    
  20.     private boolean running = false;
  21.    
  22.     private Camera camera;
  23.    
  24.     private Thread thread;
  25.    
  26.     private BufferedImage levelimg;
  27.    
  28.     private Game game;
  29.    
  30.     private Input input;
  31.    
  32.     public MainComponent() {
  33.        
  34.         input = new Input();
  35.        
  36.         this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  37.         this.setMaximumSize(new Dimension(WIDTH, HEIGHT));
  38.         this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
  39.        
  40.         this.addKeyListener(input);
  41.        
  42.         game = new Game(this);
  43.        
  44.         levelimg = new ImageLoader().load("/image.png");
  45.        
  46.         camera = new Camera(0, 0);
  47.        
  48.         JFrame frame = new JFrame(":D");
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         frame.setResizable(false);
  51.         frame.setLocationRelativeTo(null);
  52.         frame.setVisible(true);
  53.         frame.add(this);
  54.         frame.pack();
  55.        
  56.         loadLevel(levelimg);
  57.        
  58.        
  59.     }
  60.    
  61.     public synchronized void start() {
  62.         if(running)
  63.             return;
  64.         running = true;
  65.         thread = new Thread(this);
  66.         thread.run();
  67.     }
  68.    
  69.     public synchronized void stop() {
  70.         if(!running)
  71.             return;
  72.         running = false;
  73.        
  74.         try {
  75.             thread.join();
  76.         } catch(InterruptedException e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.    
  81.     public void run() {
  82.        
  83.         this.requestFocus();
  84.        
  85.         long last = System.nanoTime();
  86.         long now;
  87.        
  88.         long timer = System.currentTimeMillis();
  89.        
  90.         int frames = 0;
  91.         int ticks = 0;
  92.        
  93.         double delta = 0;
  94.        
  95.         double ns = 1000000000 / 60;
  96.        
  97.         boolean render = true;
  98.        
  99.         while(running) {
  100.            
  101.             now = System.nanoTime();
  102.            
  103.             delta += (now - last) / ns;
  104.            
  105.             last = now;
  106.            
  107.             while(delta >= 1) {
  108.                 tick();
  109.                 ticks++;
  110.                 render = true;
  111.                 delta--;
  112.             }
  113.            
  114.             try {
  115.                 thread.sleep(2);
  116.             } catch (InterruptedException e) {
  117.                 // TODO Auto-generated catch block
  118.                 e.printStackTrace();
  119.             }
  120.            
  121.             if(render) {
  122.                 render();
  123.                 frames++;
  124.             }
  125.            
  126.             if(System.currentTimeMillis() - timer >= 1000) {
  127.                 System.out.println(ticks + " " + frames);
  128.                 frames = 0;
  129.                 ticks = 0;
  130.                 timer += 1000;
  131.             }
  132.            
  133.         }
  134.        
  135.         stop();
  136.        
  137.     }
  138.    
  139.     public void tick() {
  140.         game.tick();
  141.        
  142.         for(int i = 0; i < game.entities.size(); i++) {
  143.            
  144.             if(game.entities.get(i).getID() == ID.Player) {
  145.                 camera.setPos(game.entities.get(i));
  146.             }
  147.            
  148.         }
  149.     }
  150.    
  151.     public void render() {
  152.        
  153.         BufferStrategy bs = this.getBufferStrategy();
  154.        
  155.         if(bs == null) {
  156.             this.createBufferStrategy(3);
  157.             return;
  158.         }
  159.        
  160.         Graphics g = bs.getDrawGraphics();
  161.        
  162.         Graphics2D g2d = (Graphics2D) g;
  163.        
  164.         g2d.translate(-camera.getX(), -camera.getY());
  165.        
  166.         g.clearRect((int)camera.getX(), (int)camera.getY(), WIDTH, HEIGHT);
  167.        
  168.         g.setColor(Color.RED);
  169.         g.fillRect((int)camera.getX(), (int)camera.getY(), WIDTH, HEIGHT);
  170.        
  171.         game.render(g);
  172.        
  173.         g2d.translate(camera.getX(), camera.getY());
  174.        
  175.         g.dispose();
  176.         bs.show();
  177.        
  178.     }
  179.    
  180.     private void loadLevel(BufferedImage image) {
  181.        
  182.         int W = image.getWidth();
  183.         int H = image.getHeight();
  184.        
  185.         for(int y = 0; y < H; y++) {
  186.             for(int x = 0; x < W; x++) {
  187.                
  188.                 int pixel = image.getRGB(x, y);
  189.                
  190.                 int red = (pixel >> 16) & 0xff;
  191.                
  192.                 int green = (pixel >> 8) & 0xff;
  193.                
  194.                 int blue = (pixel) & 0xff;
  195.            
  196.            
  197.                 if(red == 255) {
  198.                     Game.entities.add(new Block(x*32, y*32));
  199.                 }
  200.                
  201.                 if(blue == 255) {
  202.                     Game.entities.add(new Player(x*32, y*32));
  203.                 }
  204.             }
  205.         }
  206.        
  207.     }
  208.    
  209.     public static void main(String[] args) {
  210.         new MainComponent().start();;
  211.     }
  212.    
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement