Advertisement
Guest User

Untitled

a guest
Feb 27th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. class RSArea
  2. {
  3.     Polygon p; //Polygon for the area
  4.     //Creates a polygon from an array of tiles
  5.     public RSArea(RSTile[] tiles)
  6.     {
  7.         int[] xTiles = new int[tiles.length];
  8.         int[] yTiles = new int[tiles.length];
  9.         for(int i = 0; i < tiles.length; i++)
  10.         {
  11.             xTiles[i] = tiles[i].getX();
  12.             yTiles[i] = tiles[i].getY();
  13.         }
  14.         p = new Polygon(xTiles, yTiles, xTiles.length);
  15.     }
  16.     //Creates a rectangular area from 2 tiles
  17.     public RSArea(RSTile min, RSTile max)
  18.     {
  19.         p = new Polygon(new int[]{min.getX(), min.getX(), max.getX(), max.getX()}, new int[]{min.getY(), max.getY(), max.getY(), min.getY()}, 4);
  20.     }
  21.     //Creates an area with a center point and radius
  22.     public RSArea(RSTile center, int rad)
  23.     {
  24.         p = new Polygon(new int[]{center.getX() - rad, center.getX() - rad, center.getX() + rad, center.getX() + rad}, new int[]{center.getY() - rad, center.getY() + rad, center.getY() + rad, center.getY() - rad}, 4);
  25.     }
  26.     //Checkes if the polygon contains the tile
  27.     public boolean contains(RSTile tile)
  28.     {
  29.         return p.contains(tile.getX(), tile.getY());
  30.     }
  31.     //Gets the tile at the coords
  32.     public RSTile getTile(int x, int y)
  33.     {
  34.         return p.contains(x, y) ? new RSTile(x, y) : null;
  35.     }
  36.     //Draws the area
  37.     public void drawArea(Graphics g)
  38.     {
  39.         Rectangle2D b = p.getBounds2D();
  40.         for(int x = (int)b.getMinX(); x <= (int)b.getMaxX(); x++)
  41.         {
  42.             for (int y = (int)b.getMinX(); y <= (int)b.getMaxY(); y++)
  43.             {
  44.                 if (p.contains(x, y))
  45.                     drawTile(new RSTile(x, y), (Graphics2D)g, false);
  46.             }
  47.         }
  48.     }
  49.     //Draws the tile
  50.     private void drawTile(RSTile tile, Graphics2D g, boolean fill)
  51.     {
  52.         if (tile.isOnScreen())
  53.         {
  54.             if (fill)
  55.                 g.fillPolygon(Projection.getTileBoundsPoly(tile, 0));
  56.             else
  57.                 g.drawPolygon(Projection.getTileBoundsPoly(tile, 0));
  58.         }
  59.     }
  60.     //Gets a random tile from the area
  61.     public RSTile getRandomTile()
  62.     {
  63.         Rectangle2D bounds = p.getBounds2D();
  64.         RSTile tile = new RSTile(General.random((int)bounds.getMinX(), (int)bounds.getMaxX()), General.random((int)bounds.getMinY(), (int)bounds.getMaxY()));
  65.         return p.contains(tile.getX(), tile.getY()) ? tile : getRandomTile();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement