You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
2.9 KiB
127 lines
2.9 KiB
10 years ago
|
package mightypork.gamecore.util.math.constraints.rect.builders;
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
import mightypork.gamecore.logging.Log;
|
||
|
import mightypork.gamecore.util.math.constraints.num.Num;
|
||
|
import mightypork.gamecore.util.math.constraints.rect.Rect;
|
||
|
import mightypork.gamecore.util.math.constraints.rect.proxy.RectProxy;
|
||
|
import mightypork.gamecore.util.math.constraints.vect.Vect;
|
||
10 years ago
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Utility for cutting rect into evenly sized cells.
|
||
10 years ago
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
|
public class TiledRect extends RectProxy {
|
||
|
|
||
|
final private int tilesY;
|
||
|
final private int tilesX;
|
||
|
final private Num perRow;
|
||
|
final private Num perCol;
|
||
|
|
||
10 years ago
|
/** Left top tile */
|
||
10 years ago
|
private Rect aTile;
|
||
10 years ago
|
|
||
10 years ago
|
|
||
10 years ago
|
public TiledRect(Rect source, int horizontal, int vertical)
|
||
|
{
|
||
10 years ago
|
super(source);
|
||
|
this.tilesX = horizontal;
|
||
|
this.tilesY = vertical;
|
||
|
|
||
|
this.perRow = height().div(vertical);
|
||
|
this.perCol = width().div(horizontal);
|
||
10 years ago
|
|
||
|
this.aTile = Rect.make(origin(), perCol, perRow);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Set tile overlap. Applies only to tile, not span.
|
||
|
*
|
||
|
* @param overlap how far to overlap to neighbouring tiles on all sides
|
||
|
*/
|
||
|
public void setOverlap(double overlap)
|
||
|
{
|
||
|
aTile = aTile.grow(overlap);
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Get a tile.
|
||
10 years ago
|
*
|
||
10 years ago
|
* @param x x position
|
||
|
* @param y y position
|
||
10 years ago
|
* @return tile
|
||
|
* @throws IndexOutOfBoundsException when invalid index is specified.
|
||
|
*/
|
||
|
public Rect tile(int x, int y)
|
||
|
{
|
||
10 years ago
|
if (x >= tilesX || x < 0) { throw new IndexOutOfBoundsException("X coordinate out fo range: " + x); }
|
||
10 years ago
|
|
||
10 years ago
|
if (y >= tilesY || y < 0) { throw new IndexOutOfBoundsException("Y coordinate out of range: " + y); }
|
||
10 years ago
|
|
||
10 years ago
|
return aTile.move(perCol.mul(x), perRow.mul(y));
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get a span (tile spanning across multiple cells)
|
||
|
*
|
||
10 years ago
|
* @param x x start position
|
||
|
* @param y y start position
|
||
10 years ago
|
* @param size_x horizontal size (columns)
|
||
|
* @param size_y vertical size (rows)
|
||
10 years ago
|
* @return tile the tile
|
||
|
* @throws IndexOutOfBoundsException when invalid index is specified.
|
||
|
*/
|
||
10 years ago
|
public Rect span(int x, int y, int size_x, int size_y)
|
||
10 years ago
|
{
|
||
10 years ago
|
final int x_to = x + size_x - 1;
|
||
|
final int y_to = y + size_y - 1;
|
||
10 years ago
|
|
||
|
if (size_x <= 0 || size_y <= 0) {
|
||
10 years ago
|
Log.w("Size must be > 0.", new IllegalAccessException());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if (x >= tilesX || x < 0 || x_to >= tilesX || x_to < 0) {
|
||
10 years ago
|
Log.w("X coordinate(s) out of range.", new IllegalAccessException());
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (y >= tilesY || y < 0 || y_to >= tilesY || y_to < 0) {
|
||
10 years ago
|
Log.w("Y coordinate(s) out of range.", new IllegalAccessException());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
final Vect orig = origin().add(perCol.mul(x), perRow.mul(y));
|
||
10 years ago
|
|
||
10 years ago
|
return Rect.make(orig, perCol.mul(size_x), perRow.mul(size_y));
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Get n-th column
|
||
10 years ago
|
*
|
||
10 years ago
|
* @param n column index
|
||
10 years ago
|
* @return the column tile
|
||
|
* @throws IndexOutOfBoundsException when invalid index is specified.
|
||
|
*/
|
||
|
public Rect column(int n)
|
||
|
{
|
||
10 years ago
|
return tile(n, 0);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Get n-th row
|
||
10 years ago
|
*
|
||
10 years ago
|
* @param n row index
|
||
10 years ago
|
* @return the row rect
|
||
|
* @throws IndexOutOfBoundsException when invalid index is specified.
|
||
|
*/
|
||
|
public Rect row(int n)
|
||
|
{
|
||
10 years ago
|
return tile(0, n);
|
||
10 years ago
|
}
|
||
|
}
|