Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Riven (posted 2014-07-02 19:10:01, viewed 39 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188  
class Circle {
   public float x, y, radius;

   public boolean intersects(Circle that) {
      float dx = this.x - that.x;
      float dy = this.y - that.y;
      float rr = this.radius + that.radius;
      return (dx * dx + dy * dy) < (rr * rr);
   }
}

class Rect {
   public final Vec2 min, max;

   public Rect() {
      this(new Vec2(), new Vec2());
   }

   public Rect(Vec2 min, Vec2 max) {
      this.min = min;
      this.max = max;
   }

   public boolean intersects(Rect that) {
      if(this.min.x > that.max.x)
         return false;
      if(this.min.y > that.max.y)
         return false;
      if(this.max.x < that.min.x)
         return false;
      if(this.max.y < that.min.y)
         return false;
      return true;
   }

   public boolean intersects(Circle that) {
      if(this.min.x > that.x + that.radius)
         return false;
      if(this.min.y > that.y + that.radius)
         return false;
      if(this.max.x < that.x - that.radius)
         return false;
      if(this.max.y < that.y - that.radius)
         return false;
      return true;
   }
}

class ItemScene {
   private final List<ItemGrid> lodGrids;

   public ItemScene(Rect bounds, float minCellSize) {
      this.lodGrids = new ArrayList<>();

      for(int i = 0; true; i++) {
         int cols = (1 << i);
         int rows = (1 << i);
         ItemGrid grid = new ItemGrid(cols, rows, bounds);

         if(grid.getCellWidth() < minCellSize)
            break;
         if(grid.getCellHeight() < minCellSize)
            break;

         this.lodGrids.add(grid);
      }

      if(this.lodGrids.isEmpty())
         throw new IllegalStateException();
   }

   public void put(Object attachment, Circle boundingCircle) {
      Item item = new Item(boundingCircle, attachment);

      // leafs --> root
      for(int i = lodGrids.size() - 1; i >= 0; i--) {
         ItemGrid lodGrid = lodGrids.get(i);
         if(i == 0 || lodGrid.fits(item)) {
            lodGrid.put(item);
            return;
         }
      }
   }

   public void query(Rect queryArea, List<Object> result, int sinceItemId) {
      for(int i = lodGrids.size() - 1; i >= 0; i--) {
         lodGrids.get(i).query(queryArea, result, sinceItemId);
      }
   }
}

class ItemGrid {
   private final int cols;
   private final int rows;
   private final Vec2 min;
   private final Vec2 max;
   private List<Item>[][] cells;
   private int lastItemId;

   @SuppressWarnings("unchecked")
   ItemGrid(int cols, int rows, Rect rect) {
      this.cols = cols;
      this.rows = rows;
      this.min = rect.min;
      this.max = rect.max;
      this.cells = (List<Item>[][]) new List[rows][cols];
      this.lastItemId = Integer.MAX_VALUE;
   }

   void query(Rect area, List<Object> result, int sinceItemId) {
      if(sinceItemId >= lastItemId) {
         return;
      }

      int minCol = Math.max(0, Math.min(cols - 1, this.getCol(area.min.x) - 1));
      int maxCol = Math.max(0, Math.min(cols - 1, this.getCol(area.max.x) + 1));
      int minRow = Math.max(0, Math.min(rows - 1, this.getRow(area.min.y) - 1));
      int maxRow = Math.max(0, Math.min(rows - 1, this.getRow(area.max.y) + 1));

      for(int row = minRow; row <= maxRow; row++) {
         for(int col = minCol; col <= maxCol; col++) {
            List<Item> items = cells[row][col];
            if(items == null) {
               continue;
            }

            // newest --> oldest
            for(int i = items.size() - 1; i >= 0; i--) {
               Item item = items.get(i);
               if(item.id <= sinceItemId) {
                  break;
               }

               if(area.intersects(item.boundingCircle)) {
                  result.add(item.attachment);
               }
            }
         }
      }
   }

   boolean fits(Item item) {
      float minCellDim = Math.min(this.getCellWidth(), this.getCellHeight());
      return item.boundingCircle.radius < minCellDim * 0.5f;
   }

   void put(Item item) {
      int col = Math.max(0, Math.min(cols - 1, this.getCol(item.boundingCircle.x)));
      int row = Math.max(0, Math.min(rows - 1, this.getRow(item.boundingCircle.y)));

      List<Item> cell = cells[row][col];
      if(cell == null) {
         cell = cells[row][col] = new ArrayList<>();
      }
      cell.add(item);

      lastItemId = item.id;
   }

   float getCellWidth() {
      return (max.x - min.x) / cols;
   }

   float getCellHeight() {
      return (max.y - min.y) / rows;
   }

   private int getCol(float x) {
      return (int) ((x - min.x) / (max.x - min.x) * cols);
   }

   private int getRow(float y) {
      return (int) ((y - min.y) / (max.y - min.y) * rows);
   }
}

class Item {
   private static int ID_GEN;
   final int id;
   final Circle boundingCircle;
   final Object attachment;

   Item(Circle circle, Object attachment) {
      this.id = ++ID_GEN;
      this.boundingCircle = circle;
      this.attachment = attachment;
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!