use of com.watabou.utils.Rect in project pixel-dungeon-remix by NYRDS.
the class RegularLevel method joinRooms.
protected boolean joinRooms(Room r, Room n) {
if (r.type != Room.Type.STANDARD || n.type != Room.Type.STANDARD) {
return false;
}
Rect w = r.intersect(n);
if (w.left == w.right) {
if (w.bottom - w.top < 3) {
return false;
}
if (w.height() == Math.max(r.height(), n.height())) {
return false;
}
if (r.width() + n.width() > maxRoomSize) {
return false;
}
w.top += 1;
w.bottom -= 0;
w.right++;
Painter.fill(this, w.left, w.top, 1, w.height(), Terrain.EMPTY);
} else {
if (w.right - w.left < 3) {
return false;
}
if (w.width() == Math.max(r.width(), n.width())) {
return false;
}
if (r.height() + n.height() > maxRoomSize) {
return false;
}
w.left += 1;
w.right -= 0;
w.bottom++;
Painter.fill(this, w.left, w.top, w.width(), 1, Terrain.EMPTY);
}
return true;
}
use of com.watabou.utils.Rect in project shattered-pixel-dungeon-gdx by 00-Evan.
the class Builder method placeRoom.
// Attempts to place a room such that the angle between the center of the previous room
// and it matches the given angle ([0-360), where 0 is straight up) as closely as possible.
// Note that getting an exactly correct angle is harder the closer that angle is to diagonal.
// Returns the exact angle between the centerpoints of the two rooms, or -1 if placement fails.
protected static float placeRoom(ArrayList<Room> collision, Room prev, Room next, float angle) {
// wrap angle around to always be [0-360)
angle %= 360f;
if (angle < 0) {
angle += 360f;
}
PointF prevCenter = new PointF((prev.left + prev.right) / 2f, (prev.top + prev.bottom) / 2f);
// calculating using y = mx+b, straight line formula
double m = Math.tan(angle / A + Math.PI / 2.0);
double b = prevCenter.y - m * prevCenter.x;
// using the line equation, we find the point along the prev room where the line exists
Point start;
int direction;
if (Math.abs(m) >= 1) {
if (angle < 90 || angle > 270) {
direction = Room.TOP;
start = new Point((int) Math.round((prev.top - b) / m), prev.top);
} else {
direction = Room.BOTTOM;
start = new Point((int) Math.round((prev.bottom - b) / m), prev.bottom);
}
} else {
if (angle < 180) {
direction = Room.RIGHT;
start = new Point(prev.right, (int) Math.round(m * prev.right + b));
} else {
direction = Room.LEFT;
start = new Point(prev.left, (int) Math.round(m * prev.left + b));
}
}
// cap it to a valid connection point for most rooms
if (direction == Room.TOP || direction == Room.BOTTOM) {
start.x = (int) GameMath.gate(prev.left + 1, start.x, prev.right - 1);
} else {
start.y = (int) GameMath.gate(prev.top + 1, start.y, prev.bottom - 1);
}
// space checking
Rect space = findFreeSpace(start, collision, Math.max(next.maxWidth(), next.maxHeight()));
if (!next.setSizeWithLimit(space.width() + 1, space.height() + 1)) {
return -1;
}
// find the ideal center for this new room using the line equation and known dimensions
PointF targetCenter = new PointF();
if (direction == Room.TOP) {
targetCenter.y = prev.top - (next.height() - 1) / 2f;
targetCenter.x = (float) ((targetCenter.y - b) / m);
next.setPos(Math.round(targetCenter.x - (next.width() - 1) / 2f), prev.top - (next.height() - 1));
} else if (direction == Room.BOTTOM) {
targetCenter.y = prev.bottom + (next.height() - 1) / 2f;
targetCenter.x = (float) ((targetCenter.y - b) / m);
next.setPos(Math.round(targetCenter.x - (next.width() - 1) / 2f), prev.bottom);
} else if (direction == Room.RIGHT) {
targetCenter.x = prev.right + (next.width() - 1) / 2f;
targetCenter.y = (float) (m * targetCenter.x + b);
next.setPos(prev.right, Math.round(targetCenter.y - (next.height() - 1) / 2f));
} else if (direction == Room.LEFT) {
targetCenter.x = prev.left - (next.width() - 1) / 2f;
targetCenter.y = (float) (m * targetCenter.x + b);
next.setPos(prev.left - (next.width() - 1), Math.round(targetCenter.y - (next.height() - 1) / 2f));
}
// perform connection bounds and target checking, move the room if necessary
if (direction == Room.TOP || direction == Room.BOTTOM) {
if (next.right < prev.left + 2)
next.shift(prev.left + 2 - next.right, 0);
else if (next.left > prev.right - 2)
next.shift(prev.right - 2 - next.left, 0);
if (next.right > space.right)
next.shift(space.right - next.right, 0);
else if (next.left < space.left)
next.shift(space.left - next.left, 0);
} else {
if (next.bottom < prev.top + 2)
next.shift(0, prev.top + 2 - next.bottom);
else if (next.top > prev.bottom - 2)
next.shift(0, prev.bottom - 2 - next.top);
if (next.bottom > space.bottom)
next.shift(0, space.bottom - next.bottom);
else if (next.top < space.top)
next.shift(0, space.top - next.top);
}
// attempt to connect, return the result angle if successful.
if (next.connect(prev)) {
return angleBetweenRooms(prev, next);
} else {
return -1;
}
}
use of com.watabou.utils.Rect in project shattered-pixel-dungeon-gdx by 00-Evan.
the class CavesPainter method decorate.
@Override
protected void decorate(Level level, ArrayList<Room> rooms) {
int w = level.width();
int l = level.length();
int[] map = level.map;
for (Room room : rooms) {
if (!(room instanceof EmptyRoom || room instanceof CaveRoom)) {
continue;
}
if (room.width() <= 4 || room.height() <= 4) {
continue;
}
int s = room.square();
if (Random.Int(s) > 8) {
int corner = (room.left + 1) + (room.top + 1) * w;
if (map[corner - 1] == Terrain.WALL && map[corner - w] == Terrain.WALL) {
map[corner] = Terrain.WALL;
level.traps.remove(corner);
}
}
if (Random.Int(s) > 8) {
int corner = (room.right - 1) + (room.top + 1) * w;
if (map[corner + 1] == Terrain.WALL && map[corner - w] == Terrain.WALL) {
map[corner] = Terrain.WALL;
level.traps.remove(corner);
}
}
if (Random.Int(s) > 8) {
int corner = (room.left + 1) + (room.bottom - 1) * w;
if (map[corner - 1] == Terrain.WALL && map[corner + w] == Terrain.WALL) {
map[corner] = Terrain.WALL;
level.traps.remove(corner);
}
}
if (Random.Int(s) > 8) {
int corner = (room.right - 1) + (room.bottom - 1) * w;
if (map[corner + 1] == Terrain.WALL && map[corner + w] == Terrain.WALL) {
map[corner] = Terrain.WALL;
level.traps.remove(corner);
}
}
for (Room n : room.connected.keySet()) {
if ((n instanceof StandardRoom || n instanceof ConnectionRoom) && Random.Int(3) == 0) {
Painter.set(level, room.connected.get(n), Terrain.EMPTY_DECO);
}
}
}
for (int i = w + 1; i < l - w; i++) {
if (map[i] == Terrain.EMPTY) {
int n = 0;
if (map[i + 1] == Terrain.WALL) {
n++;
}
if (map[i - 1] == Terrain.WALL) {
n++;
}
if (map[i + w] == Terrain.WALL) {
n++;
}
if (map[i - w] == Terrain.WALL) {
n++;
}
if (Random.Int(6) <= n) {
map[i] = Terrain.EMPTY_DECO;
}
}
}
for (int i = 0; i < l - w; i++) {
if (map[i] == Terrain.WALL && DungeonTileSheet.floorTile(map[i + w]) && Random.Int(4) == 0) {
map[i] = Terrain.WALL_DECO;
}
}
for (Room r : rooms) {
if (r instanceof EmptyRoom) {
for (Room n : r.neigbours) {
if (n instanceof EmptyRoom && !r.connected.containsKey(n)) {
Rect i = r.intersect(n);
if (i.left == i.right && i.bottom - i.top >= 5) {
i.top += 2;
i.bottom -= 1;
i.right++;
Painter.fill(level, i.left, i.top, 1, i.height(), Terrain.CHASM);
} else if (i.top == i.bottom && i.right - i.left >= 5) {
i.left += 2;
i.right -= 1;
i.bottom++;
Painter.fill(level, i.left, i.top, i.width(), 1, Terrain.CHASM);
}
}
}
}
}
}
use of com.watabou.utils.Rect in project shattered-pixel-dungeon-gdx by 00-Evan.
the class Room method canConnect.
// considers both direction and point limits
public boolean canConnect(Room r) {
Rect i = intersect(r);
boolean foundPoint = false;
for (Point p : i.getPoints()) {
if (canConnect(p) && r.canConnect(p)) {
foundPoint = true;
break;
}
}
if (!foundPoint)
return false;
if (i.width() == 0 && i.left == left)
return canConnect(LEFT) && r.canConnect(LEFT);
else if (i.height() == 0 && i.top == top)
return canConnect(TOP) && r.canConnect(TOP);
else if (i.width() == 0 && i.right == right)
return canConnect(RIGHT) && r.canConnect(RIGHT);
else if (i.height() == 0 && i.bottom == bottom)
return canConnect(BOTTOM) && r.canConnect(BOTTOM);
else
return false;
}
use of com.watabou.utils.Rect in project shattered-pixel-dungeon-gdx by 00-Evan.
the class RingTunnelRoom method paint.
@Override
public void paint(Level level) {
super.paint(level);
int floor = level.tunnelTile();
Rect ring = getConnectionSpace();
Painter.fill(level, ring.left, ring.top, 3, 3, floor);
Painter.fill(level, ring.left + 1, ring.top + 1, 1, 1, Terrain.WALL);
}
Aggregations