Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. package twodee;
  2.  
  3. import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
  4. import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
  5. import static org.lwjgl.opengl.GL11.GL_PROJECTION;
  6. import static org.lwjgl.opengl.GL11.glClear;
  7. import static org.lwjgl.opengl.GL11.glLoadIdentity;
  8. import static org.lwjgl.opengl.GL11.glMatrixMode;
  9. import static org.lwjgl.opengl.GL11.glOrtho;
  10. import static org.lwjgl.opengl.GL11.glRectd;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. import org.lwjgl.LWJGLException;
  15. import org.lwjgl.opengl.Display;
  16. import org.lwjgl.opengl.DisplayMode;
  17.  
  18. public class Renderer {
  19.    
  20.     private Engine engine;
  21.     private static int DISPLAY_X=1024, DISPLAY_Y=768;
  22.    
  23.     public Renderer(Engine engine) {
  24.         this.engine=engine;
  25.         try {
  26.             Display.setDisplayMode(new DisplayMode(DISPLAY_X, DISPLAY_Y));
  27.             Display.setTitle("my2DGameEngine");
  28.             Display.create();
  29.         } catch (LWJGLException e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.    
  34.     public void init() {
  35.         glMatrixMode(GL_PROJECTION);
  36.         glLoadIdentity();
  37.         glOrtho(0, DISPLAY_X, DISPLAY_Y, 0, 1, -1);
  38.         glMatrixMode(GL_MODELVIEW);
  39.         engine.boxes.add(new Box(500,500,50,50));
  40.         engine.boxes.add(new Box(100,200,50,50));
  41.         engine.boxes.add(new Box(200,300,50,50));
  42.     }
  43.    
  44.     public void draw() {
  45.         glClear(GL_COLOR_BUFFER_BIT);      
  46.         for (Box box : engine.boxes) {
  47.             box.draw();
  48.         }      
  49.         Display.update();
  50.         Display.sync(60);
  51.     }
  52.    
  53.     public void destroy() {
  54.         Display.destroy();
  55.     }
  56.    
  57.     public boolean isCloseRequested() {
  58.         return Display.isCloseRequested();
  59.     }
  60. }
  61.  
  62.  
  63. package twodee;
  64.  
  65. import org.lwjgl.opengl.Display;
  66. import java.util.ArrayList;
  67.  
  68. public class Engine extends Thread {
  69.  
  70.     public ArrayList<Box> boxes = new ArrayList<Box>();
  71.     private Renderer render;
  72.     private static Engine engine;
  73.    
  74.     public Engine() {
  75.         //nothing
  76.     }
  77.    
  78.     public static Engine instance() {
  79.         if (engine == null) {
  80.             engine = new Engine();
  81.         }
  82.         return engine;
  83.     }
  84.    
  85.     public void run() {
  86.         render = new Renderer(instance());
  87.         render.init();
  88.         while (!render.isCloseRequested()) {
  89.             this.update();
  90.             this.collision();
  91.             render.draw();     
  92.         }
  93.         render.destroy();
  94.     }
  95.    
  96.     public void update() {
  97.         for (Box box : engine.boxes) {
  98.             box.update();
  99.         }
  100.     }
  101.    
  102.     public void collision() {
  103.         for (Box box : engine.boxes) {
  104.             for (Box other : engine.boxes) {
  105.                 if (box.intersects(other)) {
  106.                     if (box!=other) {
  107.                         box.collided();
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116. package twodee;
  117.  
  118. import static org.lwjgl.opengl.GL11.glRectd;
  119.  
  120. import java.awt.Rectangle;
  121.  
  122. public class Box {
  123.    
  124.     private double x,y,h,w;
  125.     private double directionx, directiony;
  126.     protected Rectangle hitbox = new Rectangle();
  127.    
  128.     public double boundx=1024, boundy=768;
  129.    
  130.     public Box(double x, double y, double w, double h) {
  131.         this.x=x; this.y=y; this.h=h; this.w=w;
  132.        
  133.         if(Math.random()>0.5) {
  134.             directionx=1;
  135.         } else {
  136.             directionx=-1;
  137.         }
  138.        
  139.         if (Math.random()>0.5) {
  140.             directiony=1;
  141.         } else {
  142.             directiony=-1;
  143.         }
  144.     }
  145.    
  146.     public double getX() {
  147.         return x;
  148.     }
  149.    
  150.     public double getY() {
  151.         return y;
  152.     }
  153.    
  154.     public double getWidth() {
  155.         return w;
  156.     }
  157.    
  158.     public double getHeight() {
  159.         return h;
  160.     }
  161.    
  162.     public void update() {     
  163.         if (x+w>boundx) {
  164.             directionx = -1;
  165.         }
  166.        
  167.         if (y+h>boundy) {
  168.             directiony = -1;
  169.         }
  170.        
  171.         if (x<0) {
  172.             directionx = 1;
  173.         }
  174.        
  175.         if (y<0) {
  176.             directiony = 1;
  177.         }
  178.         this.x = x + directionx*1;
  179.         this.y = y + directiony*2;
  180.     }
  181.    
  182.     public boolean intersects(Box other) {
  183.         hitbox.setBounds((int) x, (int) y, (int) w, (int)h );
  184.         return hitbox.intersects(other.getX(), other.getY(), other.getWidth(), other.getHeight());
  185.     }
  186.    
  187.     public void draw() {
  188.         glRectd(x, y, x + w, y + h);
  189.     }
  190.    
  191.     public void collided() {
  192.         if (directionx==-1) {
  193.             directionx=1;
  194.         } else {
  195.             directionx=-1;
  196.         }
  197.         if (directiony==-1) {
  198.             directiony=1;
  199.         } else {
  200.             directiony=-1;
  201.         }
  202.     }
  203. }
  204.  
  205.  
  206. import twodee.Engine;
  207.  
  208. public class my2DEngineGame {
  209.  
  210.     /**
  211.      * @param args
  212.      */
  213.     public static void main(String[] args) {
  214.         Engine engine = new Engine();
  215.         engine.start();
  216.     }
  217.  
  218. }
Add Comment
Please, Sign In to add comment