Advertisement
Guest User

Area.java

a guest
Mar 10th, 2015
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package com;
  2.  
  3. import java.awt.Rectangle;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. public class Area {
  9.     private static final Random rand = new Random();
  10.    
  11.     public int x, y, w, h;
  12.     public boolean visible = false;
  13.     public List<Rectangle> objects = new ArrayList<>();
  14.  
  15.     public Area(int x, int y, int width, int height) {
  16.         this.x = x;
  17.         this.y = y;
  18.         this.w = width;
  19.         this.h = height;
  20.     }
  21.    
  22.     public void fillObjects() {
  23.         int count = 10 + rand.nextInt(10);
  24.         loop: for (int i = 0; i < count; i++) {
  25.             int ww = w / 3 + rand.nextInt(3);
  26.             int hh = h / 3 + rand.nextInt(3);
  27.             int xx = x + 3 + rand.nextInt(w - 6 - ww);
  28.             int yy = y + 3 + rand.nextInt(h - 6 - hh);
  29.            
  30.             if (i < 5) {
  31.                 int offs = 10;
  32.                 ww = w - offs;
  33.                 hh = h - offs;
  34.                 xx = x + offs / 2 + 1;
  35.                 yy = y + offs / 2 + 1;
  36.                 if (rand.nextInt(4) != 0) {
  37.                     ww /= 2;
  38.                     if (rand.nextBoolean()) xx += ww;
  39.                 }
  40.                 if (rand.nextInt(4) != 0) {
  41.                     hh /= 2;
  42.                     if (rand.nextBoolean()) yy += hh;
  43.                 }
  44.             }
  45.            
  46.             Rectangle obj = new Rectangle(xx, yy, ww, hh);
  47.             for (Rectangle r : objects) {
  48.                 if (obj.intersects(r)) continue loop;
  49.             }
  50.            
  51.             objects.add(obj);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement