-
Notifications
You must be signed in to change notification settings - Fork 1
#004 Rooms
Valkryst edited this page Jul 20, 2018
·
2 revisions
With the Map, MapTile, and Sprite classes written, we can begin to create an area in which the player can move around.
As with many early Roguelikes such as Rogue (1980), Sword of Fargoal (1982), and Moria (1983), we're going to create rooms filled with obstacles, monsters, and loot for the player to encounter. Aside from containing these things, a Room is a rectangular area on a Map.
Our Room class can be defined as follows:
- Position
- The (x, y) coordinates of the top-left corner of the room within a Map.
- Dimensions
- The width and height of the room.
- A function to "carve" the Room into a Map.
package com.valkryst.VTerminal_Tutorial;
import java.awt.Dimension;
import java.awt.Point;
public class Room {
/** The coordinates of the top-left tile. */
private final Point position;
/** The width & height. */
private final Dimension dimensions;
/** Constructs a new Room. */
public Room() {
this.position = new Point(0, 0);
this.dimensions = new Dimension(0, 0);
}
/**
* Constructs a new Room.
*
* @param position
* The coordinates of the top-left tile.
*
* @param dimensions
* The width & height.
*/
public Room(final Point position, final Dimension dimensions) {
this.position = position;
this.dimensions = dimensions;
}
/**
* Carves the room into a Map.
*
* @param map
* The map.
*/
public void carve(final Map map) {
final MapTile[][] tiles = map.getMapTiles();
for (int y = position.y ; y < position.y + dimensions.height ; y++) {
for (int x = position.x ; x < position.x + dimensions.width ; x++) {
tiles[y][x].setSprite(Sprite.DIRT);
tiles[y][x].setSolid(false);
}
}
map.updateLayerTiles();
}
}
We can now go back to the Driver class and create a new Room at position (10x, 10y) with a width of 10 and height of 5.
import com.valkryst.VTerminal.Screen;
import java.awt.*;
import java.io.IOException;
public class Driver {
public static void main(String[] args) throws IOException {
final Screen screen = new Screen(80, 40);
screen.addCanvasToFrame();
final Map map = new Map();
screen.addComponent(map);
final Point position = new Point(10, 10);
final Dimension dimensions = new Dimension(10, 5);
final Room room = new Room(position, dimensions);
room.carve(map);
screen.draw();
}
}