Java Code Examples for com.watabou.utils.Rect#width()

The following examples show how to use com.watabou.utils.Rect#width() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Room.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
public int curConnections(int direction){
	if (direction == ALL) {
		return connected.size();
		
	} else {
		int total = 0;
		for (Room r : connected.keySet()){
			Rect i = intersect( r );
			if      (direction == LEFT && i.width() == 0 && i.left == left)         total++;
			else if (direction == TOP && i.height() == 0 && i.top == top)           total++;
			else if (direction == RIGHT && i.width() == 0 && i.right == right)      total++;
			else if (direction == BOTTOM && i.height() == 0 && i.bottom == bottom)  total++;
		}
		return total;
	}
}
 
Example 2
Source File: Room.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public int curConnections(int direction){
	if (direction == ALL) {
		return connected.size();
		
	} else {
		int total = 0;
		for (Room r : connected.keySet()){
			Rect i = intersect( r );
			if      (direction == LEFT && i.width() == 0 && i.left == left)         total++;
			else if (direction == TOP && i.height() == 0 && i.top == top)           total++;
			else if (direction == RIGHT && i.width() == 0 && i.right == right)      total++;
			else if (direction == BOTTOM && i.height() == 0 && i.bottom == bottom)  total++;
		}
		return total;
	}
}
 
Example 3
Source File: RingBridgeRoom.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paint(Level level) {
	Painter.fill(level, this, 1, Terrain.CHASM);

	super.paint(level);

	for (Room r : neigbours){
		if (r instanceof BridgeRoom || r instanceof RingBridgeRoom || r instanceof WalkwayRoom){
			Rect i = intersect(r);
			if (i.width() != 0){
				i.left++;
				i.right--;
			} else {
				i.top++;
				i.bottom--;
			}
			Painter.fill(level, i.left, i.top, i.width()+1, i.height()+1, Terrain.CHASM);
		}
	}
}
 
Example 4
Source File: BridgeRoom.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paint(Level level) {
	
	if (Math.min(width(), height()) > 3) {
		Painter.fill(level, this, 1, Terrain.CHASM);
	}
	
	super.paint(level);
	
	for (Room r : neigbours){
		if (r instanceof BridgeRoom || r instanceof RingBridgeRoom || r instanceof WalkwayRoom){
			Rect i = intersect(r);
			if (i.width() != 0){
				i.left++;
				i.right--;
			} else {
				i.top++;
				i.bottom--;
			}
			Painter.fill(level, i.left, i.top, i.width()+1, i.height()+1, Terrain.CHASM);
		}
	}
}
 
Example 5
Source File: BridgeRoom.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paint(Level level) {
	
	if (Math.min(width(), height()) > 3) {
		Painter.fill(level, this, 1, Terrain.CHASM);
	}
	
	super.paint(level);
	
	for (Room r : neigbours){
		if (r instanceof BridgeRoom || r instanceof RingBridgeRoom || r instanceof WalkwayRoom){
			Rect i = intersect(r);
			if (i.width() != 0){
				i.left++;
				i.right--;
			} else {
				i.top++;
				i.bottom--;
			}
			Painter.fill(level, i.left, i.top, i.width()+1, i.height()+1, Terrain.CHASM);
		}
	}
}
 
Example 6
Source File: RegularLevel.java    From pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
private void placeDoors( Room r ) {
	for (Room n : r.connected.keySet()) {
		Room.Door door = r.connected.get( n );
		if (door == null) {
			
			Rect i = r.intersect( n );
			if (i.width() == 0) {
				door = new Room.Door( 
					i.left, 
					Random.Int( i.top + 1, i.bottom ) );
			} else {
				door = new Room.Door( 
					Random.Int( i.left + 1, i.right ),
					i.top);
			}

			r.connected.put( n, door );
			n.connected.put( r, door );
		}
	}
}
 
Example 7
Source File: WalkwayRoom.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paint(Level level) {
	
	if (Math.min(width(), height()) > 3) {
		Painter.fill(level, this, 1, Terrain.CHASM);
	}
	
	super.paint(level);
	
	for (Room r : neigbours){
		if (r instanceof BridgeRoom || r instanceof RingBridgeRoom || r instanceof WalkwayRoom){
			Rect i = intersect(r);
			if (i.width() != 0){
				i.left++;
				i.right--;
			} else {
				i.top++;
				i.bottom--;
			}
			Painter.fill(level, i.left, i.top, i.width()+1, i.height()+1, Terrain.CHASM);
		}
	}
}
 
Example 8
Source File: RegularLevel.java    From YetAnotherPixelDungeon with GNU General Public License v3.0 6 votes vote down vote up
private void placeDoors( Room r ) {
	for (Room n : r.connected.keySet()) {
		Room.Door door = r.connected.get( n );
		if (door == null) {
			
			Rect i = r.intersect( n );
			if (i.width() == 0) {
				door = new Room.Door( 
					i.left, 
					Random.IntRange( i.top + 1, i.bottom - 1 ) );
			} else {
				door = new Room.Door( 
					Random.IntRange( i.left + 1, i.right - 1 ),
					i.top);
			}

			r.connected.put( n, door );
			n.connected.put( r, door );
		}
	}
}
 
Example 9
Source File: Room.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public boolean addNeigbour( Room other ) {
	if (neigbours.contains(other))
		return true;
	
	Rect i = intersect( other );
	if ((i.width() == 0 && i.height() >= 2) ||
			(i.height() == 0 && i.width() >= 2)) {
		neigbours.add( other );
		other.neigbours.add( this );
		return true;
	}
	return false;
}
 
Example 10
Source File: Room.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public boolean addNeigbour( Room other ) {
	if (neigbours.contains(other))
		return true;
	
	Rect i = intersect( other );
	if ((i.width() == 0 && i.height() >= 2) ||
		(i.height() == 0 && i.width() >= 2)) {
		neigbours.add( other );
		other.neigbours.add( this );
		return true;
	}
	return false;
}
 
Example 11
Source File: Maze.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public static boolean[][] generate(Rect r, int[] terrain, int width, int filledTerrainType){
	boolean[][] maze = new boolean[r.width()][r.height()];
	for (int x = 0; x < maze.length; x++) {
		for (int y = 0; y < maze[0].length; y++) {
			if (terrain[x + r.left + (y + r.top)*width] == filledTerrainType){
				maze[x][y] = FILLED;
			}
		}
	}
	
	return generate(maze);
}
 
Example 12
Source File: CavesBossLevel.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean build() {
	
	setSize(WIDTH, HEIGHT);

	Rect space = new Rect();

	space.set(
			Random.IntRange(2, 6),
			Random.IntRange(2, 6),
			Random.IntRange(width-6, width-2),
			Random.IntRange(height-6, height-2)
	);

	Painter.fillEllipse( this, space, Terrain.EMPTY );

	exit = space.left + space.width()/2 + (space.top - 1) * width();
	
	map[exit] = Terrain.LOCKED_EXIT;
	
	Painter.fill( this, ROOM_LEFT - 1, ROOM_TOP - 1,
		ROOM_RIGHT - ROOM_LEFT + 3, ROOM_BOTTOM - ROOM_TOP + 3, Terrain.WALL );
	Painter.fill( this, ROOM_LEFT, ROOM_TOP + 1,
		ROOM_RIGHT - ROOM_LEFT + 1, ROOM_BOTTOM - ROOM_TOP, Terrain.EMPTY );

	Painter.fill( this, ROOM_LEFT, ROOM_TOP,
		ROOM_RIGHT - ROOM_LEFT + 1, 1, Terrain.EMPTY_DECO );
	
	arenaDoor = Random.Int( ROOM_LEFT, ROOM_RIGHT ) + (ROOM_BOTTOM + 1) * width();
	map[arenaDoor] = Terrain.DOOR;
	
	entrance = Random.Int( ROOM_LEFT + 1, ROOM_RIGHT - 1 ) +
		Random.Int( ROOM_TOP + 1, ROOM_BOTTOM - 1 ) * width();
	map[entrance] = Terrain.ENTRANCE;
	
	boolean[] patch = Patch.generate( width, height, 0.30f, 6, true );
	for (int i=0; i < length(); i++) {
		if (map[i] == Terrain.EMPTY && patch[i]) {
			map[i] = Terrain.WATER;
		}
	}

	for (int i=0; i < length(); i++) {
		if (map[i] == Terrain.EMPTY && Random.Int( 6 ) == 0) {
			map[i] = Terrain.INACTIVE_TRAP;
			Trap t = new ToxicTrap().reveal();
			t.active = false;
			setTrap(t, i);
		}
	}
	
	for (int i=width() + 1; i < length() - width(); 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+width()] == Terrain.WALL) {
				n++;
			}
			if (map[i-width()] == Terrain.WALL) {
				n++;
			}
			if (Random.Int( 8 ) <= n) {
				map[i] = Terrain.EMPTY_DECO;
			}
		}
	}
	
	for (int i=0; i < length() - width(); i++) {
		if (map[i] == Terrain.WALL
				&& DungeonTileSheet.floorTile(map[i + width()])
				&& Random.Int( 3 ) == 0) {
			map[i] = Terrain.WALL_DECO;
		}
	}
	
	return true;
}
 
Example 13
Source File: RegularPainter.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
protected boolean joinRooms( Level l, Room r, Room n ) {
	
	if (!(r instanceof EmptyRoom && n instanceof EmptyRoom)) {
		return false;
	}
	
	//TODO decide on good probabilities and dimension restrictions
	Rect w = r.intersect( n );
	if (w.left == w.right) {
		
		if (w.bottom - w.top < 3) {
			return false;
		}
		
		if (w.height()+1 == Math.max( r.height(), n.height() )) {
			return false;
		}
		
		if (r.width() + n.width() > 10) {
			return false;
		}
		
		w.top += 1;
		w.bottom -= 0;
		
		w.right++;
		
		Painter.fill( l, w.left, w.top, 1, w.height(), Terrain.EMPTY );
		
	} else {
		
		if (w.right - w.left < 3) {
			return false;
		}
		
		if (w.width()+1 == Math.max( r.width(), n.width() )) {
			return false;
		}
		
		if (r.height() + n.height() > 10) {
			return false;
		}
		
		w.left += 1;
		w.right -= 0;
		
		w.bottom++;
		
		Painter.fill( l, w.left, w.top, w.width(), 1, Terrain.EMPTY );
	}
	
	return true;
}
 
Example 14
Source File: RegularLevel.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
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;
	}
 
Example 15
Source File: Builder.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
protected static Rect findFreeSpace(Point start, ArrayList<Room> collision, int maxSize){
	Rect space = new Rect(start.x-maxSize, start.y-maxSize, start.x+maxSize, start.y+maxSize);

	//shallow copy
	ArrayList<Room> colliding = new ArrayList<>(collision);
	do{
		
		//remove empty rooms and any rooms we aren't currently overlapping
		Iterator<Room> it = colliding.iterator();
		while (it.hasNext()){
			Room room = it.next();
			//if not colliding
			if ( room.isEmpty()
					|| Math.max(space.left, room.left) >= Math.min(space.right, room.right)
					|| Math.max(space.top, room.top) >= Math.min(space.bottom, room.bottom) ){
				it.remove();
			}
		}
		
		//iterate through all rooms we are overlapping, and find the closest one
		Room closestRoom = null;
		int closestDiff = Integer.MAX_VALUE;
		boolean inside = true;
		int curDiff = 0;
		for (Room curRoom : colliding){
			
			if (start.x <= curRoom.left){
				inside = false;
				curDiff += curRoom.left - start.x;
			} else if (start.x >= curRoom.right){
				inside = false;
				curDiff += start.x - curRoom.right;
			}
		
			if (start.y <= curRoom.top){
				inside = false;
				curDiff += curRoom.top - start.y;
			} else if (start.y >= curRoom.bottom){
				inside = false;
				curDiff += start.y - curRoom.bottom;
			}
			
			if (inside){
				space.set(start.x, start.y, start.x, start.y);
				return space;
			}
			
			if (curDiff < closestDiff){
				closestDiff = curDiff;
				closestRoom = curRoom;
			}
		
		}
		
		int wDiff, hDiff;
		if (closestRoom != null){
			
			wDiff = Integer.MAX_VALUE;
			if (closestRoom.left >= start.x){
				wDiff = (space.right - closestRoom.left) * (space.height() + 1);
			} else if (closestRoom.right <= start.x){
				wDiff = (closestRoom.right - space.left) * (space.height() + 1);
			}
			
			hDiff = Integer.MAX_VALUE;
			if (closestRoom.top >= start.y){
				hDiff = (space.bottom - closestRoom.top) * (space.width() + 1);
			} else if (closestRoom.bottom <= start.y){
				hDiff = (closestRoom.bottom - space.top) * (space.width() + 1);
			}
			
			//reduce by as little as possible to resolve the collision
			if (wDiff < hDiff || wDiff == hDiff && Random.Int(2) == 0){
				if (closestRoom.left >= start.x && closestRoom.left < space.right) space.right = closestRoom.left;
				if (closestRoom.right <= start.x && closestRoom.right > space.left) space.left = closestRoom.right;
			} else {
				if (closestRoom.top >= start.y && closestRoom.top < space.bottom) space.bottom = closestRoom.top;
				if (closestRoom.bottom <= start.y && closestRoom.bottom > space.top) space.top = closestRoom.bottom;
			}
			colliding.remove(closestRoom);
		} else {
			colliding.clear();
		}
		
		//loop until we are no longer colliding with any rooms
	} while (!colliding.isEmpty());
	
	return space;
}
 
Example 16
Source File: RegularPainter.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
protected boolean joinRooms( Level l, Room r, Room n ) {
	
	if (!(r instanceof EmptyRoom && n instanceof EmptyRoom)) {
		return false;
	}
	
	//TODO decide on good probabilities and dimension restrictions
	Rect w = r.intersect( n );
	if (w.left == w.right) {
		
		if (w.bottom - w.top < 3) {
			return false;
		}
		
		if (w.height()+1 == Math.max( r.height(), n.height() )) {
			return false;
		}
		
		if (r.width() + n.width() > 10) {
			return false;
		}
		
		w.top += 1;
		w.bottom -= 0;
		
		w.right++;
		
		Painter.fill( l, w.left, w.top, 1, w.height(), Terrain.EMPTY );
		
	} else {
		
		if (w.right - w.left < 3) {
			return false;
		}
		
		if (w.width()+1 == Math.max( r.width(), n.width() )) {
			return false;
		}
		
		if (r.height() + n.height() > 10) {
			return false;
		}
		
		w.left += 1;
		w.right -= 0;
		
		w.bottom++;
		
		Painter.fill( l, w.left, w.top, w.width(), 1, Terrain.EMPTY );
	}
	
	return true;
}
 
Example 17
Source File: RegularLevel.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: Builder.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
protected static Rect findFreeSpace(Point start, ArrayList<Room> collision, int maxSize){
	Rect space = new Rect(start.x-maxSize, start.y-maxSize, start.x+maxSize, start.y+maxSize);

	//shallow copy
	ArrayList<Room> colliding = new ArrayList<>(collision);
	do{
		
		//remove empty rooms and any rooms we aren't currently overlapping
		Iterator<Room> it = colliding.iterator();
		while (it.hasNext()){
			Room room = it.next();
			//if not colliding
			if ( room.isEmpty()
					|| Math.max(space.left, room.left) >= Math.min(space.right, room.right)
					|| Math.max(space.top, room.top) >= Math.min(space.bottom, room.bottom) ){
				it.remove();
			}
		}
		
		//iterate through all rooms we are overlapping, and find the closest one
		Room closestRoom = null;
		int closestDiff = Integer.MAX_VALUE;
		boolean inside = true;
		int curDiff = 0;
		for (Room curRoom : colliding){
			
			if (start.x <= curRoom.left){
				inside = false;
				curDiff += curRoom.left - start.x;
			} else if (start.x >= curRoom.right){
				inside = false;
				curDiff += start.x - curRoom.right;
			}
		
			if (start.y <= curRoom.top){
				inside = false;
				curDiff += curRoom.top - start.y;
			} else if (start.y >= curRoom.bottom){
				inside = false;
				curDiff += start.y - curRoom.bottom;
			}
			
			if (inside){
				space.set(start.x, start.y, start.x, start.y);
				return space;
			}
			
			if (curDiff < closestDiff){
				closestDiff = curDiff;
				closestRoom = curRoom;
			}
		
		}
		
		int wDiff, hDiff;
		if (closestRoom != null){
			
			wDiff = Integer.MAX_VALUE;
			if (closestRoom.left >= start.x){
				wDiff = (space.right - closestRoom.left) * (space.height() + 1);
			} else if (closestRoom.right <= start.x){
				wDiff = (closestRoom.right - space.left) * (space.height() + 1);
			}
			
			hDiff = Integer.MAX_VALUE;
			if (closestRoom.top >= start.y){
				hDiff = (space.bottom - closestRoom.top) * (space.width() + 1);
			} else if (closestRoom.bottom <= start.y){
				hDiff = (closestRoom.bottom - space.top) * (space.width() + 1);
			}
			
			//reduce by as little as possible to resolve the collision
			if (wDiff < hDiff || wDiff == hDiff && Random.Int(2) == 0){
				if (closestRoom.left >= start.x && closestRoom.left < space.right) space.right = closestRoom.left;
				if (closestRoom.right <= start.x && closestRoom.right > space.left) space.left = closestRoom.right;
			} else {
				if (closestRoom.top >= start.y && closestRoom.top < space.bottom) space.bottom = closestRoom.top;
				if (closestRoom.bottom <= start.y && closestRoom.bottom > space.top) space.top = closestRoom.bottom;
			}
			colliding.remove(closestRoom);
		} else {
			colliding.clear();
		}
		
		//loop until we are no longer colliding with any rooms
	} while (!colliding.isEmpty());
	
	return space;
}
 
Example 19
Source File: InfiniteLevel.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
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;
    }
 
Example 20
Source File: RegularLevel.java    From YetAnotherPixelDungeon with GNU General Public License v3.0 4 votes vote down vote up
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;
}