Advertisement
Guest User

Corner Lord

a guest
Jul 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. public abstract class Component {
  2.  
  3.     private Game game;
  4.     private Entity parent;
  5.    
  6.     protected String tag;
  7.    
  8.     public Component(Game game, Entity parent) {
  9.         this.game =game;
  10.         this.parent = parent;
  11.     }
  12.     public abstract void tick();
  13.     public abstract void render(Screen screen);
  14.    
  15.     public Game getGame() {
  16.         return game;
  17.     }
  18.     public void setGame(Game game) {
  19.         this.game = game;
  20.     }
  21.     public Entity getParent() {
  22.         return parent;
  23.     }
  24.     public void setParent(Entity parent) {
  25.         this.parent = parent;
  26.     }
  27.     public String getTag() {
  28.         return tag;
  29.     }
  30.     public void setTag(String tag) {
  31.         this.tag = tag;
  32.     }
  33. }
  34.  
  35. //
  36.  
  37. public class AABBComponent extends Component {
  38.  
  39.     private int centerX, centerY;
  40.     private int halfWidth, halfHeight;
  41.    
  42.     public AABBComponent(Game game, Entity parent) {
  43.         super(game, parent);
  44.         this.tag = "aabb";
  45.     }
  46.  
  47.     @Override
  48.     public void tick() {
  49.         double time = 1.0/ 60.0;
  50.         @SuppressWarnings("unused")
  51.         float dt = (float) time;
  52.        
  53.         centerX = (int) (getParent().getPosX() + (getParent().getWidth() / 2));
  54.         centerY = (int) (getParent().getPosY() + (getParent().getHeight() / 2) + getParent().getPaddingTop() / 2);
  55.         halfWidth = (getParent().getWidth()/2) - getParent().getPadding();
  56.         halfHeight = (getParent().getHeight()/2) - (getParent().getPaddingTop() / 2);
  57.        
  58.         Physics.addAABBComponent(this);
  59.     }
  60.  
  61.     @Override
  62.     public void render(Screen screen) {
  63.        
  64.     }
  65.  
  66.     public int getCenterX() {
  67.         return centerX;
  68.     }
  69.  
  70.     public void setCenterX(int centerX) {
  71.         this.centerX = centerX;
  72.     }
  73.  
  74.     public int getCenterY() {
  75.         return centerY;
  76.     }
  77.  
  78.     public void setCenterY(int centerY) {
  79.         this.centerY = centerY;
  80.     }
  81.  
  82.     public int getHalfWidth() {
  83.         return halfWidth;
  84.     }
  85.  
  86.     public void setHalfWidth(int halfWidth) {
  87.         this.halfWidth = halfWidth;
  88.     }
  89.  
  90.     public int getHalfHeight() {
  91.         return halfHeight;
  92.     }
  93.  
  94.     public void setHalfHeight(int halfHeight) {
  95.         this.halfHeight = halfHeight;
  96.     }
  97. }
  98.  
  99. //
  100.  
  101. public class Physics {
  102.  
  103.     private static List<AABBComponent> aabbList = new ArrayList<AABBComponent>();
  104.    
  105.     public static void addAABBComponent(AABBComponent aabb) {
  106.         aabbList.add(aabb);
  107.     }
  108.    
  109.     public static void tick() {
  110.         for (int i = 0; i < aabbList.size(); i++) {
  111.             for (int j = i + 1; j < aabbList.size(); j++) {
  112.                 AABBComponent c0 = aabbList.get(i);
  113.                 AABBComponent c1 = aabbList.get(j);
  114.                 if(Math.abs(c0.getCenterX() - c1.getCenterX()) < c0.getHalfWidth() + c1.getHalfWidth()) {
  115.                     if(Math.abs(c0.getCenterY() - c1.getCenterY()) < c0.getHalfHeight() + c1.getHalfHeight()) {
  116.                         c0.getParent().collision(c1.getParent());
  117.                         c1.getParent().collision(c0.getParent());
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.        
  123.         aabbList.clear();
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement